CB-6994 Improves merged code to be able to write a File
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/055e7e0b Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/tree/055e7e0b Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/diff/055e7e0b Branch: refs/heads/master Commit: 055e7e0bde7d8580ec6c992b140fa5afbb3eda7e Parents: a631753 Author: sgrebnov <[email protected]> Authored: Wed Oct 8 20:42:59 2014 +0400 Committer: sgrebnov <[email protected]> Committed: Wed Oct 8 20:42:59 2014 +0400 ---------------------------------------------------------------------- src/windows/FileProxy.js | 55 +++++++++++++++++++++++-------------------- www/FileWriter.js | 6 ++--- 2 files changed, 33 insertions(+), 28 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/055e7e0b/src/windows/FileProxy.js ---------------------------------------------------------------------- diff --git a/src/windows/FileProxy.js b/src/windows/FileProxy.js index d466e87..5d1f7ca 100644 --- a/src/windows/FileProxy.js +++ b/src/windows/FileProxy.js @@ -67,13 +67,7 @@ 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(); - } + var input = (data.detachStream || data.msDetachStream).call(data); // Copy the stream from the blob to the File stream Windows.Storage.Streams.RandomAccessStream.copyAsync(input, output).then( @@ -102,6 +96,10 @@ var writeBlobAsync = function writeBlobAsync(storageFile, data) { }); }; +var writeArrayBufferAsync = function writeArrayBufferAsync(storageFile, data) { + return writeBlobAsync(storageFile, new Blob([data])); +}; + module.exports = { getFileMetadata: function (success, fail, args) { @@ -555,32 +553,39 @@ module.exports = { position = args[2], isBinary = args[3]; - if (data instanceof ArrayBuffer) { - var dataView = new DataView(data); - data = new Blob([dataView]); - } - fileName = fileName.split("/").join("\\"); // split path to folder and file name var path = fileName.substring(0, fileName.lastIndexOf('\\')), file = fileName.split('\\').pop(); + function getWriteMethodForData(data, isBinary) { + + if (data instanceof Blob) { + return writeBlobAsync; + } + + if (data instanceof ArrayBuffer) { + return writeArrayBufferAsync; + } + + if (isBinary) { + return writeBytesAsync; + } + + if (typeof data === 'string') { + return writeTextAsync; + } + + throw new Error('Unsupported data type for write method'); + } + + var writePromise = getWriteMethodForData(data, isBinary); + getFolderFromPathAsync(path).done( - function(storageFolder) { + function (storageFolder) { storageFolder.createFileAsync(file, Windows.Storage.CreationCollisionOption.openIfExists).done( - function(storageFile) { - var writePromise; - if (data instanceof Blob || data instanceof File) { - writePromise = writeBlobAsync; - } - else if (isBinary) { - writePromise = writeBytesAsync; - } - else { - writePromise = writeTextAsync; - } - + function (storageFile) { writePromise(storageFile, data).done( function () { win(data.length); http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/055e7e0b/www/FileWriter.js ---------------------------------------------------------------------- diff --git a/www/FileWriter.js b/www/FileWriter.js index f009107..7f53c0c 100644 --- a/www/FileWriter.js +++ b/www/FileWriter.js @@ -1,4 +1,4 @@ -/* +/* * * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -99,11 +99,11 @@ FileWriter.prototype.write = function(data) { var that=this; var supportsBinary = (typeof window.Blob !== 'undefined' && typeof window.ArrayBuffer !== 'undefined'); - var isOkForWindows = (cordova.platformId === "windows8" || cordova.platformId === "windows") && (data instanceof Blob || data instanceof File); + var isProxySupportBlobNatively = (cordova.platformId === "windows8" || cordova.platformId === "windows"); var isBinary; // Check to see if the incoming data is a blob - if (!isOkForWindows && (data instanceof File || (supportsBinary && data instanceof Blob))) { + if (data instanceof File || (!isProxySupportBlobNatively && supportsBinary && data instanceof Blob)) { var fileReader = new FileReader(); fileReader.onload = function() { // Call this method again, with the arraybuffer as argument --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
