Skip to content

Commit a631753

Browse files
committed
Merge remote-tracking branch 'maxime/CB-6994'
2 parents 1e9587c + 3e38306 commit a631753

File tree

3 files changed

+138
-33
lines changed

3 files changed

+138
-33
lines changed

src/windows/FileProxy.js

Lines changed: 66 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,46 @@ function getFilesystemFromPath(path) {
6161
var getFolderFromPathAsync = Windows.Storage.StorageFolder.getFolderFromPathAsync;
6262
var getFileFromPathAsync = Windows.Storage.StorageFile.getFileFromPathAsync;
6363

64+
var writeBytesAsync = Windows.Storage.FileIO.writeBytesAsync;
65+
var writeTextAsync = Windows.Storage.FileIO.writeTextAsync;
66+
var writeBlobAsync = function writeBlobAsync(storageFile, data) {
67+
return new WinJS.Promise(function (resolve, reject) {
68+
storageFile.openAsync(Windows.Storage.FileAccessMode.readWrite).then(
69+
function (output) {
70+
var input;
71+
if (data.detachStream) {
72+
input = data.detachStream();
73+
}
74+
else {
75+
input = data.msDetachStream();
76+
}
77+
78+
// Copy the stream from the blob to the File stream
79+
Windows.Storage.Streams.RandomAccessStream.copyAsync(input, output).then(
80+
function () {
81+
output.flushAsync().done(
82+
function () {
83+
input.close();
84+
output.close();
85+
86+
resolve(data.length);
87+
},
88+
function () {
89+
reject(FileError.INVALID_MODIFICATION_ERR);
90+
}
91+
);
92+
},
93+
function () {
94+
reject(FileError.INVALID_MODIFICATION_ERR);
95+
}
96+
);
97+
},
98+
function () {
99+
reject(FileError.INVALID_MODIFICATION_ERR);
100+
}
101+
);
102+
});
103+
};
64104

65105
module.exports = {
66106

@@ -516,38 +556,50 @@ module.exports = {
516556
isBinary = args[3];
517557

518558
if (data instanceof ArrayBuffer) {
519-
data = Array.apply(null, new Uint8Array(data));
559+
var dataView = new DataView(data);
560+
data = new Blob([dataView]);
520561
}
521-
522-
var writePromise = isBinary ? Windows.Storage.FileIO.writeBytesAsync : Windows.Storage.FileIO.writeTextAsync;
523562

524-
525563
fileName = fileName.split("/").join("\\");
526564

527-
528565
// split path to folder and file name
529566
var path = fileName.substring(0, fileName.lastIndexOf('\\')),
530567
file = fileName.split('\\').pop();
531-
532568

533569
getFolderFromPathAsync(path).done(
534570
function(storageFolder) {
535571
storageFolder.createFileAsync(file, Windows.Storage.CreationCollisionOption.openIfExists).done(
536572
function(storageFile) {
537-
writePromise(storageFile, data).
538-
done(function () {
573+
var writePromise;
574+
if (data instanceof Blob || data instanceof File) {
575+
writePromise = writeBlobAsync;
576+
}
577+
else if (isBinary) {
578+
writePromise = writeBytesAsync;
579+
}
580+
else {
581+
writePromise = writeTextAsync;
582+
}
583+
584+
writePromise(storageFile, data).done(
585+
function () {
539586
win(data.length);
540-
}, function () {
587+
},
588+
function () {
541589
fail(FileError.INVALID_MODIFICATION_ERR);
542-
});
543-
}, function() {
590+
}
591+
);
592+
},
593+
function () {
544594
fail(FileError.INVALID_MODIFICATION_ERR);
545595
}
546596
);
547-
548-
}, function() {
597+
598+
},
599+
function () {
549600
fail(FileError.NOT_FOUND_ERR);
550-
});
601+
}
602+
);
551603
},
552604

