From 96c47df151a2bc799e8f52b6e5c204033694f394 Mon Sep 17 00:00:00 2001 From: Ignat Ignatov Date: Wed, 6 Apr 2016 16:36:26 +0200 Subject: [PATCH 1/2] Added implementation for readAsText() method. --- src/plugins/file.ts | 52 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/src/plugins/file.ts b/src/plugins/file.ts index 54874bcbe..fb0fa9358 100644 --- a/src/plugins/file.ts +++ b/src/plugins/file.ts @@ -462,7 +462,57 @@ export class File { // static writeExistingFile(path: string, fileName: string, text: string): Promise { return } - // static readAsText(path: string, file: string): Promise { return } + /** + * Read a file as string. + * + * @param {string} path Base FileSystem. Please refer to the iOS and Android filesystems above + * @param {string} fileName Name of file to move + * @return Returns a Promise that resolves or rejects with an error. + */ + static readAsText(path: string, fileName: string): Promise { + let resolveFn, rejectFn; + let promise = new Promise((resolve, reject) => {resolveFn = resolve; rejectFn = reject; }); + + if ((/^\//.test(fileName))) { + rejectFn('file-name cannot start with \/'); + } + + try { + window.resolveLocalFileSystemURL(path, function (fileSystem) { + fileSystem.getFile(fileName, {create: false}, function (fileEntry) { + fileEntry.file(function (file) { + var reader = new FileReader(); + + reader.onloadend = function(e) { + if (this.result !== undefined && this.result !== null) { + resolveFn(this.result); + } else if (this.error !== undefined && this.error !== null) { + rejectFn(this.error); + } else { + rejectFn({code: null, message: 'READER_ONLOADEND_ERR'}); + } + } + + reader.readAsText(file); + }, function (error) { + error.message = File.cordovaFileError[error.code]; + rejectFn(error); + }); + }, function (err) { + err.message = File.cordovaFileError[err.code]; + rejectFn(err); + }); + }, function (er) { + er.message = File.cordovaFileError[er.code]; + rejectFn(er); + }); + } catch (e) { + e.message = File.cordovaFileError[e.code]; + rejectFn(e); + } + + return promise; + } // static readAsDataURL(path: string, file: string): Promise { return } From ce2772b994058b22978d0f842b2c95672a47e708 Mon Sep 17 00:00:00 2001 From: Barry Rowe Date: Thu, 14 Apr 2016 12:48:04 -0400 Subject: [PATCH 2/2] Append trailingSlash if not present on path param In some cases the incoming ```path``` variable may not necessarily contain a trailing slash, but in cases where the directory path is manually built within the plugin this was not protected against. This shouldn't be a big issue if consumers are using ```cordova.file.*``` for the path params, but not all consumers may be using this. --- src/plugins/file.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/plugins/file.ts b/src/plugins/file.ts index 54874bcbe..893458f1d 100644 --- a/src/plugins/file.ts +++ b/src/plugins/file.ts @@ -50,7 +50,11 @@ export class File { if ((/^\//.test(dir))) { rejectFn('directory cannot start with \/'); } - + + if (!(/\/$/.test(noSlash))) { + path += "/"; + } + try { var directory = path + dir; @@ -351,6 +355,10 @@ export class File { rejectFn('file cannot start with \/'); } + if (!(/\/$/.test(noSlash))) { + path += "/"; + } + try { var directory = path + file;