aboutsummaryrefslogtreecommitdiff
path: root/js/io/system/fileio.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/io/system/fileio.js')
-rwxr-xr-xjs/io/system/fileio.js292
1 files changed, 46 insertions, 246 deletions
diff --git a/js/io/system/fileio.js b/js/io/system/fileio.js
index 045fa2fd..f78ad173 100755
--- a/js/io/system/fileio.js
+++ b/js/io/system/fileio.js
@@ -23,7 +23,7 @@ var Montage = require("montage/core/core").Montage,
23//Exporting as File I/O 23//Exporting as File I/O
24exports.FileIo = Montage.create(Component, { 24exports.FileIo = Montage.create(Component, {
25 //////////////////////////////////////////////////////////////////// 25 ////////////////////////////////////////////////////////////////////
26 //newFile Object (*required): {uri*, contents, contentType} 26 //Creating new file
27 newFile: { 27 newFile: {
28 enumerable: true, 28 enumerable: true,
29 value: function(file) { 29 value: function(file) {
@@ -49,19 +49,21 @@ exports.FileIo = Montage.create(Component, {
49 break; 49 break;
50 default: 50 default:
51 //Unknown Error 51 //Unknown Error
52 status = 500;
52 break; 53 break;
53 } 54 }
54 } else { 55 } else {
55 //Unknown Error 56 //Unknown Error
57 status = 500;
56 } 58 }
57 //Returning resulting code 59 //Returning resulting code
58 return status; 60 return status;
59 // 204: File exists | 400: File exists | 404: File does not exists 61 // 204: File exists (not created) | 400: File exists | 404: File does not exists
60 // 201: File succesfully created | 500: Unknown | undefined: Unknown 62 // 201: File succesfully created | 500: Unknown
61 } 63 }
62 }, 64 },
63 //////////////////////////////////////////////////////////////////// 65 ////////////////////////////////////////////////////////////////////
64 // 66 //Reading contents from file
65 readFile: { 67 readFile: {
66 enumerable: true, 68 enumerable: true,
67 value: function(file) { 69 value: function(file) {
@@ -84,7 +86,7 @@ exports.FileIo = Montage.create(Component, {
84 status = check.status; 86 status = check.status;
85 break; 87 break;
86 case 404: 88 case 404:
87 //File does not exists, ready to be created 89 //File does not exists
88 status = check.status; 90 status = check.status;
89 break; 91 break;
90 default: 92 default:
@@ -96,21 +98,51 @@ exports.FileIo = Montage.create(Component, {
96 //Unknown Error 98 //Unknown Error
97 status = 500; 99 status = 500;
98 } 100 }
99 //Returning resulting code 101 //Returning status and result (null if none)
100 return {status: status, file: result}; 102 return {status: status, file: result};
103 //Status Codes
104 // 204: File exists | 404: File does not exists | 500: Unknown
101 } 105 }
102 }, 106 },
103 //////////////////////////////////////////////////////////////////// 107 ////////////////////////////////////////////////////////////////////
104 // 108 //Saving file (existing file or creates and saves if none exists)
105 saveFile: { 109 saveFile: {
106 enumerable: true, 110 enumerable: true,
107 value: function() { 111 value: function(file) {
108 //Checking for API to be available 112 //Checking for API to be available
109 if (!this.application.ninja.coreIoApi.cloudAvailable()) { 113 if (!this.application.ninja.coreIoApi.cloudAvailable()) {
110 //API not available, no IO action taken 114 //API not available, no IO action taken
111 return null; 115 return null;
112 } 116 }
113 // 117 //Peforming check for file to exist
118 var check = this.application.ninja.coreIoApi.fileExists({uri: file.uri}), status, result;
119 //Upon successful check, handling results
120 if (check.success) {
121 //Handling status of check
122 switch (check.status) {
123 case 204:
124 //File exists
125 result = this.application.ninja.coreIoApi.updateFile(file);
126 status = 204;
127 break;
128 case 404:
129 //File does not exists, ready to be created
130 result = this.application.ninja.coreIoApi.createFile(file);
131 status = 404;
132 break;
133 default:
134 //Unknown Error
135 status = 500;
136 break;
137 }
138 } else {
139 //Unknown Error
140 status = 500;
141 }
142 //Returning status and result (null if none)
143 return {status: status, result: result};
144 //Status Codes
145 // 204: File exists | 404: File does not exists | 500: Unknown
114 } 146 }
115 }, 147 },
116 //////////////////////////////////////////////////////////////////// 148 ////////////////////////////////////////////////////////////////////
@@ -161,19 +193,20 @@ exports.FileIo = Montage.create(Component, {
161 details.uri = file.uri; 193 details.uri = file.uri;
162 details.name = this.getFileNameFromPath(file.uri); 194 details.name = this.getFileNameFromPath(file.uri);
163 details.extension = details.name.split('.')[details.name.split('.').length-1]; 195 details.extension = details.name.split('.')[details.name.split('.').length-1];
196 details.status = 204;
164 break; 197 break;
165 case 404: 198 case 404:
166 //File does not exists, ready to be created 199 //File does not exists, ready to be created
167 200 details = {status: 404, uri: file.uri, name: this.getFileNameFromPath(file.uri)};
168 break; 201 break;
169 default: 202 default:
170 //Unknown Error 203 //Unknown Error
171 204 details = {status: 500, uri: file.uri, name: this.getFileNameFromPath(file.uri)};
172 break; 205 break;
173 } 206 }
174 } else { 207 } else {
175 //Unknown Error 208 //Unknown Error
176 209 details = {status: 500, uri: file.uri, name: this.getFileNameFromPath(file.uri)};
177 } 210 }
178 return details; 211 return details;
179 } 212 }
@@ -188,240 +221,7 @@ exports.FileIo = Montage.create(Component, {
188 } 221 }
189 } 222 }
190 //////////////////////////////////////////////////////////////////// 223 ////////////////////////////////////////////////////////////////////
191 //////////////////////////////////////////////////////////////////// 224 ////////////////////////////////////////////////////////////////////
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246 /*
247open: {
248 enumerable: true,
249 value: function(doc, type, uri, server) {
250 //
251 var file = {}, head, body, h, b;
252 file.uri = uri;
253 file.server = server;
254 //
255 if (doc.content) {
256 if (type === 'html' || type === 'htm') {
257 //
258 h = doc.content.split('</head>');
259 h = h[0].split('<head>');
260 head = h[1];
261 //
262 b = doc.content.split('</body>');
263 b = b[0].split('<body>');
264 body = b[1];
265 //
266 file.type = 'html';
267 file.head = head;
268 file.body = body;
269 } else {
270 //TODO: Add other file type routines
271 file.type = type;
272 file.content = doc.content;
273 }