1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
|
/* <copyright>
This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved.
</copyright> */
/* /////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
NOTES:
The init function starts up the file system API, and a size must be
set, no unlimited available as of now.
////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////// */
//
var Montage = require("montage/core/core").Montage;
////////////////////////////////////////////////////////////////////////
//
exports.ChromeApi = Montage.create(Object.prototype, {
////////////////////////////////////////////////////////////////////
//Needs size in MBs for fileSystem init
init: {
enumerable: true,
value: function(size) {
//
if (window.webkitRequestFileSystem) {
//Current way to init Chrome's fileSystem API
window.webkitRequestFileSystem(window.PERSISTENT, size*1024*1024, function (fs) {
this.fileSystem = fs;
}.bind(this));
return true;
} else {
//No fileSystem API
return false;
}
}
},
////////////////////////////////////////////////////////////////////
//
_fileSystem: {
enumerable: false,
value: null
},
////////////////////////////////////////////////////////////////////
//
fileSystem: {
enumerable: false,
get: function() {
return this._fileSystem;
},
set: function(value) {
this._fileSystem = value;
}
},
////////////////////////////////////////////////////////////////////
//
directoryNew: {
enumerable: true,
value: function() {
}
},
////////////////////////////////////////////////////////////////////
//
directoryDelete: {//TODO: Make sure it uses a force delete
enumerable: true,
value: function() {
}
},
////////////////////////////////////////////////////////////////////
//
directoryContents: {
enumerable: true,
value: function() {
}
},
////////////////////////////////////////////////////////////////////
//
directoryCopy: {
enumerable: true,
value: function() {
}
},
////////////////////////////////////////////////////////////////////
//
directoryRename: {
enumerable: true,
value: function() {
}
},
////////////////////////////////////////////////////////////////////
//
directoryMove: {
enumerable: true,
value: function() {
}
}
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
});
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
//window.webkitRequestFileSystem(window.PERSISTENT, 10*1024*1024 /*10MB*/, function (fs) {
/*
for (var i=1; i<50; i++) {
fs.root.getDirectory('montage0.0.0.'+i, {}, function(dirEntry) {
//
dirEntry.removeRecursively(function() {
console.log('Directory removed.');
});
});
}
*/
//
/*
var xhr = new XMLHttpRequest(), dir, mjs;
//
xhr.open("GET", '/ninja-internal/node_modules/descriptor.json', false);
xhr.send();
//
if (xhr.readyState === 4) {
//
mjs = JSON.parse(xhr.response);
//
if (mjs.version) {
//Checking for version to exist
fs.root.getDirectory('montage'+mjs.version, {}, function(dirEntry) {
//Already copied, nothing
console.log('montage'+mjs.version+' has already been created');
}, function (e) {
//Not present, should be copied
createFolder(false, {name: 'montage'+mjs.version});
//
for (var i in mjs.directories) {
createFolder('montage'+mjs.version, mjs.directories[i]);
}
//
for (var j in mjs.files) {
var frqst = new XMLHttpRequest();
frqst.open("GET", '/ninja-internal/node_modules/montage/'+mjs.files[j], false);
frqst.send();
if (frqst.readyState === 4) {
createFile('montage'+mjs.version+'/'+mjs.files[j], frqst.response);
}
}
//
console.log('montage'+mjs.version+' was created');
}, folderError);
}
}
//
function createFile (path, content) {
//
fs.root.getFile(path, {create: true}, function(fileEntry) {
//
fileEntry.createWriter(function(fileWriter) {
//
//console.log(path);
var bb = new window.WebKitBlobBuilder;
bb.append(content);
fileWriter.write(bb.getBlob('text/plain'));
}, fileError);
}, fileError);
}
//
function createFolder(root, folder) {
if (folder.name) {
if (root) {
dir = root+'/'+folder.name;
} else {
dir = folder.name;
}
//
//console.log(dir);
//
fs.root.getDirectory(dir, {create: true}, function(dirEntry) {
//
}, folderError);
}
//
if (folder.children) {
for (var i in folder.children) {
if (root) {
createFolder(root+'/'+folder.name, folder.children[i]);
} else {
createFolder(folder.name, folder.children[i]);
}
}
}
}
//
function folderError (e) {
console.log(e);
}
function fileError (e) {
console.log(e);
}
setTimeout( function () {
for (var m in mjs.files) {
fs.root.getFile('montage'+mjs.version+'/'+mjs.files[m], {}, function(fileEntry) {
console.log(mjs.files[m]);
fileEntry.file(function(file) {
var reader = new FileReader();
reader.onloadend = function(e) {
//console.log(e.target.file.name);
var test = this.createFile({uri: '/Users/kgq387/Desktop/Ninja Cloud/Disk/'+e.target.file.name, contents: e.target.result});
}.bind(this);
reader.file = file;
reader.readAsText(file);
}.bind(this));
}.bind(this));
}}.bind(this), 5000);
*/
//}.bind(this));
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
//TODO: Remove, this is only for testing
/*
window.webkitRequestFileSystem(window.PERSISTENT, 10*1024*1024 , function (fs) {
var start = new Date().getTime();
for (var i=0; i<250; i++) {
fs.root.getFile('test'+i+'.txt', {create: true}, function(fileEntry) {
fileEntry.createWriter(function(fileWriter) {
var xhr = new XMLHttpRequest();
//
xhr.open("GET", '/ninja-internal/js/io/templates/descriptor.json', false);
xhr.send();
//
if (xhr.readyState === 4) {
var bb = new window.WebKitBlobBuilder;
bb.append(xhr.response);
fileWriter.write(bb.getBlob('text/plain'));
}
});
});
}
var end = new Date().getTime();
var time = end - start;
console.log('Create execution time: ' + time);
start = new Date().getTime();
for (var j=0; j<250; j++) {
fs.root.getFile('test'+j+'.txt', {create: true}, function(fileEntry) {
fileEntry.file(function(file) {
var reader = new FileReader();
reader.onloadend = function(e) {
//console.log(this, e.target);
var test = this.createFile({uri: '/Users/kgq387/Desktop/Ninja Cloud/Disk/'+e.target.file.name, contents: e.target.result});
console.log(e.target.file.name);
}.bind(this);
reader.file = file;
reader.readAsText(file);
}.bind(this));
}.bind(this));
}
end = new Date().getTime();
time = end - start;
console.log('Read execution time: ' + time);
for (var k=0; k<250; k++) {
fs.root.getFile('test'+k+'.txt', {create: true}, function(fileEntry) {
fileEntry.remove(function(fileWriter) {
});
});
}
}.bind(this));
*/
|