From 842a80d4930e3cb01d2da6b91708f4e1b1d266eb Mon Sep 17 00:00:00 2001 From: Ibby Date: Tue, 11 Oct 2016 20:10:47 -0400 Subject: [PATCH 1/8] fix(file): fix writeFile method addresses #464 #552 #666 --- src/plugins/file.ts | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/plugins/file.ts b/src/plugins/file.ts index 59c0ccea2..9cb067588 100644 --- a/src/plugins/file.ts +++ b/src/plugins/file.ts @@ -674,22 +674,30 @@ export class File { if (replaceOrOptions) { if (typeof(replaceOrOptions) === 'boolean') { opts.replace = replaceOrOptions; + } else { + opts.replace = (replaceOrOptions).replace } } + let getFileOpts: Flags = { + create: true, + exclusive: opts.replace + }; + return File.resolveDirectoryUrl(path) .then((fse) => { - return File.getFile(fse, fileName, opts); + return File.getFile(fse, fileName, getFileOpts); }) .then((fe) => { return File.createWriter(fe); }) .then((writer) => { + if (opts.append) { writer.seek(writer.length); } - if (opts.hasOwnProperty('truncate')) { + if (opts.truncate) { writer.truncate(opts.truncate); } @@ -1018,9 +1026,7 @@ export class File { private static getFile(fse: DirectoryEntry, fn: string, flags: Flags): Promise { return new Promise((resolve, reject) => { try { - fse.getFile(fn, flags, (fe) => { - resolve(fe); - }, (err) => { + fse.getFile(fn, flags, resolve, (err) => { File.fillErrorMessage(err); reject(err); }); @@ -1123,12 +1129,12 @@ export class File { return this.writeFileInChunks(writer, gu); } - return new Promise((resolve, reject) => { + return new Promise((resolve, reject) => { writer.onwriteend = (evt) => { if (writer.error) { reject(writer.error); } else { - resolve(); + resolve(evt); } }; writer.write(gu); From 5710eb78a85e3cde0d04078b1915961d6ef26562 Mon Sep 17 00:00:00 2001 From: Ibby Date: Tue, 11 Oct 2016 20:13:21 -0400 Subject: [PATCH 2/8] fix(file): last parameter for writeFile now only accepts options --- src/plugins/file.ts | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/src/plugins/file.ts b/src/plugins/file.ts index 9cb067588..e352c5bb5 100644 --- a/src/plugins/file.ts +++ b/src/plugins/file.ts @@ -659,29 +659,20 @@ export class File { * @param {string} path Base FileSystem. Please refer to the iOS and Android filesystems above * @param {string} fileName path relative to base path * @param {string | Blob} text content or blob to write - * @param {boolean | WriteOptions} replaceOrOptions replace file if set to true. See WriteOptions for more information. + * @param {WriteOptions} options replace file if set to true. See WriteOptions for more information. * @returns {Promise} Returns a Promise that resolves or rejects with an error. */ static writeFile(path: string, fileName: string, - text: string | Blob, replaceOrOptions: boolean | WriteOptions): Promise { + text: string | Blob, options: WriteOptions): Promise { if ((/^\//.test(fileName))) { let err = new FileError(5); err.message = 'file-name cannot start with \/'; return Promise.reject(err); } - let opts: WriteOptions = {}; - if (replaceOrOptions) { - if (typeof(replaceOrOptions) === 'boolean') { - opts.replace = replaceOrOptions; - } else { - opts.replace = (replaceOrOptions).replace - } - } - let getFileOpts: Flags = { create: true, - exclusive: opts.replace + exclusive: options.replace }; return File.resolveDirectoryUrl(path) @@ -693,12 +684,12 @@ export class File { }) .then((writer) => { - if (opts.append) { + if (options.append) { writer.seek(writer.length); } - if (opts.truncate) { - writer.truncate(opts.truncate); + if (options.truncate) { + writer.truncate(options.truncate); } return File.write(writer, text); From 542ff4cf952ada154541e1c2dc81ac2485d931fe Mon Sep 17 00:00:00 2001 From: Ibby Date: Tue, 11 Oct 2016 20:16:39 -0400 Subject: [PATCH 3/8] feat(file): resolveLocalFilesystemUrl and resolveDirectoryUrl are now public methods closes #657 --- src/plugins/file.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/plugins/file.ts b/src/plugins/file.ts index e352c5bb5..588bc89fa 100644 --- a/src/plugins/file.ts +++ b/src/plugins/file.ts @@ -958,9 +958,11 @@ export class File { } /** - * @private + * Resolves a local file system URL + * @param furl {string} file system url + * @returns {Promise} */ - private static resolveLocalFilesystemUrl(furl: string): Promise { + static resolveLocalFilesystemUrl(furl: string): Promise { return new Promise((resolve, reject) => { try { window.resolveLocalFileSystemURL(furl, (entry) => { @@ -977,9 +979,11 @@ export class File { } /** - * @private + * Resolves a local directory url + * @param durl {string} directory system url + * @returns {Promise} */ - private static resolveDirectoryUrl(durl: string): Promise { + static resolveDirectoryUrl(durl: string): Promise { return File.resolveLocalFilesystemUrl(durl) .then((de) => { if (de.isDirectory) { From 5c92455ee9868f704354de6f36c1c3e91daee400 Mon Sep 17 00:00:00 2001 From: Ibby Date: Tue, 11 Oct 2016 20:30:14 -0400 Subject: [PATCH 4/8] feat(file): getFile and getDirectory are now public closes #657 --- src/plugins/file.ts | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/src/plugins/file.ts b/src/plugins/file.ts index 588bc89fa..794e2d8ad 100644 --- a/src/plugins/file.ts +++ b/src/plugins/file.ts @@ -959,13 +959,13 @@ export class File { /** * Resolves a local file system URL - * @param furl {string} file system url + * @param fileUrl {string} file system url * @returns {Promise} */ - static resolveLocalFilesystemUrl(furl: string): Promise { + static resolveLocalFilesystemUrl(fileUrl: string): Promise { return new Promise((resolve, reject) => { try { - window.resolveLocalFileSystemURL(furl, (entry) => { + window.resolveLocalFileSystemURL(fileUrl, (entry) => { resolve(entry); }, (err) => { File.fillErrorMessage(err); @@ -980,11 +980,11 @@ export class File { /** * Resolves a local directory url - * @param durl {string} directory system url + * @param directoryUrl {string} directory system url * @returns {Promise} */ - static resolveDirectoryUrl(durl: string): Promise { - return File.resolveLocalFilesystemUrl(durl) + static resolveDirectoryUrl(directoryUrl: string): Promise { + return File.resolveLocalFilesystemUrl(directoryUrl) .then((de) => { if (de.isDirectory) { return de; @@ -997,12 +997,16 @@ export class File { } /** - * @private + * Get a directory + * @param directoryEntry {DirectoryEntry} Directory entry, obtained by resolveDirectoryUrl method + * @param directoryName {string} Directory name + * @param flags {Flags} Options + * @returns {Promise} */ - private static getDirectory(fse: DirectoryEntry, dn: string, flags: Flags): Promise { + static getDirectory(directoryEntry: DirectoryEntry, directoryName: string, flags: Flags): Promise { return new Promise((resolve, reject) => { try { - fse.getDirectory(dn, flags, (de) => { + directoryEntry.getDirectory(directoryName, flags, (de) => { resolve(de); }, (err) => { File.fillErrorMessage(err); @@ -1016,12 +1020,16 @@ export class File { } /** - * @private + * Get a file + * @param directoryEntry {DirectoryEntry} Directory entry, obtained by resolveDirectoryUrl method + * @param fileName {string} File name + * @param flags {Flags} Options + * @returns {Promise} */ - private static getFile(fse: DirectoryEntry, fn: string, flags: Flags): Promise { + static getFile(directoryEntry: DirectoryEntry, fileName: string, flags: Flags): Promise { return new Promise((resolve, reject) => { try { - fse.getFile(fn, flags, resolve, (err) => { + directoryEntry.getFile(fileName, flags, resolve, (err) => { File.fillErrorMessage(err); reject(err); }); From fe46907aaa8db6a3778a2e457a13502ce78a44e1 Mon Sep 17 00:00:00 2001 From: Ibby Date: Tue, 11 Oct 2016 20:39:26 -0400 Subject: [PATCH 5/8] docs(file): document getFreeDiskspace --- src/plugins/file.ts | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/plugins/file.ts b/src/plugins/file.ts index 794e2d8ad..612332cab 100644 --- a/src/plugins/file.ts +++ b/src/plugins/file.ts @@ -381,6 +381,10 @@ export class File { 14: 'DIR_READ_ERR', }; + /** + * Get free disk space + * @returns {Promise { return; @@ -393,7 +397,6 @@ export class File { * @param {string} dir Name of directory to check * @return {Promise} Returns a Promise that resolves to true if the directory exists or rejects with an error. */ - static checkDir(path: string, dir: string): Promise { if ((/^\//.test(dir))) { let err = new FileError(5); @@ -418,7 +421,6 @@ export class File { * @param {boolean} replace If true, replaces file with same name. If false returns error * @return {Promise} Returns a Promise that resolves with a DirectoryEntry or rejects with an error. */ - static createDir(path: string, dirName: string, replace: boolean): Promise { if ((/^\//.test(dirName))) { let err = new FileError(5); @@ -447,7 +449,6 @@ export class File { * @param {string} dirName The directory name * @return {Promise} Returns a Promise that resolves to a RemoveResult or rejects with an error. */ - static removeDir(path: string, dirName: string): Promise { if ((/^\//.test(dirName))) { let err = new FileError(5); @@ -473,7 +474,6 @@ export class File { * @param {string} newDirName The destination directory name * @return {Promise} Returns a Promise that resolves to the new DirectoryEntry object or rejects with an error. */ - static moveDir(path: string, dirName: string, newPath: string, newDirName: string): Promise { newDirName = newDirName || dirName; @@ -530,7 +530,6 @@ export class File { * @param {string} dirName Name of directory * @return {Promise} Returns a Promise that resolves to an array of Entry objects or rejects with an error. */ - static listDir(path: string, dirName: string): Promise { if ((/^\//.test(dirName))) { let err = new FileError(5); @@ -555,7 +554,6 @@ export class File { * @param {string} dirName Name of directory * @return {Promise} Returns a Promise that resolves with a RemoveResult or rejects with an error. */ - static removeRecursively(path: string, dirName: string): Promise { if ((/^\//.test(dirName))) { let err = new FileError(5); @@ -579,7 +577,6 @@ export class File { * @param {string} file Name of file to check * @return {Promise} Returns a Promise that resolves with a boolean or rejects with an error. */ - static checkFile(path: string, file: string): Promise { if ((/^\//.test(file))) { let err = new FileError(5); @@ -637,7 +634,6 @@ export class File { * @param {string} fileName Name of file to remove * @return {Promise} Returns a Promise that resolves to a RemoveResult or rejects with an error. */ - static removeFile(path: string, fileName: string): Promise { if ((/^\//.test(fileName))) { let err = new FileError(5); From d2f42ef33a12b2f706016dd06cf4f337aebd0c03 Mon Sep 17 00:00:00 2001 From: Ibby Date: Tue, 11 Oct 2016 20:44:27 -0400 Subject: [PATCH 6/8] fix(file): getFreeDiskSpace now works --- src/plugins/file.ts | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/plugins/file.ts b/src/plugins/file.ts index 612332cab..3e540b981 100644 --- a/src/plugins/file.ts +++ b/src/plugins/file.ts @@ -1,4 +1,4 @@ -import { Plugin, Cordova } from './plugin'; +import {Plugin, Cordova, pluginWarn} from './plugin'; declare var window: any; declare var cordova: any; @@ -337,6 +337,12 @@ declare var FileError: { PATH_EXISTS_ERR: number; }; +let pluginMeta = { + plugin: 'cordova-plugin-file', + pluginRef: 'cordova.file', + repo: 'https://github.com/apache/cordova-plugin-file' +}; + /** * @name File * @description @@ -358,11 +364,7 @@ declare var FileError: { * 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/ * It also implements the FileWriter spec : http://dev.w3.org/2009/dap/file-system/file-writer.html */ -@Plugin({ - plugin: 'cordova-plugin-file', - pluginRef: 'cordova.file', - repo: 'https://github.com/apache/cordova-plugin-file' -}) +@Plugin(pluginMeta) export class File { static cordovaFileError: {} = { 1: 'NOT_FOUND_ERR', @@ -383,11 +385,17 @@ export class File { /** * Get free disk space - * @returns {Promise} */ - @Cordova() static getFreeDiskSpace(): Promise { - return; + return new Promise((resolve, reject) => { + if (!cordova || !cordova.exec) { + pluginWarn(pluginMeta); + reject({ error: 'plugin_not_installed' }); + } else { + cordova.exec(resolve, reject, 'File', 'getFreeDiskSpace', []); + } + }); } /** From 397a209ad2551413aef8b91b814a84d5ec85ad00 Mon Sep 17 00:00:00 2001 From: Ibby Date: Tue, 11 Oct 2016 20:46:05 -0400 Subject: [PATCH 7/8] docs(file): improve docs --- src/plugins/file.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/plugins/file.ts b/src/plugins/file.ts index 3e540b981..ec0a2a548 100644 --- a/src/plugins/file.ts +++ b/src/plugins/file.ts @@ -385,7 +385,7 @@ export class File { /** * Get free disk space - * @returns {Promise} + * @returns {Promise} Returns a promise that resolves with the remaining free disk space */ static getFreeDiskSpace(): Promise { return new Promise((resolve, reject) => { From 276d61bf3a681684cf79531ead83d0e8c4bc8fcc Mon Sep 17 00:00:00 2001 From: Ibby Date: Tue, 11 Oct 2016 20:48:53 -0400 Subject: [PATCH 8/8] fix(file): read methods can accept Blobs too --- src/plugins/file.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/plugins/file.ts b/src/plugins/file.ts index ec0a2a548..69cf3287f 100644 --- a/src/plugins/file.ts +++ b/src/plugins/file.ts @@ -301,10 +301,10 @@ export interface FileReader { onabort: (evt: ProgressEvent) => void; abort(): void; - readAsText(fe: File, encoding?: string): void; - readAsDataURL(fe: File): void; - readAsBinaryString(fe: File): void; - readAsArrayBuffer(fe: File): void; + readAsText(fe: File | Blob, encoding?: string): void; + readAsDataURL(fe: File | Blob): void; + readAsBinaryString(fe: File | Blob): void; + readAsArrayBuffer(fe: File | Blob): void; } declare var FileReader: {