aboutsummaryrefslogtreecommitdiff
path: root/js/io/system/fileio.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/io/system/fileio.js')
-rwxr-xr-x[-rw-r--r--]js/io/system/fileio.js279
1 files changed, 240 insertions, 39 deletions
diff --git a/js/io/system/fileio.js b/js/io/system/fileio.js
index 1d76a91b..045fa2fd 100644..100755
--- a/js/io/system/fileio.js
+++ b/js/io/system/fileio.js
@@ -4,21 +4,247 @@ No rights, expressed or implied, whatsoever to this software are provided by Mot
4(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. 4(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved.
5</copyright> */ 5</copyright> */
6 6
7//Required modules 7/* /////////////////////////////////////////////////////////////////////
8var Serializer = require("montage/core/serializer").Serializer; 8////////////////////////////////////////////////////////////////////////
9NOTES:
10
11 For newFile, only the 'uri' is required, if contents is empty, such
12 empty file will be created. 'contents' should be a string to be saved
13 as the file. 'contentType' is the mime type of the file.
14
15 Core API reference in NINJA: this.application.ninja.coreIoApi
16
17////////////////////////////////////////////////////////////////////////
18///////////////////////////////////////////////////////////////////// */
19//
20var Montage = require("montage/core/core").Montage,
21 Component = require("montage/ui/component").Component;
22////////////////////////////////////////////////////////////////////////
9//Exporting as File I/O 23//Exporting as File I/O
10exports.FileIo = (require("montage/core/core").Montage).create(Object.prototype, { 24exports.FileIo = Montage.create(Component, {
11 /*
12create: {
13 enumerable: true,
14 value: function (type) {
15 //
16 }
17 },
18*/
19 //////////////////////////////////////////////////////////////////// 25 ////////////////////////////////////////////////////////////////////
26 //newFile Object (*required): {uri*, contents, contentType}
27 newFile: {
28 enumerable: true,
29 value: function(file) {
30 //Checking for API to be available
31 if (!this.application.ninja.coreIoApi.cloudAvailable()) {
32 //API not available, no IO action taken
33 return null;
34 }
35 //Peforming check for file to exist
36 var check = this.application.ninja.coreIoApi.fileExists({uri: file.uri}), status, create;
37 //Upon successful check, handling results
38 if (check.success) {
39 //Handling status of check
40 switch (check.status) {
41 case 204:
42 //Storing status to be returned (for UI handling)
43 status = check.status;
44 break;
45 case 404:
46 //File does not exists, ready to be created
47 create = this.application.ninja.coreIoApi.createFile(file);
48 status = create.status;
49 break;
50 default:
51 //Unknown Error
52 break;
53 }
54 } else {
55 //Unknown Error
56 }
57 //Returning resulting code
58 return status;
59 // 204: File exists | 400: File exists | 404: File does not exists
60 // 201: File succesfully created | 500: Unknown | undefined: Unknown
61 }
62 },
63 ////////////////////////////////////////////////////////////////////
64 //
65 readFile: {
66 enumerable: true,
67 value: function(file) {
68 //Checking for API to be available
69 if (!this.application.ninja.coreIoApi.cloudAvailable()) {
70 //API not available, no IO action taken
71 return null;
72 }
73 //Peforming check for file to exist
74 var check = this.application.ninja.coreIoApi.fileExists({uri: file.uri}), status, create, result;
75 //Upon successful check, handling results
76 if (check.success) {
77 //Handling status of check
78 switch (check.status) {
79 case 204:
80 //File exists
81 result = {};
82 result.content = this.application.ninja.coreIoApi.readFile(file).content;
83 result.details = this.infoFile(file);
84 status = check.status;
85 break;
86 case 404:
87 //File does not exists, ready to be created
88 status = check.status;
89 break;
90 default:
91 //Unknown Error
92 status = 500;
93 break;
94 }
95 } else {
96 //Unknown Error
97 status = 500;
98 }
99 //Returning resulting code
100 return {status: status, file: result};
101 }
102 },
103 ////////////////////////////////////////////////////////////////////
104 //
105 saveFile: {
106 enumerable: true,
107 value: function() {
108 //Checking for API to be available
109 if (!this.application.ninja.coreIoApi.cloudAvailable()) {
110 //API not available, no IO action taken
111 return null;
112 }
113 //
114 }
115 },
116 ////////////////////////////////////////////////////////////////////
117 //
118 deleteFile: {
119 enumerable: true,
120 value: function() {
121 //Checking for API to be available
122 if (!this.application.ninja.coreIoApi.cloudAvailable()) {
123 //API not available, no IO action taken
124 return null;
125 }
126 //
127 }
128 },
129 ////////////////////////////////////////////////////////////////////
130 //
131 copyFile: {
132 enumerable: true,
133 value: function() {
134 //Checking for API to be available
135 if (!this.application.ninja.coreIoApi.cloudAvailable()) {
136 //API not available, no IO action taken
137 return null;
138 }
139 //
140 }
141 },
142 ////////////////////////////////////////////////////////////////////
143 //
144 infoFile: {
145 enumerable: true,
146 value: function(file) {
147 //Checking for API to be available
148 if (!this.application.ninja.coreIoApi.cloudAvailable()) {
149 //API not available, no IO action taken
150 return null;
151 }
152 //
153 var check = this.application.ninja.coreIoApi.fileExists({uri: file.uri}), details;
154 //
155 if (check.success) {
156 //Handling status of check
157 switch (check.status) {
158 case 204:
159 //File exists
160 details = JSON.parse(this.application.ninja.coreIoApi.isFileWritable(file).content);
161 details.uri = file.uri;
162 details.name = this.getFileNameFromPath(file.uri);
163 details.extension = details.name.split('.')[details.name.split('.').length-1];
164 break;
165 case 404:
166 //File does not exists, ready to be created
167
168 break;
169 default:
170 //Unknown Error
171
172 break;
173 }
174 } else {
175 //Unknown Error
176
177 }
178 return details;
179 }
180 },
181 ////////////////////////////////////////////////////////////////////
20 // 182 //
21 open: { 183 getFileNameFromPath : {
184 value: function(path) {
185 path = path.replace(/[/\\]$/g,"");
186 path = path.replace(/\\/g,"/");
187 return path.substr(path.lastIndexOf('/') + 1);
188 }
189 }
190 ////////////////////////////////////////////////////////////////////
191 ////////////////////////////////////////////////////////////////////
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230