UNPKG

26.9 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', { value: true });
4
5var tslib = require('tslib');
6var core$1 = require('@angular/core');
7var core = require('@ionic-native/core');
8
9var File = /** @class */ (function (_super) {
10 tslib.__extends(File, _super);
11 function File() {
12 var _this = _super !== null && _super.apply(this, arguments) || this;
13 _this.cordovaFileError = {
14 1: 'NOT_FOUND_ERR',
15 2: 'SECURITY_ERR',
16 3: 'ABORT_ERR',
17 4: 'NOT_READABLE_ERR',
18 5: 'ENCODING_ERR',
19 6: 'NO_MODIFICATION_ALLOWED_ERR',
20 7: 'INVALID_STATE_ERR',
21 8: 'SYNTAX_ERR',
22 9: 'INVALID_MODIFICATION_ERR',
23 10: 'QUOTA_EXCEEDED_ERR',
24 11: 'TYPE_MISMATCH_ERR',
25 12: 'PATH_EXISTS_ERR',
26 13: 'WRONG_ENTRY_TYPE',
27 14: 'DIR_READ_ERR',
28 };
29 return _this;
30 }
31 File.prototype.getFreeDiskSpace = function () {
32 var _this = this;
33 return (function () {
34 if (core.checkAvailability(_this) === true) {
35 return core.getPromise(function (resolve, reject) {
36 cordova.exec(resolve, reject, 'File', 'getFreeDiskSpace', []);
37 });
38 }
39 })();
40 };
41 File.prototype.checkDir = function (path, dir) {
42 var _this = this;
43 return (function () {
44 if (core.checkAvailability(_this) === true) {
45 if (/^\//.test(dir)) {
46 var err = new FileError(5);
47 err.message = 'directory cannot start with /';
48 return Promise.reject(err);
49 }
50 var fullPath = path + dir;
51 return _this.resolveDirectoryUrl(fullPath).then(function () {
52 return true;
53 });
54 }
55 })();
56 };
57 File.prototype.createDir = function (path, dirName, replace) {
58 var _this = this;
59 return (function () {
60 if (core.checkAvailability(_this) === true) {
61 if (/^\//.test(dirName)) {
62 var err = new FileError(5);
63 err.message = 'directory cannot start with /';
64 return Promise.reject(err);
65 }
66 var options = {
67 create: true,
68 };
69 if (!replace) {
70 options.exclusive = true;
71 }
72 return _this.resolveDirectoryUrl(path).then(function (fse) {
73 return _this.getDirectory(fse, dirName, options);
74 });
75 }
76 })();
77 };
78 File.prototype.removeDir = function (path, dirName) {
79 var _this = this;
80 return (function () {
81 if (core.checkAvailability(_this) === true) {
82 if (/^\//.test(dirName)) {
83 var err = new FileError(5);
84 err.message = 'directory cannot start with /';
85 return Promise.reject(err);
86 }
87 return _this.resolveDirectoryUrl(path)
88 .then(function (fse) {
89 return _this.getDirectory(fse, dirName, { create: false });
90 })
91 .then(function (de) {
92 return _this.remove(de);
93 });
94 }
95 })();
96 };
97 File.prototype.moveDir = function (path, dirName, newPath, newDirName) {
98 var _this = this;
99 return (function () {
100 if (core.checkAvailability(_this) === true) {
101 newDirName = newDirName || dirName;
102 if (/^\//.test(newDirName)) {
103 var err = new FileError(5);
104 err.message = 'directory cannot start with /';
105 return Promise.reject(err);
106 }
107 return _this.resolveDirectoryUrl(path)
108 .then(function (fse) {
109 return _this.getDirectory(fse, dirName, { create: false });
110 })
111 .then(function (srcde) {
112 return _this.resolveDirectoryUrl(newPath).then(function (destenation) {
113 return _this.move(srcde, destenation, newDirName);
114 });
115 });
116 }
117 })();
118 };
119 File.prototype.copyDir = function (path, dirName, newPath, newDirName) {
120 var _this = this;
121 return (function () {
122 if (core.checkAvailability(_this) === true) {
123 if (/^\//.test(newDirName)) {
124 var err = new FileError(5);
125 err.message = 'directory cannot start with /';
126 return Promise.reject(err);
127 }
128 return _this.resolveDirectoryUrl(path)
129 .then(function (fse) {
130 return _this.getDirectory(fse, dirName, { create: false });
131 })
132 .then(function (srcde) {
133 return _this.resolveDirectoryUrl(newPath).then(function (deste) {
134 return _this.copy(srcde, deste, newDirName);
135 });
136 });
137 }
138 })();
139 };
140 File.prototype.listDir = function (path, dirName) {
141 var _this = this;
142 return (function () {
143 if (core.checkAvailability(_this) === true) {
144 if (/^\//.test(dirName)) {
145 var err = new FileError(5);
146 err.message = 'directory cannot start with /';
147 return Promise.reject(err);
148 }
149 return _this.resolveDirectoryUrl(path)
150 .then(function (fse) {
151 return _this.getDirectory(fse, dirName, {
152 create: false,
153 exclusive: false,
154 });
155 })
156 .then(function (de) {
157 var reader = de.createReader();
158 return _this.readEntries(reader);
159 });
160 }
161 })();
162 };
163 File.prototype.removeRecursively = function (path, dirName) {
164 var _this = this;
165 return (function () {
166 if (core.checkAvailability(_this) === true) {
167 if (/^\//.test(dirName)) {
168 var err = new FileError(5);
169 err.message = 'directory cannot start with /';
170 return Promise.reject(err);
171 }
172 return _this.resolveDirectoryUrl(path)
173 .then(function (fse) {
174 return _this.getDirectory(fse, dirName, { create: false });
175 })
176 .then(function (de) {
177 return _this.rimraf(de);
178 });
179 }
180 })();
181 };
182 File.prototype.checkFile = function (path, file) {
183 var _this = this;
184 return (function () {
185 if (core.checkAvailability(_this) === true) {
186 if (/^\//.test(file)) {
187 var err = new FileError(5);
188 err.message = 'file cannot start with /';
189 return Promise.reject(err);
190 }
191 return _this.resolveLocalFilesystemUrl(path + file).then(function (fse) {
192 if (fse.isFile) {
193 return true;
194 }
195 else {
196 var err = new FileError(13);
197 err.message = 'input is not a file';
198 return Promise.reject(err);
199 }
200 });
201 }
202 })();
203 };
204 File.prototype.createFile = function (path, fileName, replace) {
205 var _this = this;
206 return (function () {
207 if (core.checkAvailability(_this) === true) {
208 if (/^\//.test(fileName)) {
209 var err = new FileError(5);
210 err.message = 'file-name cannot start with /';
211 return Promise.reject(err);
212 }
213 var options = {
214 create: true,
215 };
216 if (!replace) {
217 options.exclusive = true;
218 }
219 return _this.resolveDirectoryUrl(path).then(function (fse) {
220 return _this.getFile(fse, fileName, options);
221 });
222 }
223 })();
224 };
225 File.prototype.removeFile = function (path, fileName) {
226 var _this = this;
227 return (function () {
228 if (core.checkAvailability(_this) === true) {
229 if (/^\//.test(fileName)) {
230 var err = new FileError(5);
231 err.message = 'file-name cannot start with /';
232 return Promise.reject(err);
233 }
234 return _this.resolveDirectoryUrl(path)
235 .then(function (fse) {
236 return _this.getFile(fse, fileName, { create: false });
237 })
238 .then(function (fe) {
239 return _this.remove(fe);
240 });
241 }
242 })();
243 };
244 File.prototype.writeFile = function (path, fileName, text, options) {
245 var _this = this;
246 if (options === void 0) { options = {}; }
247 return (function () {
248 if (core.checkAvailability(_this) === true) {
249 if (/^\//.test(fileName)) {
250 var err = new FileError(5);
251 err.message = 'file-name cannot start with /';
252 return Promise.reject(err);
253 }
254 var getFileOpts = {
255 create: !options.append,
256 exclusive: !options.replace,
257 };
258 return _this.resolveDirectoryUrl(path)
259 .then(function (directoryEntry) {
260 return _this.getFile(directoryEntry, fileName, getFileOpts);
261 })
262 .then(function (fileEntry) {
263 return _this.writeFileEntry(fileEntry, text, options);
264 });
265 }
266 })();
267 };
268 /**
269 * Write content to FileEntry.
270 * @hidden
271 * Write to an existing file.
272 * @param {FileEntry} fe file entry object
273 * @param {string | Blob | ArrayBuffer} text text content or blob to write
274 * @param {IWriteOptions} options replace file if set to true. See WriteOptions for more information.
275 * @returns {Promise<FileEntry>} Returns a Promise that resolves to updated file entry or rejects with an error.
276 */
277 File.prototype.writeFileEntry = function (fe, text, options) {
278 var _this = this;
279 return this.createWriter(fe)
280 .then(function (writer) {
281 if (options.append) {
282 writer.seek(writer.length);
283 }
284 if (options.truncate) {
285 writer.truncate(options.truncate);
286 }
287 return _this.write(writer, text);
288 })
289 .then(function () { return fe; });
290 };
291 File.prototype.writeExistingFile = function (path, fileName, text) {
292 var _this = this;
293 return (function () {
294 if (core.checkAvailability(_this) === true) {
295 return _this.writeFile(path, fileName, text, { replace: true });
296 }
297 })();
298 };
299 File.prototype.readAsText = function (path, file) {
300 var _this = this;
301 return (function () {
302 if (core.checkAvailability(_this) === true) {
303 return _this.readFile(path, file, 'Text');
304 }
305 })();
306 };
307 File.prototype.readAsDataURL = function (path, file) {
308 var _this = this;
309 return (function () {
310 if (core.checkAvailability(_this) === true) {
311 return _this.readFile(path, file, 'DataURL');
312 }
313 })();
314 };
315 File.prototype.readAsBinaryString = function (path, file) {
316 var _this = this;
317 return (function () {
318 if (core.checkAvailability(_this) === true) {
319 return _this.readFile(path, file, 'BinaryString');
320 }
321 })();
322 };
323 File.prototype.readAsArrayBuffer = function (path, file) {
324 var _this = this;
325 return (function () {
326 if (core.checkAvailability(_this) === true) {
327 return _this.readFile(path, file, 'ArrayBuffer');
328 }
329 })();
330 };
331 File.prototype.moveFile = function (path, fileName, newPath, newFileName) {
332 var _this = this;
333 return (function () {
334 if (core.checkAvailability(_this) === true) {
335 newFileName = newFileName || fileName;
336 if (/^\//.test(newFileName)) {
337 var err = new FileError(5);
338 err.message = 'file name cannot start with /';
339 return Promise.reject(err);
340 }
341 return _this.resolveDirectoryUrl(path)
342 .then(function (fse) {
343 return _this.getFile(fse, fileName, { create: false });
344 })
345 .then(function (srcfe) {
346 return _this.resolveDirectoryUrl(newPath).then(function (deste) {
347 return _this.move(srcfe, deste, newFileName);
348 });
349 });
350 }
351 })();
352 };
353 File.prototype.copyFile = function (path, fileName, newPath, newFileName) {
354 var _this = this;
355 return (function () {
356 if (core.checkAvailability(_this) === true) {
357 newFileName = newFileName || fileName;
358 if (/^\//.test(newFileName)) {
359 var err = new FileError(5);
360 err.message = 'file name cannot start with /';
361 return Promise.reject(err);
362 }
363 return _this.resolveDirectoryUrl(path)
364 .then(function (fse) {
365 return _this.getFile(fse, fileName, { create: false });
366 })
367 .then(function (srcfe) {
368 return _this.resolveDirectoryUrl(newPath).then(function (deste) {
369 return _this.copy(srcfe, deste, newFileName);
370 });
371 });
372 }
373 })();
374 };
375 /**
376 * @hidden
377 */
378 File.prototype.fillErrorMessage = function (err) {
379 try {
380 err.message = this.cordovaFileError[err.code];
381 }
382 catch (e) { }
383 };
384 File.prototype.resolveLocalFilesystemUrl = function (fileUrl) {
385 var _this = this;
386 return (function () {
387 if (core.checkAvailability(_this) === true) {
388 return core.getPromise(function (resolve, reject) {
389 try {
390 window.resolveLocalFileSystemURL(fileUrl, function (entry) {
391 resolve(entry);
392 }, function (err) {
393 _this.fillErrorMessage(err);
394 reject(err);
395 });
396 }
397 catch (xc) {
398 _this.fillErrorMessage(xc);
399 reject(xc);
400 }
401 });
402 }
403 })();
404 };
405 File.prototype.resolveDirectoryUrl = function (directoryUrl) {
406 var _this = this;
407 return (function () {
408 if (core.checkAvailability(_this) === true) {
409 return _this.resolveLocalFilesystemUrl(directoryUrl).then(function (de) {
410 if (de.isDirectory) {
411 return de;
412 }
413 else {
414 var err = new FileError(13);
415 err.message = 'input is not a directory';
416 return Promise.reject(err);
417 }
418 });
419 }
420 })();
421 };
422 File.prototype.getDirectory = function (directoryEntry, directoryName, flags) {
423 var _this = this;
424 return (function () {
425 if (core.checkAvailability(_this) === true) {
426 return new Promise(function (resolve, reject) {
427 try {
428 directoryEntry.getDirectory(directoryName, flags, function (de) {
429 resolve(de);
430 }, function (err) {
431 _this.fillErrorMessage(err);
432 reject(err);
433 });
434 }
435 catch (xc) {
436 _this.fillErrorMessage(xc);
437 reject(xc);
438 }
439 });
440 }
441 })();
442 };
443 File.prototype.getFile = function (directoryEntry, fileName, flags) {
444 var _this = this;
445 return (function () {
446 if (core.checkAvailability(_this) === true) {
447 return new Promise(function (resolve, reject) {
448 try {
449 directoryEntry.getFile(fileName, flags, resolve, function (err) {
450 _this.fillErrorMessage(err);
451 reject(err);
452 });
453 }
454 catch (xc) {
455 _this.fillErrorMessage(xc);
456 reject(xc);
457 }
458 });
459 }
460 })();
461 };
462 File.prototype.readFile = function (path, file, readAs) {
463 var _this = this;
464 if (/^\//.test(file)) {
465 var err = new FileError(5);
466 err.message = 'file-name cannot start with /';
467 return Promise.reject(err);
468 }
469 return this.resolveDirectoryUrl(path)
470 .then(function (directoryEntry) {
471 return _this.getFile(directoryEntry, file, { create: false });
472 })
473 .then(function (fileEntry) {
474 var reader = new FileReader();
475 return core.getPromise(function (resolve, reject) {
476 reader.onloadend = function () {
477 if (reader.result !== undefined || reader.result !== null) {
478 resolve(reader.result);
479 }
480 else if (reader.error !== undefined || reader.error !== null) {
481 reject(reader.error);
482 }
483 else {
484 reject({ code: null, message: 'READER_ONLOADEND_ERR' });
485 }
486 };
487 fileEntry.file(function (file) {
488 reader["readAs" + readAs].call(reader, file);
489 }, function (error) {
490 reject(error);
491 });
492 });
493 });
494 };
495 /**
496 * @hidden
497 */
498 File.prototype.remove = function (fe) {
499 var _this = this;
500 return new Promise(function (resolve, reject) {
501 fe.remove(function () {
502 resolve({ success: true, fileRemoved: fe });
503 }, function (err) {
504 _this.fillErrorMessage(err);
505 reject(err);
506 });
507 });
508 };
509 /**
510 * @hidden
511 */
512 File.prototype.move = function (srce, destdir, newName) {
513 var _this = this;
514 return new Promise(function (resolve, reject) {
515 srce.moveTo(destdir, newName, function (deste) {
516 resolve(deste);
517 }, function (err) {
518 _this.fillErrorMessage(err);
519 reject(err);
520 });
521 });
522 };
523 /**
524 * @hidden
525 */
526 File.prototype.copy = function (srce, destdir, newName) {
527 var _this = this;
528 return new Promise(function (resolve, reject) {
529 srce.copyTo(destdir, newName, function (deste) {
530 resolve(deste);
531 }, function (err) {
532 _this.fillErrorMessage(err);
533 reject(err);
534 });
535 });
536 };
537 /**
538 * @hidden
539 */
540 File.prototype.readEntries = function (dr) {
541 var _this = this;
542 return new Promise(function (resolve, reject) {
543 dr.readEntries(function (entries) {
544 resolve(entries);
545 }, function (err) {
546 _this.fillErrorMessage(err);
547 reject(err);
548 });
549 });
550 };
551 /**
552 * @hidden
553 */
554 File.prototype.rimraf = function (de) {
555 var _this = this;
556 return new Promise(function (resolve, reject) {
557 de.removeRecursively(function () {
558 resolve({ success: true, fileRemoved: de });
559 }, function (err) {
560 _this.fillErrorMessage(err);
561 reject(err);
562 });
563 });
564 };
565 /**
566 * @hidden
567 */
568 File.prototype.createWriter = function (fe) {
569 var _this = this;
570 return new Promise(function (resolve, reject) {
571 fe.createWriter(function (writer) {
572 resolve(writer);
573 }, function (err) {
574 _this.fillErrorMessage(err);
575 reject(err);
576 });
577 });
578 };
579 /**
580 * @hidden
581 */
582 File.prototype.write = function (writer, gu) {
583 if (gu instanceof Blob) {
584 return this.writeFileInChunks(writer, gu);
585 }
586 return new Promise(function (resolve, reject) {
587 writer.onwriteend = function (evt) {
588 if (writer.error) {
589 reject(writer.error);
590 }
591 else {
592 resolve(evt);
593 }
594 };
595 writer.write(gu);
596 });
597 };
598 /**
599 * @hidden
600 */
601 File.prototype.writeFileInChunks = function (writer, file) {
602 var BLOCK_SIZE = 1024 * 1024;
603 var writtenSize = 0;
604 function writeNextChunk() {
605 var size = Math.min(BLOCK_SIZE, file.size - writtenSize);
606 var chunk = file.slice(writtenSize, writtenSize + size);
607 writtenSize += size;
608 writer.write(chunk);
609 }
610 return core.getPromise(function (resolve, reject) {
611 writer.onerror = reject;
612 writer.onwrite = function () {
613 if (writtenSize < file.size) {
614 writeNextChunk();
615 }
616 else {
617 resolve();
618 }
619 };
620 writeNextChunk();
621 });
622 };
623 Object.defineProperty(File.prototype, "applicationDirectory", {
624 get: function () { return core.cordovaPropertyGet(this, "applicationDirectory"); },
625 set: function (value) { core.cordovaPropertySet(this, "applicationDirectory", value); },
626 enumerable: false,
627 configurable: true
628 });
629 Object.defineProperty(File.prototype, "applicationStorageDirectory", {
630 get: function () { return core.cordovaPropertyGet(this, "applicationStorageDirectory"); },
631 set: function (value) { core.cordovaPropertySet(this, "applicationStorageDirectory", value); },
632 enumerable: false,
633 configurable: true
634 });
635 Object.defineProperty(File.prototype, "dataDirectory", {
636 get: function () { return core.cordovaPropertyGet(this, "dataDirectory"); },
637 set: function (value) { core.cordovaPropertySet(this, "dataDirectory", value); },
638 enumerable: false,
639 configurable: true
640 });
641 Object.defineProperty(File.prototype, "cacheDirectory", {
642 get: function () { return core.cordovaPropertyGet(this, "cacheDirectory"); },
643 set: function (value) { core.cordovaPropertySet(this, "cacheDirectory", value); },
644 enumerable: false,
645 configurable: true
646 });
647 Object.defineProperty(File.prototype, "externalApplicationStorageDirectory", {
648 get: function () { return core.cordovaPropertyGet(this, "externalApplicationStorageDirectory"); },
649 set: function (value) { core.cordovaPropertySet(this, "externalApplicationStorageDirectory", value); },
650 enumerable: false,
651 configurable: true
652 });
653 Object.defineProperty(File.prototype, "externalDataDirectory", {
654 get: function () { return core.cordovaPropertyGet(this, "externalDataDirectory"); },
655 set: function (value) { core.cordovaPropertySet(this, "externalDataDirectory", value); },
656 enumerable: false,
657 configurable: true
658 });
659 Object.defineProperty(File.prototype, "externalCacheDirectory", {
660 get: function () { return core.cordovaPropertyGet(this, "externalCacheDirectory"); },
661 set: function (value) { core.cordovaPropertySet(this, "externalCacheDirectory", value); },
662 enumerable: false,
663 configurable: true
664 });
665 Object.defineProperty(File.prototype, "externalRootDirectory", {
666 get: function () { return core.cordovaPropertyGet(this, "externalRootDirectory"); },
667 set: function (value) { core.cordovaPropertySet(this, "externalRootDirectory", value); },
668 enumerable: false,
669 configurable: true
670 });
671 Object.defineProperty(File.prototype, "tempDirectory", {
672 get: function () { return core.cordovaPropertyGet(this, "tempDirectory"); },
673 set: function (value) { core.cordovaPropertySet(this, "tempDirectory", value); },
674 enumerable: false,
675 configurable: true
676 });
677 Object.defineProperty(File.prototype, "syncedDataDirectory", {
678 get: function () { return core.cordovaPropertyGet(this, "syncedDataDirectory"); },
679 set: function (value) { core.cordovaPropertySet(this, "syncedDataDirectory", value); },
680 enumerable: false,
681 configurable: true
682 });
683 Object.defineProperty(File.prototype, "documentsDirectory", {
684 get: function () { return core.cordovaPropertyGet(this, "documentsDirectory"); },
685 set: function (value) { core.cordovaPropertySet(this, "documentsDirectory", value); },
686 enumerable: false,
687 configurable: true
688 });
689 Object.defineProperty(File.prototype, "sharedDirectory", {
690 get: function () { return core.cordovaPropertyGet(this, "sharedDirectory"); },
691 set: function (value) { core.cordovaPropertySet(this, "sharedDirectory", value); },
692 enumerable: false,
693 configurable: true
694 });
695 File.pluginName = "File";
696 File.plugin = "cordova-plugin-file";
697 File.pluginRef = "cordova.file";
698 File.repo = "https://github.com/apache/cordova-plugin-file";
699 File.platforms = ["Android", "Browser", "iOS", "macOS", "Windows"];
700 File.decorators = [
701 { type: core$1.Injectable }
702 ];
703 return File;
704}(core.IonicNativePlugin));
705
706exports.File = File;