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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* Namespace for utility functions.
*/
var util = {
/**
* Returns a function that console.log's its arguments, prefixed by |msg|.
*
* @param {string} msg The message prefix to use in the log.
* @param {function(*)} opt_callback A function to invoke after logging.
*/
flog: function(msg, opt_callback) {
return function() {
var ary = Array.apply(null, arguments);
console.log(msg + ': ' + ary.join(', '));
if (opt_callback)
opt_callback.call(arguments);
};
},
/**
* Returns a function that throws an exception that includes its arguments
* prefixed by |msg|.
*
* @param {string} msg The message prefix to use in the exception.
*/
ferr: function(msg) {
return function() {
var ary = Array.apply(null, arguments);
throw new Error(msg + ': ' + ary.join(', '));
};
},
/**
* Install a sensible toString() on the FileError object.
*
* FileError.prototype.code is a numeric code describing the cause of the
* error. The FileError constructor has a named property for each possible
* error code, but provides no way to map the code to the named property.
* This toString() implementation fixes that.
*/
installFileErrorToString: function() {
FileError.prototype.toString = function() {
return '[object FileError: ' + util.getFileErrorMnemonic(this.code) + ']';
}
},
getFileErrorMnemonic: function(code) {
for (var key in FileError) {
if (key.search(/_ERR$/) != -1 && FileError[key] == code)
return key;
}
return code;
},
htmlUnescape: function(str) {
return str.replace(/&(lt|gt|amp);/g, function(entity) {
switch (entity) {
case '<': return '<';
case '>': return '>';
case '&': return '&';
}
});
},
/**
* Given a list of Entries, recurse any DirectoryEntries, and call back
* with a list of all file and directory entries encountered (including the
* original set).
*/
recurseAndResolveEntries: function(entries, successCallback, errorCallback) {
var pendingSubdirectories = 0;
var pendingFiles = 0;
var dirEntries = [];
var fileEntries = [];
var fileBytes = 0;
function pathCompare(a, b) {
if (a.fullPath > b.fullPath)
return 1;
if (a.fullPath < b.fullPath)
return -1;
return 0;
}
// We invoke this after each async callback to see if we've received all
// the expected callbacks. If so, we're done.
function areWeThereYet() {
if (pendingSubdirectories == 0 && pendingFiles == 0) {
var result = {
dirEntries: dirEntries.sort(pathCompare),
fileEntries: fileEntries.sort(pathCompare),
fileBytes: fileBytes
};
if (successCallback) {
successCallback(result);
}
}
}
function tallyEntry(entry) {
if (entry.isDirectory) {
dirEntries.push(entry);
recurseDirectory(entry);
} else {
fileEntries.push(entry);
pendingFiles++;
entry.file(function(file) {
fileBytes += file.size;
pendingFiles--;
areWeThereYet();
});
}
}
function recurseDirectory(dirEntry) {
pendingSubdirectories++;
util.forEachDirEntry(dirEntry, function(entry) {
if (entry == null) {
// Null entry indicates we're done scanning this directory.
pendingSubdirectories--;
areWeThereYet();
} else {
tallyEntry(entry);
}
});
}
for (var i = 0; i < entries.length; i++) {
tallyEntry(entries[i]);
}
areWeThereYet();
},
/**
* Utility function to invoke callback once for each entry in dirEntry.
*
* @param {DirectoryEntry} dirEntry The directory entry to enumerate.
* @param {function(Entry)} callback The function to invoke for each entry in
* dirEntry.
*/
forEachDirEntry: function(dirEntry, callback) {
var reader;
function onError(err) {
console.error('Failed to read dir entries at ' + dirEntry.fullPath);
}
function onReadSome(results) {
if (results.length == 0)
return callback(null);
for (var i = 0; i < results.length; i++)
callback(results[i]);
reader.readEntries(onReadSome, onError);
};
reader = dirEntry.createReader();
reader.readEntries(onReadSome, onError);
},
readDirectory: function(root, path, callback) {
function onError(e) {
callback([], e);
}
root.getDirectory(path, {create: false}, function(entry) {
var reader = entry.createReader();
var r = [];
function readNext() {
reader.readEntries(function(results) {
if (results.length == 0) {
callback(r, null);
return;
}
r.push.apply(r, results);
readNext();
}, onError);
}
readNext();
}, onError);
},
/**
* Utility function to resolve multiple directories with a single call.
*
* The successCallback will be invoked once for each directory object
* found. The errorCallback will be invoked once for each
* path that could not be resolved.
*
* The successCallback is invoked with a null entry when all paths have
* been processed.
*
* @param {DirEntry} dirEntry The base directory.
* @param {Object} params The parameters to pass to the underlying
* getDirectory calls.
* @param {Array<string>} paths The list of directories to resolve.
* @param {function(!DirEntry)} successCallback The function to invoke for
* each DirEntry found. Also invoked once with null at the end of the
* process.
* @param {function(FileError)} errorCallback The function to invoke
* for each path that cannot be resolved.
*/
getDirectories: function(dirEntry, params, paths, successCallback,
errorCallback) {
// Copy the params array, since we're going to destroy it.
params = [].slice.call(params);
function onComplete() {
successCallback(null);
}
function getNextDirectory() {
var path = paths.shift();
if (!path)
return onComplete();
dirEntry.getDirectory(
path, params,
function(entry) {
successCallback(entry);
getNextDirectory();
},
function(err) {
errorCallback(err);
getNextDirectory();
});
}
getNextDirectory();
},
/**
* Utility function to resolve multiple files with a single call.
*
* The successCallback will be invoked once for each directory object
* found. The errorCallback will be invoked once for each
* path that could not be resolved.
*
* The successCallback is invoked with a null entry when all paths have
* been processed.
*
* @param {DirEntry} dirEntry The base directory.
* @param {Object} params The parameters to pass to the underlying
* getFile calls.
* @param {Array<string>} paths The list of files to resolve.
* @param {function(!FileEntry)} successCallback The function to invoke for
* each FileEntry found. Also invoked once with null at the end of the
* process.
* @param {function(FileError)} errorCallback The function to invoke
* for each path that cannot be resolved.
*/
getFiles: function(dirEntry, params, paths, successCallback,
errorCallback) {
// Copy the params array, since we're going to destroy it.
params = [].slice.call(params);
function onComplete() {
successCallback(null);
}
function getNextFile() {
var path = paths.shift();
if (!path)
return onComplete();
dirEntry.getFile(
path, params,
function(entry) {
successCallback(entry);
getNextFile();
},
function(err) {
errorCallback(err);
getNextFile();
});
}
getNextFile();
},
/**
* Resolve a path to either a DirectoryEntry or a FileEntry, regardless of
* whether the path is a directory or file.
*
* @param root {DirectoryEntry} The root of the filesystem to search.
* @param path {string} The path to be resolved.
* @param resultCallback{function(Entry)} Called back when a path is
* successfully resolved. Entry will be either a DirectoryEntry or
* a FileEntry.
* @param errorCallback{function(FileError)} Called back if an unexpected
* error occurs while resolving the path.
*/
resolvePath: function(root, path, resultCallback, errorCallback) {
if (path == '' || path == '/') {
resultCallback(root);
return;
}
root.getFile(
path, {create: false},
resultCallback,
function (err) {
if (err.code == FileError.TYPE_MISMATCH_ERR) {
// Bah. It's a directory, ask again.
root.getDirectory(
path, {create: false},
resultCallback,
errorCallback);
} else {
errorCallback(err);
}
});
},
/**
* Locate the file referred to by path, creating directories or the file
* itself if necessary.
*/
getOrCreateFile: function(root, path, successCallback, errorCallback) {
var dirname = null;
var basename = null;
function onDirFound(dirEntry) {
dirEntry.getFile(basename, { create: true },
successCallback, errorCallback);
}
var i = path.lastIndexOf('/');
if (i > -1) {
dirname = path.substr(0, i);
basename = path.substr(i + 1);
} else {
basename = path;
}
if (!dirname)
return onDirFound(root);
util.getOrCreateDirectory(root, dirname, onDirFound, errorCallback);
},
/**
* Locate the directory referred to by path, creating directories along the
* way.
*/
getOrCreateDirectory: function(root, path, successCallback, errorCallback) {
var names = path.split('/');
function getOrCreateNextName(dir) {
if (!names.length)
return successCallback(dir);
var name;
do {
name = names.shift();
} while (!name || name == '.');
dir.getDirectory(name, { create: true }, getOrCreateNextName,
errorCallback);
}
getOrCreateNextName(root);
},
/**
* Remove a file or a directory.
*/
removeFileOrDirectory: function(entry, onSuccess, onError) {
if (entry.isDirectory)
entry.removeRecursively(onSuccess, onError);
else
entry.remove(onSuccess, onError);
},
/**
* Lookup tables used by bytesToSi.
*/
units_: ['B', 'KB', 'MB', 'GB', 'TB', 'PB'],
scale_: [1, 1e3, 1e6, 1e9, 1e12, 1e15],
/**
* Convert a number of bytes into an appropriate International System of
* Units (SI) representation, using the correct number separators.
*
* The first time this function is called it computes a lookup table which
* is cached for subsequent calls.
*
* @param {number} bytes The number of bytes.
*/
bytesToSi: function(bytes) {
function fmt(s, u) {
var rounded = Math.round(bytes / s * 10) / 10;
// TODO(rginda): Switch to v8Locale's number formatter when it's
// available.
return rounded.toLocaleString() + ' ' + u;
}
// This loop index is used outside the loop if it turns out |bytes|
// requires the largest unit.
var i;
for (i = 0; i < this.units_.length - 1; i++) {
if (bytes < this.scale_[i + 1])
return fmt(this.scale_[i], this.units_[i]);
}
return fmt(this.scale_[i], this.units_[i]);
},
/**
* Utility function to read specified range of bytes from file
* @param file {File} file to read
* @param begin {int} starting byte(included)
* @param end {int} last byte(excluded)
* @param callback {function(File, Uint8Array)} callback to invoke
* @param onError {function(err)} error handler
*/
readFileBytes: function(file, begin, end, callback, onError) {
var fileReader = new FileReader();
fileReader.onerror = onError;
fileReader.onloadend = function() {
callback(file, new ByteReader(fileReader.result))
};
fileReader.readAsArrayBuffer(file.webkitSlice(begin, end));
},
/**
* Write a blob to a file.
* Truncates the file first, so the previous content is fully overwritten.
* @param {FileEntry} entry
* @param {Blob} blob
* @param {Function} onSuccess completion callback
* @param {Function} onError error handler
*/
writeBlobToFile: function(entry, blob, onSuccess, onError) {
function truncate(writer) {
writer.onerror = onError;
writer.onwriteend = write.bind(null, writer);
writer.truncate(0);
}
function write(writer) {
writer.onwriteend = onSuccess;
writer.write(blob);
}
entry.createWriter(truncate, onError);
},
createElement: function (document, elementName, var_arg) { // children
var logger = console;
var element = document.createElement(elementName);
for (var i = 2; i < arguments.length; i++) {
var arg = arguments[i];
if (typeof(arg) == 'string') {
element.appendChild(document.createTextNode(arg));
} else {
element.appendChild(arg);
}
}
return element;
},
/**
* Returns a string '[Ctrl-][Alt-][Shift-][Meta-]' depending on the event
* modifiers. Convenient for writing out conditions in keyboard handlers.
*
* @param {Event} event
* @return {string}
*/
getKeyModifiers: function(event) {
return (event.ctrlKey ? 'Ctrl-' : '') +
(event.altKey ? 'Alt-' : '') +
(event.shiftKey ? 'Shift-' : '') +
(event.metaKey ? 'Meta-' : '');
}
};
|