UNPKG

32.9 kBJavaScriptView Raw
1"use strict";
2var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5 else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6 return c > 3 && r && Object.defineProperty(target, key, r), r;
7};
8var plugin_1 = require('./plugin');
9/**
10 * @name File
11 * @description
12 * This plugin implements a File API allowing read/write access to files residing on the device.
13 *
14 * The File class implements static convenience functions to access files and directories.
15 *
16 * Example:
17 * ```
18 * import { File } from 'ionic-native';
19 *
20 * const dataDirectory: string = File.dataDirectory;
21 *
22 * File.checkDir(dataDirectory, 'mydir').then(_ => console.log('yay')).catch(err => console.log('boooh'));
23 * ```
24 *
25 * This plugin is based on several specs, including : The HTML5 File API http://www.w3.org/TR/FileAPI/
26 * The (now-defunct) Directories and System extensions Latest: http://www.w3.org/TR/2012/WD-file-system-api-20120417/
27 * Although most of the plugin code was written when an earlier spec was current: http://www.w3.org/TR/2011/WD-file-system-api-20110419/
28 * It also implements the FileWriter spec : http://dev.w3.org/2009/dap/file-system/file-writer.html
29 */
30var File = (function () {
31 function File() {
32 }
33 /**
34 * Get free disk space in Bytes
35 * @returns {Promise<number>} Returns a promise that resolves with the remaining free disk space in Bytes
36 */
37 File.getFreeDiskSpace = function () {
38 return new Promise(function (resolve, reject) {
39 if (!cordova || !cordova.exec) {
40 plugin_1.pluginWarn({
41 pluginName: 'File',
42 plugin: 'cordova-plugin-file'
43 });
44 reject({ error: 'plugin_not_installed' });
45 }
46 else {
47 cordova.exec(resolve, reject, 'File', 'getFreeDiskSpace', []);
48 }
49 });
50 };
51 /**
52 * Check if a directory exists in a certain path, directory.
53 *
54 * @param {string} path Base FileSystem. Please refer to the iOS and Android filesystems above
55 * @param {string} dir Name of directory to check
56 * @returns {Promise<boolean>} Returns a Promise that resolves to true if the directory exists or rejects with an error.
57 */
58 File.checkDir = function (path, dir) {
59 if ((/^\//.test(dir))) {
60 var err = new exports.FileError(5);
61 err.message = 'directory cannot start with \/';
62 return Promise.reject(err);
63 }
64 var fullpath = path + dir;
65 return File.resolveDirectoryUrl(fullpath)
66 .then(function () {
67 return true;
68 });
69 };
70 /**
71 * Creates a new directory in the specific path.
72 * The replace boolean value determines whether to replace an existing directory with the same name.
73 * If an existing directory exists and the replace value is false, the promise will fail and return an error.
74 *
75 * @param {string} path Base FileSystem. Please refer to the iOS and Android filesystems above
76 * @param {string} dirName Name of directory to create
77 * @param {boolean} replace If true, replaces file with same name. If false returns error
78 * @returns {Promise<DirectoryEntry>} Returns a Promise that resolves with a DirectoryEntry or rejects with an error.
79 */
80 File.createDir = function (path, dirName, replace) {
81 if ((/^\//.test(dirName))) {
82 var err = new exports.FileError(5);
83 err.message = 'directory cannot start with \/';
84 return Promise.reject(err);
85 }
86 var options = {
87 create: true
88 };
89 if (!replace) {
90 options.exclusive = true;
91 }
92 return File.resolveDirectoryUrl(path)
93 .then(function (fse) {
94 return File.getDirectory(fse, dirName, options);
95 });
96 };
97 /**
98 * Remove a directory at a given path.
99 *
100 * @param {string} path The path to the directory
101 * @param {string} dirName The directory name
102 * @returns {Promise<RemoveResult>} Returns a Promise that resolves to a RemoveResult or rejects with an error.
103 */
104 File.removeDir = function (path, dirName) {
105 if ((/^\//.test(dirName))) {
106 var err = new exports.FileError(5);
107 err.message = 'directory cannot start with \/';
108 return Promise.reject(err);
109 }
110 return File.resolveDirectoryUrl(path)
111 .then(function (fse) {
112 return File.getDirectory(fse, dirName, { create: false });
113 })
114 .then(function (de) {
115 return File.remove(de);
116 });
117 };
118 /**
119 * Move a directory to a given path.
120 *
121 * @param {string} path The source path to the directory
122 * @param {string} dirName The source directory name
123 * @param {string} newPath The destionation path to the directory
124 * @param {string} newDirName The destination directory name
125 * @returns {Promise<DirectoryEntry|Entry>} Returns a Promise that resolves to the new DirectoryEntry object or rejects with an error.
126 */
127 File.moveDir = function (path, dirName, newPath, newDirName) {
128 var _this = this;
129 newDirName = newDirName || dirName;
130 if ((/^\//.test(newDirName))) {
131 var err = new exports.FileError(5);
132 err.message = 'directory cannot start with \/';
133 return Promise.reject(err);
134 }
135 return this.resolveDirectoryUrl(path)
136 .then(function (fse) {
137 return _this.getDirectory(fse, dirName, { create: false });
138 })
139 .then(function (srcde) {
140 return _this.resolveDirectoryUrl(newPath)
141 .then(function (deste) {
142 return File.move(srcde, deste, newDirName);
143 });
144 });
145 };
146 /**
147 * Copy a directory in various methods. If destination directory exists, will fail to copy.
148 *
149 * @param {string} path Base FileSystem. Please refer to the iOS and Android filesystems above
150 * @param {string} dirName Name of directory to copy
151 * @param {string} newPath Base FileSystem of new location
152 * @param {string} newDirName New name of directory to copy to (leave blank to remain the same)
153 * @returns {Promise<Entry>} Returns a Promise that resolves to the new Entry object or rejects with an error.
154 */
155 File.copyDir = function (path, dirName, newPath, newDirName) {
156 var _this = this;
157 if ((/^\//.test(newDirName))) {
158 var err = new exports.FileError(5);
159 err.message = 'directory cannot start with \/';
160 return Promise.reject(err);
161 }
162 return this.resolveDirectoryUrl(path)
163 .then(function (fse) {
164 return _this.getDirectory(fse, dirName, { create: false });
165 })
166 .then(function (srcde) {
167 return _this.resolveDirectoryUrl(newPath)
168 .then(function (deste) {
169 return File.copy(srcde, deste, newDirName);
170 });
171 });
172 };
173 /**
174 * List files and directory from a given path.
175 *
176 * @param {string} path Base FileSystem. Please refer to the iOS and Android filesystems above
177 * @param {string} dirName Name of directory
178 * @returns {Promise<Entry[]>} Returns a Promise that resolves to an array of Entry objects or rejects with an error.
179 */
180 File.listDir = function (path, dirName) {
181 if ((/^\//.test(dirName))) {
182 var err = new exports.FileError(5);
183 err.message = 'directory cannot start with \/';
184 return Promise.reject(err);
185 }
186 return File.resolveDirectoryUrl(path)
187 .then(function (fse) {
188 return File.getDirectory(fse, dirName, { create: false, exclusive: false });
189 })
190 .then(function (de) {
191 var reader = de.createReader();
192 return File.readEntries(reader);
193 });
194 };
195 /**
196 * Removes all files and the directory from a desired location.
197 *
198 * @param {string} path Base FileSystem. Please refer to the iOS and Android filesystems above
199 * @param {string} dirName Name of directory
200 * @returns {Promise<RemoveResult>} Returns a Promise that resolves with a RemoveResult or rejects with an error.
201 */
202 File.removeRecursively = function (path, dirName) {
203 if ((/^\//.test(dirName))) {
204 var err = new exports.FileError(5);
205 err.message = 'directory cannot start with \/';
206 return Promise.reject(err);
207 }
208 return File.resolveDirectoryUrl(path)
209 .then(function (fse) {
210 return File.getDirectory(fse, dirName, { create: false });
211 })
212 .then(function (de) {
213 return File.rimraf(de);
214 });
215 };
216 /**
217 * Check if a file exists in a certain path, directory.
218 *
219 * @param {string} path Base FileSystem. Please refer to the iOS and Android filesystems above
220 * @param {string} file Name of file to check
221 * @returns {Promise<boolean>} Returns a Promise that resolves with a boolean or rejects with an error.
222 */
223 File.checkFile = function (path, file) {
224 if ((/^\//.test(file))) {
225 var err = new exports.FileError(5);
226 err.message = 'file cannot start with \/';
227 return Promise.reject(err);
228 }
229 return File.resolveLocalFilesystemUrl(path + file)
230 .then(function (fse) {
231 if (fse.isFile) {
232 return true;
233 }
234 else {
235 var err = new exports.FileError(13);
236 err.message = 'input is not a file';
237 return Promise.reject(err);
238 }
239 });
240 };
241 /**
242 * Creates a new file in the specific path.
243 * The replace boolean value determines whether to replace an existing file with the same name.
244 * If an existing file exists and the replace value is false, the promise will fail and return an error.
245 *
246 * @param {string} path Base FileSystem. Please refer to the iOS and Android filesystems above
247 * @param {string} fileName Name of file to create
248 * @param {boolean} replace If true, replaces file with same name. If false returns error
249 * @returns {Promise<FileEntry>} Returns a Promise that resolves to a FileEntry or rejects with an error.
250 */
251 File.createFile = function (path, fileName, replace) {
252 if ((/^\//.test(fileName))) {
253 var err = new exports.FileError(5);
254 err.message = 'file-name cannot start with \/';
255 return Promise.reject(err);
256 }
257 var options = {
258 create: true
259 };
260 if (!replace) {
261 options.exclusive = true;
262 }
263 return File.resolveDirectoryUrl(path)
264 .then(function (fse) {
265 return File.getFile(fse, fileName, options);
266 });
267 };
268 /**
269 * Removes a file from a desired location.
270 *
271 * @param {string} path Base FileSystem. Please refer to the iOS and Android filesystems above
272 * @param {string} fileName Name of file to remove
273 * @returns {Promise<RemoveResult>} Returns a Promise that resolves to a RemoveResult or rejects with an error.
274 */
275 File.removeFile = function (path, fileName) {
276 if ((/^\//.test(fileName))) {
277 var err = new exports.FileError(5);
278 err.message = 'file-name cannot start with \/';
279 return Promise.reject(err);
280 }
281 return File.resolveDirectoryUrl(path)
282 .then(function (fse) {
283 return File.getFile(fse, fileName, { create: false });
284 })
285 .then(function (fe) {
286 return File.remove(fe);
287 });
288 };
289 /** Write a new file to the desired location.
290 *
291 * @param {string} path Base FileSystem. Please refer to the iOS and Android filesystems above
292 * @param {string} fileName path relative to base path
293 * @param {string | Blob} text content or blob to write
294 * @param {WriteOptions} options replace file if set to true. See WriteOptions for more information.
295 * @returns {Promise<any>} Returns a Promise that resolves to updated file entry or rejects with an error.
296 */
297 File.writeFile = function (path, fileName, text, options) {
298 if (options === void 0) { options = {}; }
299 if ((/^\//.test(fileName))) {
300 var err = new exports.FileError(5);
301 err.message = 'file-name cannot start with \/';
302 return Promise.reject(err);
303 }
304 var getFileOpts = {
305 create: !options.append,
306 exclusive: !options.replace
307 };
308 return File.resolveDirectoryUrl(path)
309 .then(function (fse) {
310 return File.getFile(fse, fileName, getFileOpts);
311 })
312 .then(function (fe) {
313 return File.writeFileEntry(fe, text, options);
314 });
315 };
316 /** Write content to FileEntry.
317 *
318 * @private
319 * @param {FileEntry} fe file entry object
320 * @param {string | Blob} text content or blob to write
321 * @param {WriteOptions} options replace file if set to true. See WriteOptions for more information.
322 * @returns {Promise<FileEntry>} Returns a Promise that resolves to updated file entry or rejects with an error.
323 */
324 File.writeFileEntry = function (fe, text, options) {
325 return File.createWriter(fe)
326 .then(function (writer) {
327 if (options.append) {
328 writer.seek(writer.length);
329 }
330 if (options.truncate) {
331 writer.truncate(options.truncate);
332 }
333 return File.write(writer, text);
334 })
335 .then(function () { return fe; });
336 };
337 /** Write to an existing file.
338 *
339 * @param {string} path Base FileSystem. Please refer to the iOS and Android filesystems above
340 * @param {string} fileName path relative to base path
341 * @param {string | Blob} text content or blob to write
342 * @returns {Promise<void>} Returns a Promise that resolves or rejects with an error.
343 */
344 File.writeExistingFile = function (path, fileName, text) {
345 return File.writeFile(path, fileName, text, { replace: true });
346 };
347 /**
348 * Read the contents of a file as text.
349 *
350 * @param {string} path Base FileSystem. Please refer to the iOS and Android filesystems above
351 * @param {string} file Name of file, relative to path.
352 * @returns {Promise<string>} Returns a Promise that resolves with the contents of the file as string or rejects with an error.
353 */
354 File.readAsText = function (path, file) {
355 if ((/^\//.test(file))) {
356 var err = new exports.FileError(5);
357 err.message = 'file-name cannot start with \/';
358 return Promise.reject(err);
359 }
360 return File.resolveDirectoryUrl(path)
361 .then(function (fse) {
362 return File.getFile(fse, file, { create: false });
363 })
364 .then(function (fe) {
365 var reader = new exports.FileReader();
366 return new Promise(function (resolve, reject) {
367 reader.onloadend = function () {
368 if (reader.result !== undefined || reader.result !== null) {
369 resolve(reader.result);
370 }
371 else if (reader.error !== undefined || reader.error !== null) {
372 reject(reader.error);
373 }
374 else {
375 reject({ code: null, message: 'READER_ONLOADEND_ERR' });
376 }
377 };
378 fe.file(function (file) {
379 reader.readAsText(file);
380 }, function (error) {
381 reject(error);
382 });
383 });
384 });
385 };
386 /**
387 * Read file and return data as a base64 encoded data url.
388 * A data url is of the form:
389 * data:[<mediatype>][;base64],<data>
390
391 * @param {string} path Base FileSystem. Please refer to the iOS and Android filesystems above
392 * @param {string} file Name of file, relative to path.
393 * @returns {Promise<string>} Returns a Promise that resolves with the contents of the file as data URL or rejects with an error.
394 */
395 File.readAsDataURL = function (path, file) {
396 if ((/^\//.test(file))) {
397 var err = new exports.FileError(5);
398 err.message = 'file-name cannot start with \/';
399 return Promise.reject(err);
400 }
401 return File.resolveDirectoryUrl(path)
402 .then(function (fse) {
403 return File.getFile(fse, file, { create: false });
404 })
405 .then(function (fe) {
406 var reader = new exports.FileReader();
407 return new Promise(function (resolve, reject) {
408 reader.onloadend = function () {
409 if (reader.result !== undefined || reader.result !== null) {
410 resolve(reader.result);
411 }
412 else if (reader.error !== undefined || reader.error !== null) {
413 reject(reader.error);
414 }
415 else {
416 reject({ code: null, message: 'READER_ONLOADEND_ERR' });
417 }
418 };
419 fe.file(function (file) {
420 reader.readAsDataURL(file);
421 }, function (error) {
422 reject(error);
423 });
424 });
425 });
426 };
427 /**
428 * Read file and return data as a binary data.
429
430 * @param {string} path Base FileSystem. Please refer to the iOS and Android filesystems above
431 * @param {string} file Name of file, relative to path.
432 * @returns {Promise<string>} Returns a Promise that resolves with the contents of the file as string rejects with an error.
433 */
434 File.readAsBinaryString = function (path, file) {
435 if ((/^\//.test(file))) {
436 var err = new exports.FileError(5);
437 err.message = 'file-name cannot start with \/';
438 return Promise.reject(err);
439 }
440 return File.resolveDirectoryUrl(path)
441 .then(function (fse) {
442 return File.getFile(fse, file, { create: false });
443 })
444 .then(function (fe) {
445 var reader = new exports.FileReader();
446 return new Promise(function (resolve, reject) {
447 reader.onloadend = function () {
448 if (reader.result !== undefined || reader.result !== null) {
449 resolve(reader.result);
450 }
451 else if (reader.error !== undefined || reader.error !== null) {
452 reject(reader.error);
453 }
454 else {
455 reject({ code: null, message: 'READER_ONLOADEND_ERR' });
456 }
457 };
458 fe.file(function (file) {
459 reader.readAsBinaryString(file);
460 }, function (error) {
461 reject(error);
462 });
463 });
464 });
465 };
466 /**
467 * Read file and return data as an ArrayBuffer.
468
469 * @param {string} path Base FileSystem. Please refer to the iOS and Android filesystems above
470 * @param {string} file Name of file, relative to path.
471 * @returns {Promise<ArrayBuffer>} Returns a Promise that resolves with the contents of the file as ArrayBuffer or rejects with an error.
472 */
473 File.readAsArrayBuffer = function (path, file) {
474 if ((/^\//.test(file))) {
475 var err = new exports.FileError(5);
476 err.message = 'file-name cannot start with \/';
477 return Promise.reject(err);
478 }
479 return File.resolveDirectoryUrl(path)
480 .then(function (fse) {
481 return File.getFile(fse, file, { create: false });
482 })
483 .then(function (fe) {
484 var reader = new exports.FileReader();
485 return new Promise(function (resolve, reject) {
486 reader.onloadend = function () {
487 if (reader.result !== undefined || reader.result !== null) {
488 resolve(reader.result);
489 }
490 else if (reader.error !== undefined || reader.error !== null) {
491 reject(reader.error);
492 }
493 else {
494 reject({ code: null, message: 'READER_ONLOADEND_ERR' });
495 }
496 };
497 fe.file(function (file) {
498 reader.readAsArrayBuffer(file);
499 }, function (error) {
500 reject(error);
501 });
502 });
503 });
504 };
505 /**
506 * Move a file to a given path.
507 *
508 * @param {string} path Base FileSystem. Please refer to the iOS and Android filesystems above
509 * @param {string} fileName Name of file to move
510 * @param {string} newPath Base FileSystem of new location
511 * @param {string} newFileName New name of file to move to (leave blank to remain the same)
512 * @returns {Promise<Entry>} Returns a Promise that resolves to the new Entry or rejects with an error.
513 */
514 File.moveFile = function (path, fileName, newPath, newFileName) {
515 var _this = this;
516 newFileName = newFileName || fileName;
517 if ((/^\//.test(newFileName))) {
518 var err = new exports.FileError(5);
519 err.message = 'file name cannot start with \/';
520 return Promise.reject(err);
521 }
522 return this.resolveDirectoryUrl(path)
523 .then(function (fse) {
524 return _this.getFile(fse, fileName, { create: false });
525 })
526 .then(function (srcfe) {
527 return _this.resolveDirectoryUrl(newPath)
528 .then(function (deste) {
529 return File.move(srcfe, deste, newFileName);
530 });
531 });
532 };
533 /**
534 * Copy a file in various methods. If file exists, will fail to copy.
535 *
536 * @param {string} path Base FileSystem. Please refer to the iOS and Android filesystems above
537 * @param {string} fileName Name of file to copy
538 * @param {string} newPath Base FileSystem of new location
539 * @param {string} newFileName New name of file to copy to (leave blank to remain the same)
540 * @returns {Promise<Entry>} Returns a Promise that resolves to an Entry or rejects with an error.
541 */
542 File.copyFile = function (path, fileName, newPath, newFileName) {
543 var _this = this;
544 newFileName = newFileName || fileName;
545 if ((/^\//.test(newFileName))) {
546 var err = new exports.FileError(5);
547 err.message = 'file name cannot start with \/';
548 return Promise.reject(err);
549 }
550 return this.resolveDirectoryUrl(path)
551 .then(function (fse) {
552 return _this.getFile(fse, fileName, { create: false });
553 })
554 .then(function (srcfe) {
555 return _this.resolveDirectoryUrl(newPath)
556 .then(function (deste) {
557 return File.copy(srcfe, deste, newFileName);
558 });
559 });
560 };
561 /**
562 * @private
563 */
564 File.fillErrorMessage = function (err) {
565 try {
566 err.message = File.cordovaFileError[err.code];
567 }
568 catch (e) { }
569 };
570 /**
571 * Resolves a local file system URL
572 * @param fileUrl {string} file system url
573 * @returns {Promise<Entry>}
574 */
575 File.resolveLocalFilesystemUrl = function (fileUrl) {
576 return new Promise(function (resolve, reject) {
577 try {
578 window.resolveLocalFileSystemURL(fileUrl, function (entry) {
579 resolve(entry);
580 }, function (err) {
581 File.fillErrorMessage(err);
582 reject(err);
583 });
584 }
585 catch (xc) {
586 File.fillErrorMessage(xc);
587 reject(xc);
588 }
589 });
590 };
591 /**
592 * Resolves a local directory url
593 * @param directoryUrl {string} directory system url
594 * @returns {Promise<DirectoryEntry>}
595 */
596 File.resolveDirectoryUrl = function (directoryUrl) {
597 return File.resolveLocalFilesystemUrl(directoryUrl)
598 .then(function (de) {
599 if (de.isDirectory) {
600 return de;
601 }
602 else {
603 var err = new exports.FileError(13);
604 err.message = 'input is not a directory';
605 return Promise.reject(err);
606 }
607 });
608 };
609 /**
610 * Get a directory
611 * @param directoryEntry {DirectoryEntry} Directory entry, obtained by resolveDirectoryUrl method
612 * @param directoryName {string} Directory name
613 * @param flags {Flags} Options
614 * @returns {Promise<DirectoryEntry>}
615 */
616 File.getDirectory = function (directoryEntry, directoryName, flags) {
617 return new Promise(function (resolve, reject) {
618 try {
619 directoryEntry.getDirectory(directoryName, flags, function (de) {
620 resolve(de);
621 }, function (err) {
622 File.fillErrorMessage(err);
623 reject(err);
624 });
625 }
626 catch (xc) {
627 File.fillErrorMessage(xc);
628 reject(xc);
629 }
630 });
631 };
632 /**
633 * Get a file
634 * @param directoryEntry {DirectoryEntry} Directory entry, obtained by resolveDirectoryUrl method
635 * @param fileName {string} File name
636 * @param flags {Flags} Options
637 * @returns {Promise<FileEntry>}
638 */
639 File.getFile = function (directoryEntry, fileName, flags) {
640 return new Promise(function (resolve, reject) {
641 try {
642 directoryEntry.getFile(fileName, flags, resolve, function (err) {
643 File.fillErrorMessage(err);
644 reject(err);
645 });
646 }
647 catch (xc) {
648 File.fillErrorMessage(xc);
649 reject(xc);
650 }
651 });
652 };
653 /**
654 * @private
655 */
656 File.remove = function (fe) {
657 return new Promise(function (resolve, reject) {
658 fe.remove(function () {
659 resolve({ success: true, fileRemoved: fe });
660 }, function (err) {
661 File.fillErrorMessage(err);
662 reject(err);
663 });
664 });
665 };
666 /**
667 * @private
668 */
669 File.move = function (srce, destdir, newName) {
670 return new Promise(function (resolve, reject) {
671 srce.moveTo(destdir, newName, function (deste) {
672 resolve(deste);
673 }, function (err) {
674 File.fillErrorMessage(err);
675 reject(err);
676 });
677 });
678 };
679 /**
680 * @private
681 */
682 File.copy = function (srce, destdir, newName) {
683 return new Promise(function (resolve, reject) {
684 srce.copyTo(destdir, newName, function (deste) {
685 resolve(deste);
686 }, function (err) {
687 File.fillErrorMessage(err);
688 reject(err);
689 });
690 });
691 };
692 /**
693 * @private
694 */
695 File.readEntries = function (dr) {
696 return new Promise(function (resolve, reject) {
697 dr.readEntries(function (entries) {
698 resolve(entries);
699 }, function (err) {
700 File.fillErrorMessage(err);
701 reject(err);
702 });
703 });
704 };
705 /**
706 * @private
707 */
708 File.rimraf = function (de) {
709 return new Promise(function (resolve, reject) {
710 de.removeRecursively(function () {
711 resolve({ success: true, fileRemoved: de });
712 }, function (err) {
713 File.fillErrorMessage(err);
714 reject(err);
715 });
716 });
717 };
718 /**
719 * @private
720 */
721 File.createWriter = function (fe) {
722 return new Promise(function (resolve, reject) {
723 fe.createWriter(function (writer) {
724 resolve(writer);
725 }, function (err) {
726 File.fillErrorMessage(err);
727 reject(err);
728 });
729 });
730 };
731 /**
732 * @private
733 */
734 File.write = function (writer, gu) {
735 if (gu instanceof Blob) {
736 return this.writeFileInChunks(writer, gu);
737 }
738 return new Promise(function (resolve, reject) {
739 writer.onwriteend = function (evt) {
740 if (writer.error) {
741 reject(writer.error);
742 }
743 else {
744 resolve(evt);
745 }
746 };
747 writer.write(gu);
748 });
749 };
750 /**
751 * @private
752 */
753 File.writeFileInChunks = function (writer, file) {
754 var BLOCK_SIZE = 1024 * 1024;
755 var writtenSize = 0;
756 function writeNextChunk() {
757 var size = Math.min(BLOCK_SIZE, file.size - writtenSize);
758 var chunk = file.slice(writtenSize, writtenSize + size);
759 writtenSize += size;
760 writer.write(chunk);
761 }
762 return new Promise(function (resolve, reject) {
763 writer.onerror = reject;
764 writer.onwrite = function () {
765 if (writtenSize < file.size) {
766 writeNextChunk();
767 }
768 else {
769 resolve();
770 }
771 };
772 writeNextChunk();
773 });
774 };
775 File.cordovaFileError = {
776 1: 'NOT_FOUND_ERR',
777 2: 'SECURITY_ERR',
778 3: 'ABORT_ERR',
779 4: 'NOT_READABLE_ERR',
780 5: 'ENCODING_ERR',
781 6: 'NO_MODIFICATION_ALLOWED_ERR',
782 7: 'INVALID_STATE_ERR',
783 8: 'SYNTAX_ERR',
784 9: 'INVALID_MODIFICATION_ERR',
785 10: 'QUOTA_EXCEEDED_ERR',
786 11: 'TYPE_MISMATCH_ERR',
787 12: 'PATH_EXISTS_ERR',
788 13: 'WRONG_ENTRY_TYPE',
789 14: 'DIR_READ_ERR',
790 };
791 __decorate([
792 plugin_1.CordovaProperty
793 ], File, "applicationDirectory", void 0);
794 __decorate([
795 plugin_1.CordovaProperty
796 ], File, "applicationStorageDirectory", void 0);
797 __decorate([
798 plugin_1.CordovaProperty
799 ], File, "dataDirectory", void 0);
800 __decorate([
801 plugin_1.CordovaProperty
802 ], File, "cacheDirectory", void 0);
803 __decorate([
804 plugin_1.CordovaProperty
805 ], File, "externalApplicationStorageDirectory", void 0);
806 __decorate([
807 plugin_1.CordovaProperty
808 ], File, "externalDataDirectory", void 0);
809 __decorate([
810 plugin_1.CordovaProperty
811 ], File, "externalCacheDirectory", void 0);
812 __decorate([
813 plugin_1.CordovaProperty
814 ], File, "externalRootDirectory", void 0);
815 __decorate([
816 plugin_1.CordovaProperty
817 ], File, "tempDirectory", void 0);
818 __decorate([
819 plugin_1.CordovaProperty
820 ], File, "syncedDataDirectory", void 0);
821 __decorate([
822 plugin_1.CordovaProperty
823 ], File, "documentsDirectory", void 0);
824 __decorate([
825 plugin_1.CordovaProperty
826 ], File, "sharedDirectory", void 0);
827 File = __decorate([
828 plugin_1.Plugin({
829 pluginName: 'File',
830 plugin: 'cordova-plugin-file',
831 pluginRef: 'cordova.file',
832 repo: 'https://github.com/apache/cordova-plugin-file'
833 })
834 ], File);
835 return File;
836}());
837exports.File = File;
838//# sourceMappingURL=file.js.map
\No newline at end of file