Skip to content

Commit 5c253ba

Browse files
committed
2 parents 9d8db84 + 5c60410 commit 5c253ba

File tree

2 files changed

+73
-13
lines changed

2 files changed

+73
-13
lines changed

src/windows/FileProxy.js

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,49 @@ utils.extend(DirectoryEntry, Entry);
5252
var getFolderFromPathAsync = Windows.Storage.StorageFolder.getFolderFromPathAsync;
5353
var getFileFromPathAsync = Windows.Storage.StorageFile.getFileFromPathAsync;
5454

55-
var writeBytesAsync = Windows.Storage.FileIO.writeBytesAsync;
56-
var writeTextAsync = Windows.Storage.FileIO.writeTextAsync;
57-
var writeBlobAsync = function writeBlobAsync(storageFile, data) {
55+
function writeBytesAsync(storageFile, data, position) {
5856
return storageFile.openAsync(Windows.Storage.FileAccessMode.readWrite)
5957
.then(function (output) {
58+
output.seek(position);
59+
var dataWriter = new Windows.Storage.Streams.DataWriter(output);
60+
dataWriter.writeBytes(data);
61+
return dataWriter.storeAsync().then(function (size) {
62+
output.size = position+size;
63+
return dataWriter.flushAsync().then(function() {
64+
output.close();
65+
return size;
66+
});
67+
});
68+
});
69+
}
70+
71+
function writeTextAsync(storageFile, data, position) {
72+
return storageFile.openAsync(Windows.Storage.FileAccessMode.readWrite)
73+
.then(function (output) {
74+
output.seek(position);
75+
var dataWriter = new Windows.Storage.Streams.DataWriter(output);
76+
dataWriter.writeString(data);
77+
return dataWriter.storeAsync().then(function (size) {
78+
output.size = position+size;
79+
return dataWriter.flushAsync().then(function() {
80+
output.close();
81+
return size;
82+
});
83+
});
84+
});
85+
}
86+
87+
function writeBlobAsync(storageFile, data, position) {
88+
return storageFile.openAsync(Windows.Storage.FileAccessMode.readWrite)
89+
.then(function (output) {
90+
output.seek(position);
6091
var dataSize = data.size;
6192
var input = (data.detachStream || data.msDetachStream).call(data);
6293

6394
// Copy the stream from the blob to the File stream
6495
return Windows.Storage.Streams.RandomAccessStream.copyAsync(input, output)
6596
.then(function () {
97+
output.size = position+dataSize;
6698
return output.flushAsync().then(function () {
6799
input.close();
68100
output.close();
@@ -71,11 +103,11 @@ var writeBlobAsync = function writeBlobAsync(storageFile, data) {
71103
});
72104
});
73105
});
74-
};
106+
}
75107

76-
var writeArrayBufferAsync = function writeArrayBufferAsync(storageFile, data) {
77-
return writeBlobAsync(storageFile, new Blob([data]));
78-
};
108+
function writeArrayBufferAsync(storageFile, data, position) {
109+
return writeBlobAsync(storageFile, new Blob([data]), position);
110+
}
79111

80112
function cordovaPathToNative(path) {
81113
// turn / into \\
@@ -996,7 +1028,7 @@ module.exports = {
9961028
function (storageFolder) {
9971029
storageFolder.createFileAsync(fileName, Windows.Storage.CreationCollisionOption.openIfExists).done(
9981030
function (storageFile) {
999-
writePromise(storageFile, data).done(
1031+
writePromise(storageFile, data, position).done(
10001032
function (bytesWritten) {
10011033
var written = bytesWritten || data.length;
10021034
win(written);

tests/tests.js

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2389,6 +2389,7 @@ exports.defineAutoTests = function () {
23892389
it("file.spec.96 should be able to write and append to file, createWriter", function (done) {
23902390
var fileName = "writer.append.createWriter", // file content
23912391
content = "There is an exception to every rule.", // for checkin file length
2392+
exception = " Except this one.",
23922393
length = content.length;
23932394
// create file, then write and append to it
23942395
createFile(fileName, function (fileEntry) {
@@ -2399,7 +2400,6 @@ exports.defineAutoTests = function () {
23992400
expect(writer.length).toBe(length);
24002401
expect(writer.position).toBe(length);
24012402
// Append some more data
2402-
var exception = " Except this one.";
24032403
writer.onwriteend = secondVerifier;
24042404
length += exception.length;
24052405
writer.seek(writer.length);
@@ -2408,6 +2408,13 @@ exports.defineAutoTests = function () {
24082408
secondVerifier = function (evt) {
24092409
expect(writer.length).toBe(length);
24102410
expect(writer.position).toBe(length);
2411+
var reader = new FileReader();
2412+
reader.onloadend = thirdVerifier;
2413+
reader.onerror = failed.bind(null, done, 'reader.onerror - Error reading file: ' + fileName);
2414+
fileEntry.file(function(f){reader.readAsText(f);});
2415+
},
2416+
thirdVerifier = function (evt) {
2417+
expect(evt.target.result).toBe(content+exception);
24112418
// cleanup
24122419
deleteFile(fileName, done);
24132420
};
@@ -2419,7 +2426,8 @@ exports.defineAutoTests = function () {
24192426
});
24202427
it("file.spec.97 should be able to write and append to file, File object", function (done) {
24212428
var fileName = "writer.append.File", // file content
2422-
content = "There is an exception to every rule.", // for checking file length
2429+
content = "There is an exception to every rule.", // for checkin file length
2430+
exception = " Except this one.",
24232431
length = content.length;
24242432
root.getFile(fileName, {
24252433
create : true
@@ -2430,7 +2438,6 @@ exports.defineAutoTests = function () {
24302438
expect(writer.length).toBe(length);
24312439
expect(writer.position).toBe(length);
24322440
// Append some more data
2433-
var exception = " Except this one.";
24342441
writer.onwriteend = secondVerifier;
24352442
length += exception.length;
24362443
writer.seek(writer.length);
@@ -2439,6 +2446,13 @@ exports.defineAutoTests = function () {
24392446
secondVerifier = function () {
24402447
expect(writer.length).toBe(length);
24412448
expect(writer.position).toBe(length);
2449+
var reader = new FileReader();
2450+
reader.onloadend = thirdVerifier;
2451+
reader.onerror = failed.bind(null, done, 'reader.onerror - Error reading file: ' + fileName);
2452+
fileEntry.file(function(f){reader.readAsText(f);});
2453+
},
2454+
thirdVerifier = function (evt) {
2455+
expect(evt.target.result).toBe(content+exception);
24422456
// cleanup
24432457
deleteFile(fileName, done);
24442458
};
@@ -2451,6 +2465,7 @@ exports.defineAutoTests = function () {
24512465
it("file.spec.98 should be able to seek to the middle of the file and write more data than file.length", function (done) {
24522466
var fileName = "writer.seek.write", // file content
24532467
content = "This is our sentence.", // for checking file length
2468+
exception = "newer sentence.",
24542469
length = content.length;
24552470
// create file, then write and append to it
24562471
createFile(fileName, function (fileEntry) {
@@ -2460,7 +2475,6 @@ exports.defineAutoTests = function () {
24602475
expect(writer.length).toBe(length);
24612476
expect(writer.position).toBe(length);
24622477
// Append some more data
2463-
var exception = "newer sentence.";
24642478
writer.onwriteend = secondVerifier;
24652479
length = 12 + exception.length;
24662480
writer.seek(12);
@@ -2469,6 +2483,13 @@ exports.defineAutoTests = function () {
24692483
secondVerifier = function (evt) {
24702484
expect(writer.length).toBe(length);
24712485
expect(writer.position).toBe(length);
2486+
var reader = new FileReader();
2487+
reader.onloadend = thirdVerifier;
2488+
reader.onerror = failed.bind(null, done, 'reader.onerror - Error reading file: ' + fileName);
2489+
fileEntry.file(function(f){reader.readAsText(f);});
2490+
},
2491+
thirdVerifier = function (evt) {
2492+
expect(evt.target.result).toBe(content.substr(0,12)+exception);
24722493
// cleanup
24732494
deleteFile(fileName, done);
24742495
};
@@ -2487,6 +2508,7 @@ exports.defineAutoTests = function () {
24872508

24882509
var fileName = "writer.seek.write2", // file content
24892510
content = "This is our sentence.", // for checking file length
2511+
exception = "new.",
24902512
length = content.length;
24912513
// create file, then write and append to it
24922514
createFile(fileName, function (fileEntry) {
@@ -2496,7 +2518,6 @@ exports.defineAutoTests = function () {
24962518
expect(writer.length).toBe(length);
24972519
expect(writer.position).toBe(length);
24982520
// Append some more data
2499-
var exception = "new.";
25002521
writer.onwriteend = secondVerifier;
25012522
length = 8 + exception.length;
25022523
writer.seek(8);
@@ -2505,6 +2526,13 @@ exports.defineAutoTests = function () {
25052526
secondVerifier = function (evt) {
25062527
expect(writer.length).toBe(length);
25072528
expect(writer.position).toBe(length);
2529+
var reader = new FileReader();
2530+
reader.onloadend = thirdVerifier;
2531+
reader.onerror = failed.bind(null, done, 'reader.onerror - Error reading file: ' + fileName);
2532+
fileEntry.file(function(f){reader.readAsText(f);});
2533+
},
2534+
thirdVerifier = function (evt) {
2535+
expect(evt.target.result).toBe(content.substr(0,8)+exception);
25082536
// cleanup
25092537
deleteFile(fileName, done);
25102538
};

0 commit comments

Comments
 (0)