This repository was archived by the owner on Apr 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathfiler.js
805 lines (705 loc) · 25.7 KB
/
filer.js
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
"use strict";
/*
Copyright 2011, 2012 - Eric Bidelman
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Author: Eric Bidelman (ebidel@gmail.com)
*/
var self = this; // window or worker context.
self.URL = self.URL || self.webkitURL;
self.requestFileSystem = self.requestFileSystem || self.webkitRequestFileSystem;
self.resolveLocalFileSystemURL = self.resolveLocalFileSystemURL ||
self.webkitResolveLocalFileSystemURL;
self.storageInfo = self.storageInfo || self.webkitStorageInfo;
self.BlobBuilder = self.BlobBuilder || self.MozBlobBuilder ||
self.WebKitBlobBuilder;
// Prevent errors in browsers that don't support FileError.
if (self.FileError === undefined) {
var FileError = function() {};
FileError.prototype.prototype = Error.prototype;
}
var Util = {
/**
* Turns a NodeList into an array.
*
* @param {NodeList} list The array-like object.
* @return {Array} The NodeList as an array.
*/
toArray: function(list) {
return Array.prototype.slice.call(list || [], 0);
},
/*toDataURL: function(contentType, uint8Array) {
return 'data:' + contentType + ';base64,' +
self.btoa(this.arrayToBinaryString(uint8Array));
},*/
/**
* Creates a data: URL from string data.
*
* @param {string} str The content to encode the data: URL from.
* @param {string} contentType The mimetype of the data str represents.
* @param {bool=} opt_isBinary Whether the string data is a binary string
* (and therefore should be base64 encoded). True by default.
* @return {string} The created data: URL.
*/
strToDataURL: function(str, contentType, opt_isBinary) {
var isBinary = opt_isBinary != undefined ? opt_isBinary : true;
if (isBinary) {
return 'data:' + contentType + ';base64,' + self.btoa(str);
} else {
return 'data:' + contentType + ',' + str;
}
},
/**
* Creates a blob: URL from a binary str.
*
* @param {string} binStr The content as a binary string.
* @param {string=} opt_contentType An optional mimetype of the data.
* @return {string} A new blob: URL.
*/
strToObjectURL: function(binStr, opt_contentType) {
var ui8a = new Uint8Array(binStr.length);
for (var i = 0; i < ui8a.length; ++i) {
ui8a[i] = binStr.charCodeAt(i);
}
var bb = new BlobBuilder();
bb.append(ui8a.buffer);
var blob = opt_contentType ? bb.getBlob(opt_contentType) : bb.getBlob();
return self.URL.createObjectURL(blob);
},
/**
* Creates a blob: URL from a File or Blob object.
*
* @param {Blob|File} blob The File or Blob data.
* @return {string} A new blob: URL.
*/
fileToObjectURL: function(blob) {
return self.URL.createObjectURL(blob);
},
/**
* Reads a File or Blob object and returns it as an ArrayBuffer.
*
* @param {Blob|File} blob The File or Blob data.
* @param {Function} callback Success callback passed the array buffer.
* @param {Function=} opt_error Optional error callback if the read fails.
*/
fileToArrayBuffer: function(blob, callback, opt_errorCallback) {
var reader = new FileReader();
reader.onload = function(e) {
callback(e.target.result);
};
reader.onerror = function(e) {
if (opt_errorCallback) {
opt_errorCallback(e);
}
};
reader.readAsArrayBuffer(blob);
},
/**
* Creates and returns a blob from a data URL (either base64 encoded or not).
*
* @param {string} dataURL The data URL to convert.
* @return {Blob} A blob representing the array buffer data.
*/
dataURLToBlob: function(dataURL) {
var BASE64_MARKER = ';base64,';
if (dataURL.indexOf(BASE64_MARKER) == -1) {
var parts = dataURL.split(',');
var contentType = parts[0].split(':')[1];
var raw = parts[1];
var bb = new BlobBuilder();
bb.append(raw);
return bb.getBlob(contentType);
}
var parts = dataURL.split(BASE64_MARKER);
var contentType = parts[0].split(':')[1];
var raw = window.atob(parts[1]);
var rawLength = raw.length;
var uInt8Array = new Uint8Array(rawLength);
for (var i = 0; i < rawLength; ++i) {
uInt8Array[i] = raw.charCodeAt(i);
}
var bb = new BlobBuilder();
bb.append(uInt8Array.buffer);
return bb.getBlob(contentType);
},
/**
* Reads an ArrayBuffer as returns its contents as a binary string.
*
* @param {ArrayBuffer} buffer The buffer of data.
* @param {string=} opt_contentType An optional mimetype of the data.
* @return {Blob} A blob representing the array buffer data.
*/
arrayBufferToBlob: function(buffer, opt_contentType) {
var bb = new BlobBuilder();
bb.append(buffer);
return opt_contentType ? bb.getBlob(opt_contentType) : bb.getBlob();
},
/**
* Reads an ArrayBuffer as returns its contents as a binary string.
*
* @param {ArrayBuffer} buffer The buffer of data.
* @param {Function} callback Success callback passed the binary string.
* @param {Function=} opt_error Optional error callback if the read fails.
*/
arrayBufferToBinaryString: function(buffer, callback, opt_errorCallback) {
var reader = new FileReader();
reader.onload = function(e) {
callback(e.target.result);
};
reader.onerror = function(e) {
if (opt_errorCallback) {
opt_errorCallback(e);
}
};
var bb = new BlobBuilder();
bb.append(buffer);
reader.readAsBinaryString(bb.getBlob());
},
/**
* Create a binary string out of an array of numbers (bytes), each varying
* from 0-255.
*
* @param {Array} bytes The array of numbers to transform into a binary str.
* @return {string} The byte array as a string.
*/
arrayToBinaryString: function(bytes) {
if (typeof bytes != typeof []) {
return null;
}
var i = bytes.length;
var bstr = new Array(i);
while (i--) {
bstr[i] = String.fromCharCode(bytes[i]);
}
return bstr.join('');
},
/**
* Returns the file extension for a given filename.
*
* @param {string} filename The filename.
* @return {string} The file's extension.
*/
getFileExtension: function(filename) {
var idx = filename.lastIndexOf('.');
return idx != -1 ? filename.substring(idx) : '';
}
};
var MyFileError = function(obj) {
this.prototype = FileError.prototype;
this.code = obj.code;
this.name = obj.name;
};
//MyFileError.prototype.__proto__ = FileError.prototype;
// Extend FileError with custom errors and a convenience method to get error
// code mnemonic.
FileError.BROWSER_NOT_SUPPORTED = 1000;
// TODO: remove when FileError.name is implemented (crbug.com/86014).
FileError.prototype.__defineGetter__('name', function() {
var keys = Object.keys(FileError);
for (var i = 0, key; key = keys[i]; ++i) {
if (FileError[key] == this.code) {
return key;
}
}
return 'Unknown Error';
});
var Filer = new function() {
var FS_INIT_ERROR_MSG = 'Filesystem has not been initialized.';
var NOT_IMPLEMENTED_MSG = 'Not implemented.';
var NOT_A_DIRECTORY = 'Path was not a directory.';
var INCORRECT_ARGS = 'These method arguments are not supported.';
var FS_URL_SCHEME = 'filesystem:';
var DEFAULT_FS_SIZE = 1024 * 1024; // 1MB.
var fs_ = null;
var cwd_ = null;
var isOpen_ = false;
var isFsURL_ = function(path) {
return path.indexOf(FS_URL_SCHEME) == 0;
};
// Path can be relative or absolute. If relative, it's taken from the cwd_.
// If a filesystem URL is passed it, it is simple returned
var pathToFsURL_ = function(path) {
if (!isFsURL_(path)) {
if (path[0] == '/') {
path = fs_.root.toURL() + path.substring(1);
} else if (path.indexOf('./') == 0 || path.indexOf('../') == 0) {
if (path == '../' && cwd_ != fs_.root) {
path = cwd_.toURL() + '/' + path;
} else {
path = cwd_.toURL() + path;
}
} else {
path = cwd_.toURL() + '/' + path;
}
}
return path;
};
/**
* Looks up a FileEntry or DirectoryEntry for a given path.
*
* @param {function(...FileEntry|DirectorEntry)} callback A callback to be
* passed the entry/entries that were fetched. The ordering of the
* entries passed to the callback correspond to the same order passed
* to this method.
* @param {...string} var_args 1-2 paths to lookup and return entries for.
* These can be paths or filesystem: URLs.
*/
var getEntry_ = function(callback, var_args) {
var srcStr = arguments[1];
var destStr = arguments[2];
var onError = function(e) {
if (e.code == FileError.NOT_FOUND_ERR) {
if (destStr) {
throw new Error('"' + srcStr + '" or "' + destStr +
'" does not exist.');
} else {
throw new Error('"' + srcStr + '" does not exist.');
}
} else {
throw new Error('Problem getting Entry for one or more paths.');
}
};
// Build a filesystem: URL manually if we need to.
var src = pathToFsURL_(srcStr);
if (arguments.length == 3) {
var dest = pathToFsURL_(destStr);
self.resolveLocalFileSystemURL(src, function(srcEntry) {
self.resolveLocalFileSystemURL(dest, function(destEntry) {
callback(srcEntry, destEntry);
}, onError);
}, onError);
} else {
self.resolveLocalFileSystemURL(src, callback, onError);
}
};
/**
* Copy or moves a file or directory to a destination.
*
* See public method's description (Filer.cp()) for rest of params.
* @param {Boolean=} opt_deleteOrig True if the original entry should be
* deleted after the copy takes place, essentially making the operation
* a move instead of a copy. Defaults to false.
*/
var copyOrMove_ = function(src, dest, opt_newName, opt_successCallback,
opt_errorHandler, opt_deleteOrig) {
var self = this;
if (!fs_) {
throw new Error(FS_INIT_ERROR_MSG);
}
if (typeof src != typeof dest) {
throw new Error(INCORRECT_ARGS);
}
var newName = opt_newName || null;
var deleteOrig = opt_deleteOrig != undefined ? opt_deleteOrig : false;
if ((src.isFile || dest.isDirectory) && dest.isDirectory) {
if (deleteOrig) {
src.moveTo(dest, newName, opt_successCallback, opt_errorHandler);
} else {
src.copyTo(dest, newName, opt_successCallback, opt_errorHandler);
}
} else {
getEntry_(function(srcEntry, destDir) {
if (!destDir.isDirectory) {
var e = new Error('Oops! "' + destDir.name + ' is not a directory!');
if (opt_errorHandler) {
opt_errorHandler(e);
} else {
throw e;
}
return;
}
if (deleteOrig) {
srcEntry.moveTo(destDir, newName, opt_successCallback, opt_errorHandler);
} else {
srcEntry.copyTo(destDir, newName, opt_successCallback, opt_errorHandler);
}
}, src, dest);
}
};
function Filer(fs) {
fs_ = fs || null;
if (fs_) {
cwd_ = fs_.root;
isOpen_ = true; // TODO: this may not be the case.
}
}
Filer.DEFAULT_FS_SIZE = DEFAULT_FS_SIZE;
Filer.prototype = {
get fs() {
return fs_;
},
get isOpen() {
return isOpen_;
}
};
/**
* Constructs and returns a filesystem: URL given a path.
*
* @param {string=} path The path to construct a URL for.
* size {int=} The storage size (in bytes) to open the filesystem with.
* Defaults to DEFAULT_FS_SIZE.
* @return {string} The filesystem: URL.
*/
Filer.prototype.pathToFilesystemURL = function(path) {
return pathToFsURL_(path);
};
/**
* Initializes (opens) the file system.
*
* @param {object=} opt_initObj Optional object literal with the following
* properties. Note: If {} or null is passed, default values are used.
* persistent {Boolean=} Whether the browser should use persistent quota.
* Default is false.
* size {int=} The storage size (in bytes) to open the filesystem with.
* Defaults to DEFAULT_FS_SIZE.
* @param {Function=} opt_successCallback Optional success handler passed a
* DOMFileSystem object.
* @param {Function=} opt_errorHandler Optional error callback.
*/
Filer.prototype.init = function(opt_initObj, opt_successCallback,
opt_errorHandler) {
if (!self.requestFileSystem) {
throw new MyFileError({
code: FileError.BROWSER_NOT_SUPPORTED,
name: 'BROWSER_NOT_SUPPORTED'
});
}
var initObj = opt_initObj ? opt_initObj : {}; // Use defaults if obj is null.
var size = initObj.size || DEFAULT_FS_SIZE;
this.type = self.TEMPORARY;
if ('persistent' in initObj && initObj.persistent) {
this.type = self.PERSISTENT;
}
var init = function(fs) {
this.size = size;
fs_ = fs;
cwd_ = fs_.root;
isOpen_ = true;
opt_successCallback && opt_successCallback(fs);
};
if (this.type == self.PERSISTENT) {
self.webkitStorageInfo.requestQuota(this.type, size, function(grantedBytes) {
self.requestFileSystem(this.type, grantedBytes, init.bind(this), opt_errorHandler);
}.bind(this), opt_errorHandler);
} else {
self.requestFileSystem(this.type, size, init.bind(this), opt_errorHandler);
}
};
/**
* Reads the contents of a directory.
*
* @param {string|DirectoryEntry} dirEntryOrPath A path relative to the
* current working directory. In most cases that is the root entry, unless
* cd() has been called. A DirectoryEntry or filesystem URL can also be
* passed, in which case, the folder's contents will be returned.
* @param {Function} successCallback Success handler passed an Array<Entry>.
* @param {Function=} opt_errorHandler Optional error callback.
*/
Filer.prototype.ls = function(dirEntryOrPath, successCallback,
opt_errorHandler) {
if (!fs_) {
throw new Error(FS_INIT_ERROR_MSG);
}
var callback = function(dirEntry) {
cwd_ = dirEntry;
// Read contents of current working directory. According to spec, need to
// keep calling readEntries() until length of result array is 0. We're
// guarenteed the same entry won't be returned again.
var entries_ = [];
var reader = cwd_.createReader();
var readEntries = function() {
reader.readEntries(function(results) {
if (!results.length) {
// By default, sort the list by name.
entries_.sort(function(a, b) {
return a.name < b.name ? -1 : b.name < a.name ? 1 : 0;
});
successCallback(entries_);
} else {
entries_ = entries_.concat(Util.toArray(results));
readEntries();
}
}, opt_errorHandler);
};
readEntries();
};
if (dirEntryOrPath.isDirectory) { // passed a DirectoryEntry.
callback(dirEntryOrPath);
} else if (isFsURL_(dirEntryOrPath)) { // passed a filesystem URL.
getEntry_(callback, pathToFsURL_(dirEntryOrPath));
} else { // Passed a path. Look up DirectoryEntry and proceeed.
// TODO: Find way to use getEntry_(callback, dirEntryOrPath); with cwd_.
cwd_.getDirectory(dirEntryOrPath, {}, callback, opt_errorHandler);
}
};
/**
* Creates a new directory.
*
* @param {string} path The name of the directory to create. If a path is
* given, each intermediate dir is created (e.g. similar to mkdir -p).
* @param {bool=} opt_exclusive True if an error should be thrown if
* one or more of the directories already exists. False by default.
* @param {Function} successCallback Success handler passed the
* DirectoryEntry that was created. If we were passed a path, the last
* directory that was created is passed back.
* @param {Function=} opt_errorHandler Optional error callback.
*/
Filer.prototype.mkdir = function(path, opt_exclusive, successCallback,
opt_errorHandler) {
if (!fs_) {
throw new Error(FS_INIT_ERROR_MSG);
}
var exclusive = opt_exclusive != null ? opt_exclusive : false;
var folderParts = path.split('/');
var createDir = function(rootDir, folders) {
// Throw out './' or '/' and move on. Prevents: '/foo/.//bar'.
if (folders[0] == '.' || folders[0] == '') {
folders = folders.slice(1);
}
rootDir.getDirectory(folders[0], {create: true, exclusive: exclusive},
function (dirEntry) {
if (dirEntry.isDirectory) { // TODO: check shouldn't be necessary.
// Recursively add the new subfolder if we have more to create and
// There was more than one folder to create.
if (folders.length && folderParts.length != 1) {
createDir(dirEntry, folders.slice(1));
} else {
// Return the last directory that was created.
successCallback(dirEntry);
}
} else {
var e = new Error(path + ' is not a directory');
if (opt_errorHandler) {
opt_errorHandler(e);
} else {
throw e;
}
}
},
function(e) {
if (e.code == FileError.INVALID_MODIFICATION_ERR) {
e.message = "'" + path + "' already exists";
if (opt_errorHandler) {
opt_errorHandler(e);
} else {
throw e;
}
}
}
);
};
createDir(cwd_, folderParts);
};
/**
* Looks up and return a File for a given file entry.
*
* @param {string|FileEntry} entryOrPath A path, filesystem URL, or FileEntry
* of the file to lookup.
* @param {Function} successCallback Success callback passed the File object.
* @param {Function=} opt_errorHandler Optional error callback.
*/
Filer.prototype.open = function(entryOrPath, successCallback, opt_errorHandler) {
if (!fs_) {
throw new Error(FS_INIT_ERROR_MSG);
}
if (entryOrPath.isFile) {
entryOrPath.file(successCallback, opt_errorHandler);
} else {
getEntry_(function(fileEntry) {
fileEntry.file(successCallback, opt_errorHandler);
}, pathToFsURL_(entryOrPath));
}
};
/**
* Creates an empty file.
*
* @param {string} path The relative path of the file to create, from the
* current working directory.
* @param {bool=} opt_exclusive True (default) if an error should be thrown if
* the file already exists.
* @param {Function} successCallback A success callback, which is passed
* the new FileEntry.
* @param {Function=} opt_errorHandler Optional error callback.
*/
Filer.prototype.create = function(path, opt_exclusive, successCallback,
opt_errorHandler) {
if (!fs_) {
throw new Error(FS_INIT_ERROR_MSG);
}
var exclusive = opt_exclusive != null ? opt_exclusive : true;
cwd_.getFile(path, {create: true, exclusive: exclusive}, successCallback,
function(e) {
if (e.code == FileError.INVALID_MODIFICATION_ERR) {
e.message = "'" + path + "' already exists";
}
if (opt_errorHandler) {
opt_errorHandler(e);
} else {
throw e;
}
}
);
};
/**
* Moves a file or directory.
*
* @param {string|FileEntry|DirectoryEntry} src The file/directory
* to move. If src is a string, a path or filesystem: URL is accepted.
* @param {string|DirectoryEntry} dest The directory to move the src into.
* If dest is a string, a path or filesystem: URL is accepted.
* Note: dest needs to be the same type as src.
* @param {string=} opt_newName An optional new name for the moved entry.
* @param {Function=} opt_successCallback Optional callback passed the moved
* entry on a successful move.
* @param {Function=} opt_errorHandler Optional error callback.
*/
Filer.prototype.mv = function(src, dest, opt_newName, opt_successCallback,
opt_errorHandler) {
copyOrMove_.bind(this, src, dest, opt_newName, opt_successCallback,
opt_errorHandler, true)();
};
/**
* Deletes a file or directory entry.
*
* @param {string|FileEntry|DirectoryEntry} entryOrPath The file or directory
* to remove. If entry is a DirectoryEntry, its contents are removed
* recursively. If entryOrPath is a string, a path or filesystem: URL is
* accepted.
* @param {Function} successCallback Zero arg callback invoked on
* successful removal.
* @param {Function=} opt_errorHandler Optional error callback.
*/
Filer.prototype.rm = function(entryOrPath, successCallback,
opt_errorHandler) {
if (!fs_) {
throw new Error(FS_INIT_ERROR_MSG);
}
var removeIt = function(entry) {
if (entry.isFile) {
entry.remove(successCallback, opt_errorHandler);
} else if (entry.isDirectory) {
entry.removeRecursively(successCallback, opt_errorHandler);
}
};
if (entryOrPath.isFile || entryOrPath.isDirectory) {
removeIt(entryOrPath);
} else {
getEntry_(removeIt, entryOrPath);
}
};
/**
* Changes the current working directory.
*
* @param {string|DirectoryEntry} dirEntryOrPath A DirectoryEntry to move into
* or a path relative to the current working directory. A filesystem: URL
* is also accepted
* @param {Function=} opt_successCallback Optional success callback, which is
* passed the DirectoryEntry of the new current directory.
* @param {Function=} opt_errorHandler Optional error callback.
*/
Filer.prototype.cd = function(dirEntryOrPath, opt_successCallback,
opt_errorHandler) {
if (!fs_) {
throw new Error(FS_INIT_ERROR_MSG);
}
if (dirEntryOrPath.isDirectory) {
cwd_ = dirEntryOrPath;
opt_successCallback && opt_successCallback(cwd_);
} else {
// Build a filesystem: URL manually if we need to.
var dirEntryOrPath = pathToFsURL_(dirEntryOrPath);
getEntry_(function(dirEntry) {
if (dirEntry.isDirectory) {
cwd_ = dirEntry;
opt_successCallback && opt_successCallback(cwd_);
} else {
var e = new Error(NOT_A_DIRECTORY);
if (opt_errorHandler) {
opt_errorHandler(e);
} else {
throw e;
}
}
}, dirEntryOrPath);
}
};
/**
* Copies a file or directory to a destination.
*
* @param {string|FileEntry|DirectoryEntry} src The file/directory
* to copy. If src is a string, a path or filesystem: URL is accepted.
* @param {string|DirectoryEntry} dest The directory to copy the src into.
* If dest is a string, a path or filesystem: URL is accepted.
* Note: dest needs to be the same type as src.
* @param {string=} opt_newName An optional name for the copied entry.
* @param {Function=} opt_successCallback Optional callback passed the moved
* entry on a successful copy.
* @param {Function=} opt_errorHandler Optional error callback.
*/
Filer.prototype.cp = function(src, dest, opt_newName, opt_successCallback,
opt_errorHandler) {
copyOrMove_.bind(this, src, dest, opt_newName, opt_successCallback,
opt_errorHandler)();
};
/**
* Writes data to a file.
*
* If the file already exists, its contents are overwritten.
*
* @param {string|FileEntry} entryOrPath A path, filesystem URL, or FileEntry
* of the file to lookup.
* @param {object} dataObj The data to write. Example:
* {data: string|Blob|File|ArrayBuffer, type: mimetype, append: true}
* If append is specified, data is appended to the end of the file.
* @param {Function} successCallback Success callback, which is passed
* the created FileEntry and FileWriter object used to write the data.
* @param {Function=} opt_errorHandler Optional error callback.
*/
Filer.prototype.write = function(entryOrPath, dataObj, successCallback,
opt_errorHandler) {
if (!fs_) {
throw new Error(FS_INIT_ERROR_MSG);
}
var writeFile_ = function(fileEntry) {
fileEntry.createWriter(function(fileWriter) {
fileWriter.onerror = opt_errorHandler;
if (dataObj.append) {
fileWriter.onwriteend = function(e) {
successCallback(fileEntry, this);
};
fileWriter.seek(fileWriter.length); // Start write position at EOF.
} else {
var truncated = false;
fileWriter.onwriteend = function(e) {
// Truncate file to newly written file size.
if (!truncated) {
truncated = true;
this.truncate(this.position);
return;
}
successCallback(fileEntry, this);
};
}
var bb = new BlobBuilder();
bb.append(dataObj.data);
fileWriter.write(dataObj.type ? bb.getBlob(dataObj.type) : bb.getBlob());
}, opt_errorHandler);
};
if (entryOrPath.isFile) {
writeFile_(entryOrPath);
} else if (isFsURL_(entryOrPath)) {
getEntry_(writeFile_, entryOrPath);
} else {
cwd_.getFile(entryOrPath, {create: true, exclusive: false}, writeFile_,
opt_errorHandler);
}
};
return Filer;
};