Optimize FileProxy for windows platforms
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/3e383069 Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/tree/3e383069 Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/diff/3e383069 Branch: refs/heads/master Commit: 3e383069afae0c230986467d80de2043895f06bf Parents: 3d8a5c1 Author: SomaticIT <[email protected]> Authored: Tue Oct 7 18:16:03 2014 +0200 Committer: SomaticIT <[email protected]> Committed: Tue Oct 7 18:16:03 2014 +0200 ---------------------------------------------------------------------- src/windows/FileProxy.js | 95 +++++++++++++++++++++++++----------------- src/windows8/FileProxy.js | 95 +++++++++++++++++++++++++----------------- 2 files changed, 114 insertions(+), 76 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/3e383069/src/windows/FileProxy.js ---------------------------------------------------------------------- diff --git a/src/windows/FileProxy.js b/src/windows/FileProxy.js index 474f044..d466e87 100644 --- a/src/windows/FileProxy.js +++ b/src/windows/FileProxy.js @@ -61,6 +61,46 @@ function getFilesystemFromPath(path) { var getFolderFromPathAsync = Windows.Storage.StorageFolder.getFolderFromPathAsync; var getFileFromPathAsync = Windows.Storage.StorageFile.getFileFromPathAsync; +var writeBytesAsync = Windows.Storage.FileIO.writeBytesAsync; +var writeTextAsync = Windows.Storage.FileIO.writeTextAsync; +var writeBlobAsync = function writeBlobAsync(storageFile, data) { + return new WinJS.Promise(function (resolve, reject) { + storageFile.openAsync(Windows.Storage.FileAccessMode.readWrite).then( + function (output) { + var input; + if (data.detachStream) { + input = data.detachStream(); + } + else { + input = data.msDetachStream(); + } + + // Copy the stream from the blob to the File stream + Windows.Storage.Streams.RandomAccessStream.copyAsync(input, output).then( + function () { + output.flushAsync().done( + function () { + input.close(); + output.close(); + + resolve(data.length); + }, + function () { + reject(FileError.INVALID_MODIFICATION_ERR); + } + ); + }, + function () { + reject(FileError.INVALID_MODIFICATION_ERR); + } + ); + }, + function () { + reject(FileError.INVALID_MODIFICATION_ERR); + } + ); + }); +}; module.exports = { @@ -516,7 +556,8 @@ module.exports = { isBinary = args[3]; if (data instanceof ArrayBuffer) { - data = Array.apply(null, new Uint8Array(data)); + var dataView = new DataView(data); + data = new Blob([dataView]); } fileName = fileName.split("/").join("\\"); @@ -529,47 +570,25 @@ module.exports = { function(storageFolder) { storageFolder.createFileAsync(file, Windows.Storage.CreationCollisionOption.openIfExists).done( function(storageFile) { + var writePromise; if (data instanceof Blob || data instanceof File) { - storageFile.openAsync(Windows.Storage.FileAccessMode.readWrite).done( - function (output) { - var input = data.msDetachStream(); - - // Copy the stream from the blob to the File stream - Windows.Storage.Streams.RandomAccessStream.copyAsync(input, output).then( - function () { - output.flushAsync().done( - function () { - input.close(); - output.close(); - - win(data.length); - }, - function () { - fail(FileError.INVALID_MODIFICATION_ERR); - } - ); - }, - function () { - fail(FileError.INVALID_MODIFICATION_ERR); - } - ); - }, - function () { - fail(FileError.INVALID_MODIFICATION_ERR); - } - ); + writePromise = writeBlobAsync; + } + else if (isBinary) { + writePromise = writeBytesAsync; } else { - var writePromise = isBinary ? Windows.Storage.FileIO.writeBytesAsync : Windows.Storage.FileIO.writeTextAsync; - writePromise(storageFile, data).done( - function () { - win(data.length); - }, - function () { - fail(FileError.INVALID_MODIFICATION_ERR); - } - ); + writePromise = writeTextAsync; } + + writePromise(storageFile, data).done( + function () { + win(data.length); + }, + function () { + fail(FileError.INVALID_MODIFICATION_ERR); + } + ); }, function () { fail(FileError.INVALID_MODIFICATION_ERR); http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/3e383069/src/windows8/FileProxy.js ---------------------------------------------------------------------- diff --git a/src/windows8/FileProxy.js b/src/windows8/FileProxy.js index 08ba1e5..b58eb1d 100644 --- a/src/windows8/FileProxy.js +++ b/src/windows8/FileProxy.js @@ -61,6 +61,46 @@ function getFilesystemFromPath(path) { var getFolderFromPathAsync = Windows.Storage.StorageFolder.getFolderFromPathAsync; var getFileFromPathAsync = Windows.Storage.StorageFile.getFileFromPathAsync; +var writeBytesAsync = Windows.Storage.FileIO.writeBytesAsync; +var writeTextAsync = Windows.Storage.FileIO.writeTextAsync; +var writeBlobAsync = function writeBlobAsync(storageFile, data) { + return new WinJS.Promise(function (resolve, reject) { + storageFile.openAsync(Windows.Storage.FileAccessMode.readWrite).then( + function (output) { + var input; + if (data.detachStream) { + input = data.detachStream(); + } + else { + input = data.msDetachStream(); + } + + // Copy the stream from the blob to the File stream + Windows.Storage.Streams.RandomAccessStream.copyAsync(input, output).then( + function () { + output.flushAsync().done( + function () { + input.close(); + output.close(); + + resolve(data.length); + }, + function () { + reject(FileError.INVALID_MODIFICATION_ERR); + } + ); + }, + function () { + reject(FileError.INVALID_MODIFICATION_ERR); + } + ); + }, + function () { + reject(FileError.INVALID_MODIFICATION_ERR); + } + ); + }); +}; module.exports = { @@ -516,7 +556,8 @@ module.exports = { isBinary = args[3]; if (data instanceof ArrayBuffer) { - data = Array.apply(null, new Uint8Array(data)); + var dataView = new DataView(data); + data = new Blob([dataView]); } fileName = fileName.split("/").join("\\"); @@ -529,47 +570,25 @@ module.exports = { function(storageFolder) { storageFolder.createFileAsync(file, Windows.Storage.CreationCollisionOption.openIfExists).done( function(storageFile) { + var writePromise; if (data instanceof Blob || data instanceof File) { - storageFile.openAsync(Windows.Storage.FileAccessMode.readWrite).done( - function (output) { - var input = data.msDetachStream(); - - // Copy the stream from the blob to the File stream - Windows.Storage.Streams.RandomAccessStream.copyAsync(input, output).then( - function () { - output.flushAsync().done( - function () { - input.close(); - output.close(); - - win(data.length); - }, - function () { - fail(FileError.INVALID_MODIFICATION_ERR); - } - ); - }, - function () { - fail(FileError.INVALID_MODIFICATION_ERR); - } - ); - }, - function () { - fail(FileError.INVALID_MODIFICATION_ERR); - } - ); + writePromise = writeBlobAsync; + } + else if (isBinary) { + writePromise = writeBytesAsync; } else { - var writePromise = isBinary ? Windows.Storage.FileIO.writeBytesAsync : Windows.Storage.FileIO.writeTextAsync; - writePromise(storageFile, data).done( - function () { - win(data.length); - }, - function () { - fail(FileError.INVALID_MODIFICATION_ERR); - } - ); + writePromise = writeTextAsync; } + + writePromise(storageFile, data).done( + function () { + win(data.length); + }, + function () { + fail(FileError.INVALID_MODIFICATION_ERR); + } + ); }, function () { fail(FileError.INVALID_MODIFICATION_ERR); --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
