diff --git a/src/@ionic-native/plugins/file/index.ts b/src/@ionic-native/plugins/file/index.ts index a4aeb8fb6..69e7cd8d1 100644 --- a/src/@ionic-native/plugins/file/index.ts +++ b/src/@ionic-native/plugins/file/index.ts @@ -821,37 +821,9 @@ export class File extends IonicNativePlugin { */ @CordovaCheck() readAsText(path: string, file: string): Promise { - if ((/^\//.test(file))) { - let err = new FileError(5); - err.message = 'file-name cannot start with \/'; - return Promise.reject(err); - } - - return this.resolveDirectoryUrl(path) - .then((directoryEntry: DirectoryEntry) => { - return this.getFile(directoryEntry, file, { create: false }); - }) - .then((fileEntry: FileEntry) => { - let reader = new FileReader(); - return new Promise((resolve, reject) => { - reader.onloadend = () => { - if (reader.result !== undefined || reader.result !== null) { - resolve(reader.result); - } else if (reader.error !== undefined || reader.error !== null) { - reject(reader.error); - } else { - reject({ code: null, message: 'READER_ONLOADEND_ERR' }); - } - }; - fileEntry.file(file => { - reader.readAsText(file); - }, error => { - reject(error); - }); - - }); - }); + return this.readFile(path, file, 'Text'); } + /** * Read file and return data as a base64 encoded data url. * A data url is of the form: @@ -863,78 +835,18 @@ export class File extends IonicNativePlugin { */ @CordovaCheck() readAsDataURL(path: string, file: string): Promise { - if ((/^\//.test(file))) { - let err = new FileError(5); - err.message = 'file-name cannot start with \/'; - return Promise.reject(err); - } - - return this.resolveDirectoryUrl(path) - .then((directoryEntry: DirectoryEntry) => { - return this.getFile(directoryEntry, file, { create: false }); - }) - .then((fileEntry: FileEntry) => { - let reader = new FileReader(); - return new Promise((resolve, reject) => { - reader.onloadend = () => { - if (reader.result !== undefined || reader.result !== null) { - resolve(reader.result); - } else if (reader.error !== undefined || reader.error !== null) { - reject(reader.error); - } else { - reject({ code: null, message: 'READER_ONLOADEND_ERR' }); - } - }; - - fileEntry.file(file => { - reader.readAsDataURL(file); - }, error => { - reject(error); - }); - }); - }); + return this.readFile(path, file, 'DataURL'); } /** * Read file and return data as a binary data. - * @param {string} path Base FileSystem. Please refer to the iOS and Android filesystems above * @param {string} file Name of file, relative to path. * @returns {Promise} Returns a Promise that resolves with the contents of the file as string rejects with an error. */ @CordovaCheck() readAsBinaryString(path: string, file: string): Promise { - if ((/^\//.test(file))) { - let err = new FileError(5); - err.message = 'file-name cannot start with \/'; - return Promise.reject(err); - } - - return this.resolveDirectoryUrl(path) - .then((directoryEntry: DirectoryEntry) => { - return this.getFile(directoryEntry, file, { create: false }); - }) - .then((fileEntry: FileEntry) => { - let reader = new FileReader(); - return new Promise((resolve, reject) => { - reader.onloadend = () => { - if (reader.result !== undefined || reader.result !== null) { - resolve(reader.result); - } else if (reader.error !== undefined || reader.error !== null) { - reject(reader.error); - } else { - reject({ code: null, message: 'READER_ONLOADEND_ERR' }); - } - }; - - fileEntry.file(file => { - reader.readAsBinaryString(file); - }, error => { - reject(error); - }); - - }); - }); + return this.readFile(path, file, 'BinaryString'); } /** @@ -945,6 +857,10 @@ export class File extends IonicNativePlugin { */ @CordovaCheck() readAsArrayBuffer(path: string, file: string): Promise { + return this.readFile(path, file, 'ArrayBuffer'); + } + + private readFile(path: string, file: string, readAs: 'ArrayBuffer' | 'BinaryString' | 'DataURL' | 'Text'): Promise { if ((/^\//.test(file))) { let err = new FileError(5); err.message = 'file-name cannot start with \/'; @@ -957,10 +873,10 @@ export class File extends IonicNativePlugin { }) .then((fileEntry: FileEntry) => { let reader = new FileReader(); - return new Promise((resolve, reject) => { + return new Promise((resolve, reject) => { reader.onloadend = () => { if (reader.result !== undefined || reader.result !== null) { - resolve(reader.result); + resolve(reader.result); } else if (reader.error !== undefined || reader.error !== null) { reject(reader.error); } else { @@ -969,7 +885,7 @@ export class File extends IonicNativePlugin { }; fileEntry.file(file => { - reader.readAsArrayBuffer(file); + reader[`readAs${readAs}`].call(null, file); }, error => { reject(error); });