553605
truncate: function (win, fail, args) { // ["fileName","size"]

src/windows8/FileProxy.js

Lines changed: 69 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,46 @@ function getFilesystemFromPath(path) {
6161
var getFolderFromPathAsync = Windows.Storage.StorageFolder.getFolderFromPathAsync;
6262
var getFileFromPathAsync = Windows.Storage.StorageFile.getFileFromPathAsync;
6363

64+
var writeBytesAsync = Windows.Storage.FileIO.writeBytesAsync;
65+
var writeTextAsync = Windows.Storage.FileIO.writeTextAsync;
66+
var writeBlobAsync = function writeBlobAsync(storageFile, data) {
67+
return new WinJS.Promise(function (resolve, reject) {
68+
storageFile.openAsync(Windows.Storage.FileAccessMode.readWrite).then(
69+
function (output) {
70+
var input;
71+
if (data.detachStream) {
72+
input = data.detachStream();
73+
}
74+
else {
75+
input = data.msDetachStream();
76+
}
77+
78+
// Copy the stream from the blob to the File stream
79+
Windows.Storage.Streams.RandomAccessStream.copyAsync(input, output).then(
80+
function () {
81+
output.flushAsync().done(
82+
function () {
83+
input.close();
84+
output.close();
85+
86+
resolve(data.length);
87+
},
88+
function () {
89+
reject(FileError.INVALID_MODIFICATION_ERR);
90+
}
91+
);
92+
},
93+
function () {
94+
reject(FileError.INVALID_MODIFICATION_ERR);
95+
}
96+
);
97+
},
98+
function () {
99+
reject(FileError.INVALID_MODIFICATION_ERR);
100+
}
101+
);
102+
});
103+
};
64104

65105
module.exports = {
66106

@@ -140,7 +180,7 @@ module.exports = {
140180
enc = args[1],
141181
startPos = args[2],
142182
endPos = args[3];
143-
183+
144184
var encoding = Windows.Storage.Streams.UnicodeEncoding.utf8;
145185
if (enc == 'Utf16LE' || enc == 'utf16LE') {
146186
encoding = Windows.Storage.Streams.UnicodeEncoding.utf16LE;
@@ -423,7 +463,7 @@ module.exports = {
423463
completePath = completePath.replace(/\\\\\\/g, '/').replace(/\\\\/g, '\\');
424464

425465
var fileName = completePath.substring(completePath.lastIndexOf('\\'));
426-
466+
427467
//final adjustment
428468
fullPath = completePath.substring(0, completePath.lastIndexOf('\\'));
429469
path = fileName.replace(/\\/g, '');
@@ -516,38 +556,50 @@ module.exports = {
516556
isBinary = args[3];
517557

518558
if (data instanceof ArrayBuffer) {
519-
data = Array.apply(null, new Uint8Array(data));
559+
var dataView = new DataView(data);
560+
data = new Blob([dataView]);
520561
}
521-
522-
var writePromise = isBinary ? Windows.Storage.FileIO.writeBytesAsync : Windows.Storage.FileIO.writeTextAsync;
523562

524-
525563
fileName = fileName.split("/").join("\\");
526564

527-
528565
// split path to folder and file name
529566
var path = fileName.substring(0, fileName.lastIndexOf('\\')),
530567
file = fileName.split('\\').pop();
531-
532568

533569
getFolderFromPathAsync(path).done(
534570
function(storageFolder) {
535571
storageFolder.createFileAsync(file, Windows.Storage.CreationCollisionOption.openIfExists).done(
536572
function(storageFile) {
537-
writePromise(storageFile, data).
538-
done(function () {
573+
var writePromise;
574+
if (data instanceof Blob || data instanceof File) {
575+
writePromise = writeBlobAsync;
576+
}
577+
else if (isBinary) {
578+
writePromise = writeBytesAsync;
579+
}
580+
else {
581+
writePromise = writeTextAsync;
582+
}
583+
584+
writePromise(storageFile, data).done(
585+
function () {
539586
win(data.length);
540-
}, function () {
587+
},
588+
function () {
541589
fail(FileError.INVALID_MODIFICATION_ERR);
542-
});
543-
}, function() {
590+
}
591+
);
592+
},
593+
function () {
544594
fail(FileError.INVALID_MODIFICATION_ERR);
545595
}
546596
);
547-
548-
}, function() {
597+
598+
},
599+
function () {
549600
fail(FileError.NOT_FOUND_ERR);
550-
});
601+
}
602+
);
551603
},
552604

553605
truncate: function (win, fail, args) { // ["fileName","size"]
@@ -936,7 +988,7 @@ module.exports = {
936988
}
937989
);
938990
}
939-
991+
940992

941993
};
942994

www/FileWriter.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,11 @@ FileWriter.prototype.write = function(data) {
9999

100100
var that=this;
101101
var supportsBinary = (typeof window.Blob !== 'undefined' && typeof window.ArrayBuffer !== 'undefined');
102+
var isOkForWindows = (cordova.platformId === "windows8" || cordova.platformId === "windows") && (data instanceof Blob || data instanceof File);
102103
var isBinary;
103104

104105
// Check to see if the incoming data is a blob
105-
if (data instanceof File || (supportsBinary && data instanceof Blob)) {
106+
if (!isOkForWindows && (data instanceof File || (supportsBinary && data instanceof Blob))) {
106107
var fileReader = new FileReader();
107108
fileReader.onload = function() {
108109
// Call this method again, with the arraybuffer as argument
@@ -118,7 +119,7 @@ FileWriter.prototype.write = function(data) {
118119

119120
// Mark data type for safer transport over the binary bridge
120121
isBinary = supportsBinary && (data instanceof ArrayBuffer);
121-
if (isBinary && ['windowsphone', 'windows8'].indexOf(cordova.platformId) >= 0) {
122+
if (isBinary && cordova.platformId === "windowsphone") {
122123
// create a plain array, using the keys from the Uint8Array view so that we can serialize it
123124
data = Array.apply(null, new Uint8Array(data));
124125
}

0 commit comments

Comments
 (0)