Repository: cordova-plugin-file Updated Branches: refs/heads/master 21298dce5 -> 3bc494559
CB-11699 Read files as Data URLs properly When reading file as Data URL, CHUNK_SIZE must be divisible by 3, otherwise the resultant string made by concatenating chunks will not be a valid Base64 encoded Data URL. Also Windows do not support reading sliced files as data URLs, so we set chunk size equal to file size Project: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/repo Commit: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/commit/3bc49455 Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/tree/3bc49455 Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/diff/3bc49455 Branch: refs/heads/master Commit: 3bc49455963e873af457a440ea240d5422d01865 Parents: 21298dc Author: Vladimir Kotikov <[email protected]> Authored: Tue Aug 16 11:20:03 2016 +0300 Committer: Vladimir Kotikov <[email protected]> Committed: Tue Aug 16 12:02:37 2016 +0300 ---------------------------------------------------------------------- tests/tests.js | 36 +++++++++++++++++++++++++++++++++++- www/FileReader.js | 14 ++++++++++++-- 2 files changed, 47 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/3bc49455/tests/tests.js ---------------------------------------------------------------------- diff --git a/tests/tests.js b/tests/tests.js index 33688dc..e0a8b3b 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -2469,7 +2469,41 @@ exports.defineAutoTests = function () { }, 0, -1, largeText); }); - }); + it("file.spec.94.6 should read large file in multiple chunks, readAsDataURL", function (done) { + var largeText = ""; + for (var i = 0; i < 10; i++) { + largeText += "Test " + i + "\n"; + } + + // Set the chunk size so that the read will take 5 chunks + FileReader.READ_CHUNK_SIZE = Math.floor(largeText.length / 4) + 1; + + var lastProgressValue = 0; + var progressFunc = function (evt) { + expect(evt.total).toBeDefined(); + expect(evt.total).toEqual(largeText.length); + + expect(evt.loaded).toBeDefined(); + expect(evt.loaded).toBeGreaterThan(lastProgressValue); + expect(evt.loaded).toBeLessThan(evt.total + 1); + + lastProgressValue = evt.loaded; + }; + + runReaderTest('readAsDataURL', false, done, progressFunc, + function (evt, fileData, fileDataAsBinaryString) { + expect(function () { + // Cut off data uri prefix + var base64Data = evt.target.result.substring(evt.target.result.indexOf(',') + 1); + expect(window.atob(base64Data)).toEqual(fileData); + }).not.toThrow(); + + expect(lastProgressValue).toEqual(largeText.length); + done(); + }, + undefined, undefined, largeText); + }); + }); //Read method describe('FileWriter', function () { it("file.spec.95 should have correct methods", function (done) { http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/3bc49455/www/FileReader.js ---------------------------------------------------------------------- diff --git a/www/FileReader.js b/www/FileReader.js index 1b841d8..8d2f086 100644 --- a/www/FileReader.js +++ b/www/FileReader.js @@ -121,9 +121,19 @@ function readSuccessCallback(readType, encoding, offset, totalSize, accumulate, return; } + var CHUNK_SIZE = FileReader.READ_CHUNK_SIZE; + if (readType === 'readAsDataURL') { + // Windows proxy does not support reading file slices as Data URLs + // so read the whole file at once. + CHUNK_SIZE = cordova.platformId === 'windows' ? totalSize : + // Calculate new chunk size for data URLs to be multiply of 3 + // Otherwise concatenated base64 chunks won't be valid base64 data + FileReader.READ_CHUNK_SIZE - (FileReader.READ_CHUNK_SIZE % 3) + 3; + } + if (typeof r !== "undefined") { accumulate(r); - this._progress = Math.min(this._progress + FileReader.READ_CHUNK_SIZE, totalSize); + this._progress = Math.min(this._progress + CHUNK_SIZE, totalSize); if (typeof this.onprogress === "function") { this.onprogress(new ProgressEvent("progress", {loaded:this._progress, total:totalSize})); @@ -134,7 +144,7 @@ function readSuccessCallback(readType, encoding, offset, totalSize, accumulate, var execArgs = [ this._localURL, offset + this._progress, - offset + this._progress + Math.min(totalSize - this._progress, FileReader.READ_CHUNK_SIZE)]; + offset + this._progress + Math.min(totalSize - this._progress, CHUNK_SIZE)]; if (encoding) { execArgs.splice(1, 0, encoding); } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
