diff options
author | Jose Antonio Marquez | 2012-01-27 12:05:17 -0800 |
---|---|---|
committer | Jose Antonio Marquez | 2012-01-27 12:05:17 -0800 |
commit | 3a754133dbc138390503341fd2e9beba3e43aa4b (patch) | |
tree | cdeae7d7dd9a30d7b4fab5afb7efad68d4ec7508 /js/io/system/coreioapi.js | |
parent | b89a7ee8b956c96a1dcee995ea840feddc5d4b27 (diff) | |
download | ninja-3a754133dbc138390503341fd2e9beba3e43aa4b.tar.gz |
Merged old FileIO
Diffstat (limited to 'js/io/system/coreioapi.js')
-rwxr-xr-x | js/io/system/coreioapi.js | 819 |
1 files changed, 819 insertions, 0 deletions
diff --git a/js/io/system/coreioapi.js b/js/io/system/coreioapi.js new file mode 100755 index 00000000..c920d8cd --- /dev/null +++ b/js/io/system/coreioapi.js | |||
@@ -0,0 +1,819 @@ | |||
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 | /* ///////////////////////////////////////////////////////////////////// | ||
8 | //////////////////////////////////////////////////////////////////////// | ||
9 | NOTES: | ||
10 | These methods should only be access through the file and project IO classes. | ||
11 | //////////////////////////////////////////////////////////////////////// | ||
12 | ///////////////////////////////////////////////////////////////////// */ | ||
13 | var Montage = require("montage/core/core").Montage, | ||
14 | Component = require("montage/ui/component").Component; | ||
15 | //////////////////////////////////////////////////////////////////////// | ||
16 | //Exporting as Project I/O | ||
17 | exports.CoreIoApi = Montage.create(Component, { | ||
18 | //////////////////////////////////////////////////////////////////// | ||
19 | // private property containing the file service URL to use for all file IO calls | ||
20 | _fileServiceURL: { | ||
21 | enumerable: false, | ||
22 | value: "http://localhost:16380/file" | ||
23 | }, | ||
24 | //////////////////////////////////////////////////////////////////// | ||
25 | // private property containing the directory service URL to use for all file IO calls | ||
26 | _directoryServiceURL: { | ||
27 | enumerable: false, | ||
28 | value: "http://localhost:16380/directory" | ||
29 | }, | ||
30 | //////////////////////////////////////////////////////////////////// | ||
31 | // private helper to parse URIs and append them to the service URL | ||
32 | _prepareServiceURL: { | ||
33 | enumerable: false, | ||
34 | value: function(serviceURL, path) { | ||
35 | var urlOut = path.replace(/\\/g,"/"); | ||
36 | urlOut = urlOut.replace(/:/g,""); | ||
37 | urlOut = encodeURI(urlOut); | ||
38 | //add leading / if not already there | ||
39 | if((urlOut.length > 0) && (urlOut.charAt(0) !== "/")){ | ||
40 | urlOut = "/" + urlOut; | ||
41 | } | ||
42 | //remove extra / at the end | ||
43 | if((urlOut.length > 1) && (urlOut.charAt(urlOut.length - 1) === "/")){ | ||
44 | urlOut = urlOut.substring(0, (urlOut.length - 1)); | ||
45 | } | ||
46 | |||
47 | return serviceURL + urlOut; | ||
48 | } | ||
49 | }, | ||
50 | //////////////////////////////////////////////////////////////////// | ||
51 | // Checks for the existence of a file | ||
52 | // Parameters: | ||
53 | // the file parameter must contain the following properties | ||
54 | // uri: string value containing the full file path/URI i.e. "c:/foo/bar.html" | ||
55 | // | ||
56 | // Return values: | ||
57 | // returns an object with two properties | ||
58 | // success: boolean indicating if the call succeeded or failed | ||
59 | // status: int indicating the request HTTP status code | ||
60 | // 204 - the file exists | ||
61 | // 404 - the file does not exist | ||
62 | // 500 - unknown server error occurred | ||
63 | fileExists: { | ||
64 | enumerable: false, | ||
65 | value: function(file) { | ||
66 | // | ||
67 | var retValue = { success:null, status:null }; | ||
68 | // | ||
69 | if(file && file.uri && file.uri.length) { | ||
70 | try { | ||
71 | var serviceURL = this._prepareServiceURL(this._fileServiceURL, file.uri), | ||
72 | xhr = new XMLHttpRequest(); | ||
73 | // | ||
74 | xhr.open("GET", serviceURL, false); | ||
75 | xhr.setRequestHeader("check-existence-only", "true"); | ||
76 | xhr.send(); | ||
77 | // | ||
78 | if (xhr.readyState === 4) { | ||
79 | retValue.status = xhr.status; | ||
80 | retValue.success = true; | ||
81 | } | ||
82 | } | ||
83 | catch(error) { | ||
84 | xhr = null; | ||
85 | retValue.success = false; | ||
86 | } | ||
87 | } | ||
88 | // | ||
89 | return retValue; | ||
90 | } | ||
91 | }, | ||
92 | //////////////////////////////////////////////////////////////////// | ||
93 | // Creates a new file at the specified path | ||
94 | // Parameters: | ||
95 | // the file parameter must contain the following properties | ||
96 | // uri: string value containing the full file path/URI i.e. "c:/foo/bar.html" | ||
97 | // it can optionally contain the following properties | ||
98 | // contentType: string with the content type i.e. "text/plain". "text/plain" is assumed if this property is not specified | ||
99 | // contents: string containing the file contents. These contents will be saved to the new file. | ||
100 | // | ||
101 | // Return values: | ||
102 | // returns an object with two properties | ||
103 | // success: boolean indicating if the call succeeded or failed | ||
104 | // status: int indicating the request HTTP status code | ||
105 | // 201 - the file was created and contents were saved if they were passed | ||
106 | // 400 - the file already exists and could not be created | ||
107 | // 500 - unknown server error occurred | ||
108 | createFile: { | ||
109 | enumerable: false, | ||
110 | value: function(file) { | ||
111 | var retValue = { success:null, status:null }; | ||
112 | if(file && file.uri && file.uri.length) { | ||
113 | try { | ||
114 | var serviceURL = this._prepareServiceURL(this._fileServiceURL, file.uri), | ||
115 | xhr = new XMLHttpRequest(); | ||
116 | // | ||
117 | xhr.open("POST", serviceURL, false); | ||
118 | if(file.contentType && file.contentType.length) | ||
119 | xhr.setRequestHeader("Content-Type", file.contentType); | ||
120 | else | ||
121 | xhr.setRequestHeader("Content-Type", "text/plain"); | ||
122 | |||
123 | if(file.contents && file.contents.length) | ||
124 | xhr.send(file.contents); | ||
125 | else | ||
126 | xhr.send(); | ||
127 | |||
128 | if (xhr.readyState === 4) { | ||
129 | retValue.status = xhr.status; | ||
130 | retValue.success = true; | ||
131 | } | ||
132 | } | ||
133 | catch(error) { | ||
134 | xhr = null; | ||
135 | retValue.success = false; | ||
136 | } | ||
137 | } | ||
138 | |||
139 | return retValue; | ||
140 | } | ||
141 | }, | ||
142 | //////////////////////////////////////////////////////////////////// | ||
143 | // Save contents into an existing file at the specified path | ||
144 | // Parameters: | ||
145 | // the file parameter must contain the following properties | ||
146 | // uri: string value containing the full file path/URI i.e. "c:/foo/bar.html" | ||
147 | // it can optionally contain the following properties | ||
148 | // contentType: string with the content type i.e. "text/plain". "text/plain" is assumed if this property is not specified | ||
149 | // contents: string containing the file contents. These contents will be saved to the new file. | ||
150 | // | ||
151 | // Return values: | ||
152 | // returns an object with two properties | ||
153 | // success: boolean indicating if the call succeeded or failed | ||
154 | // status: int indicating the request HTTP status code | ||
155 | // 204 - the file was saved | ||
156 | // 404 - the file specified does not exist | ||
157 | // 500 - unknown server error occurred | ||
158 | updateFile: { | ||
159 | enumerable: false, | ||
160 | value: function(file) { | ||
161 | var retValue = { success:null, status:null }; | ||
162 | if(file && file.uri && file.uri.length && file.contents && file.contents.length) { | ||
163 | try { | ||
164 | var serviceURL = this._prepareServiceURL(this._fileServiceURL, file.uri), | ||
165 | xhr = new XMLHttpRequest(); | ||
166 | // | ||
167 | xhr.open("PUT", serviceURL, false); | ||
168 | if(file.contentType && file.contentType.length) | ||
169 | xhr.setRequestHeader("Content-Type", file.contentType); | ||
170 | else | ||
171 | xhr.setRequestHeader("Content-Type", "text/plain"); | ||
172 | |||
173 | xhr.send(file.contents); | ||
174 | |||
175 | if (xhr.readyState === 4) { | ||
176 | retValue.status = xhr.status; | ||
177 | retValue.success = true; | ||
178 | } | ||
179 | } | ||
180 | catch(error) { | ||
181 | xhr = null; | ||
182 | retValue.success = false; | ||
183 | } | ||
184 | } | ||
185 | |||
186 | return retValue; | ||
187 | } | ||
188 | }, | ||
189 | //////////////////////////////////////////////////////////////////// | ||
190 | // Copies a file from one location to another | ||
191 | // Parameters: | ||
192 | // the file parameter must contain the following properties | ||
193 | // sourceUri: string value containing the full file path/URI to copy from i.e. "c:/foo/bar.html" | ||
194 | // destUri: string containing the full path/URI to copy to | ||
195 | // it can optionally contain the following properties | ||
196 | // overwriteDestination: bool indicating whether it is okay to overwrite the file specified at destUri if it already exists | ||
197 | // | ||
198 | // Return values: | ||
199 | // returns an object with two properties | ||
200 | // success: boolean indicating if the call succeeded or failed | ||
201 | // status: int indicating the request HTTP status code | ||
202 | // 204 - the file was copied | ||
203 | // 404 - the file specified in sourceUri does not exist | ||
204 | // 500 - unknown server error occurred | ||
205 | copyFile: { | ||
206 | enumerable: false, | ||
207 | value: function(file) { | ||
208 | var retValue = { success:null, status:null }; | ||
209 | if(file && file.sourceUri && file.sourceUri.length && file.destUri && file.destUri.length) { | ||
210 | try { | ||
211 | var serviceURL = this._prepareServiceURL(this._fileServiceURL, file.destUri), | ||
212 | xhr = new XMLHttpRequest(); | ||
213 | // | ||
214 | xhr.open("PUT", serviceURL, false); | ||
215 | xhr.setRequestHeader("sourceURI", file.sourceUri); | ||
216 | // | ||
217 | if(file.overwriteDestination && file.overwriteDestination === true) { | ||
218 | xhr.setRequestHeader("overwrite-destination", "true"); | ||
219 | } | ||
220 | // | ||
221 | xhr.send(); | ||
222 | // | ||
223 | if (xhr.readyState === 4) { | ||
224 | retValue.status = xhr.status; | ||
225 | retValue.success = true; | ||
226 | } | ||
227 | } | ||
228 | catch(error) { | ||