diff options
author | Ananya Sen | 2012-01-30 16:02:15 -0800 |
---|---|---|
committer | Ananya Sen | 2012-01-30 16:02:15 -0800 |
commit | 63bfd3bd4a3165d83893c67cd4e6442a4794c0fb (patch) | |
tree | 625ad71e46ed7976c75806ceaaa45197cf42ddc1 /js/io/utils | |
parent | 1fd16ce7052853719ec27527157f38b2fc87b077 (diff) | |
parent | fcd9d2f2fd63c11160fcabdc8a554a1b2a81cc47 (diff) | |
download | ninja-63bfd3bd4a3165d83893c67cd4e6442a4794c0fb.tar.gz |
Merge branch 'refs/heads/IOWorkflow' into FileIO
Diffstat (limited to 'js/io/utils')
-rw-r--r-- | js/io/utils/file-utils.js | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/js/io/utils/file-utils.js b/js/io/utils/file-utils.js new file mode 100644 index 00000000..0afdffc6 --- /dev/null +++ b/js/io/utils/file-utils.js | |||
@@ -0,0 +1,75 @@ | |||
1 | /* <copyright> | ||
2 | This file contains proprietary software owned by Motorola Mobility, Inc.<br/> | ||
3 | No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/> | ||
4 | (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. | ||
5 | </copyright> */ | ||
6 | |||
7 | var fileSystem = require("js/io/system/coreioapi").CoreIoApi; | ||
8 | |||
9 | var FileUtils = exports.FileUtils = Object.create(Object.prototype, { | ||
10 | |||
11 | /*** | ||
12 | * checks for valid uri pattern | ||
13 | * also flags if Windows uri pattern and Unix uri patterns are mixed | ||
14 | */ | ||
15 | isValidUri:{ | ||
16 | value: function(uri){ | ||
17 | var isWindowsUri=false, isUnixUri=false,status=false; | ||
18 | if(uri !== ""){ | ||
19 | uri = uri.replace(/^\s+|\s+$/g,""); // strip any leading or trailing spaces | ||
20 | |||
21 | //for local machine folder uri | ||
22 | isWindowsUri = /^([a-zA-Z]:)(\\[^<>:"/\\|?*]+)*\\?$/gi.test(uri); | ||
23 | isUnixUri = /^(\/)?(\/(?![.])[^/]*)*\/?$/gi.test(uri);//folders beginning with . are hidden on Mac / Unix | ||
24 | status = isWindowsUri || isUnixUri; | ||
25 | if(isWindowsUri && isUnixUri){status = false;} | ||
26 | } | ||
27 | return status; | ||
28 | } | ||
29 | }, | ||
30 | |||
31 | /*** | ||
32 | * file name validation | ||
33 | */ | ||
34 | isValidFileName:{ | ||
35 | value: function(fileName){ | ||
36 | var status = false; | ||
37 | if(fileName !== ""){ | ||
38 | fileName = fileName.replace(/^\s+|\s+$/g,""); | ||
39 | status = !(/[/\\]/g.test(fileName)); | ||
40 | if(status && navigator.userAgent.indexOf("Macintosh") != -1){//for Mac files beginning with . are hidden | ||
41 | status = !(/^\./g.test(fileName)); | ||
42 | } | ||
43 | } | ||
44 | return status; | ||
45 | } | ||
46 | }, | ||
47 | |||
48 | /*** | ||
49 | * check if the file exists | ||
50 | */ | ||
51 | checkFileExists:{ | ||
52 | value: function(fileUri, folderUri, fileType){ | ||
53 | var uri = "", response=null, status=true; | ||
54 | |||
55 | //prepare absolute uri | ||
56 | if(/[^/\\]$/g.test(folderUri)){ | ||
57 | folderUri = folderUri + "/"; | ||
58 | } | ||
59 | |||
60 | //todo:add file extension check if fileType present | ||
61 | |||
62 | uri = ""+folderUri+fileUri; | ||
63 | |||
64 | response = fileSystem.fileExists({"uri":uri}); | ||
65 | if(!!response && response.success && (response.status === 204)){ | ||
66 | status = true; | ||
67 | }else if(!!response && response.success && (response.status === 404)){ | ||
68 | status = false; | ||
69 | }else{ | ||
70 | status = false; | ||
71 | } | ||
72 | return status; | ||
73 | } | ||
74 | } | ||
75 | }); \ No newline at end of file | ||