Skip to content

Commit 2de4d8d

Browse files
committed
CB-5602 Windows8. Fix File Api mobile spec tests
1 parent cfdb4ed commit 2de4d8d

File tree

2 files changed

+105
-67
lines changed

2 files changed

+105
-67
lines changed

src/windows8/FileProxy.js

Lines changed: 101 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/*
22
*
33
* Licensed to the Apache Software Foundation (ASF) under one
44
* or more contributor license agreements. See the NOTICE file
@@ -133,29 +133,34 @@ module.exports = {
133133
},
134134

135135
readAsText:function(win,fail,args) {
136-
var fileName = args[0];
137-
var enc = args[1];
136+
var fileName = args[0],
137+
enc = args[1],
138+
startPos = args[2],
139+
endPos = args[3];
140+
141+
var encoding = Windows.Storage.Streams.UnicodeEncoding.utf8;
142+
if (enc == 'Utf16LE' || enc == 'utf16LE') {
143+
encoding = Windows.Storage.Streams.UnicodeEncoding.utf16LE;
144+
} else if (enc == 'Utf16BE' || enc == 'utf16BE') {
145+
encoding = Windows.Storage.Streams.UnicodeEncoding.utf16BE;
146+
}
138147

139-
Windows.Storage.StorageFile.getFileFromPathAsync(fileName).done(
140-
function (storageFile) {
141-
var value = Windows.Storage.Streams.UnicodeEncoding.utf8;
142-
if (enc == 'Utf16LE' || enc == 'utf16LE') {
143-
value = Windows.Storage.Streams.UnicodeEncoding.utf16LE;
144-
}else if (enc == 'Utf16BE' || enc == 'utf16BE') {
145-
value = Windows.Storage.Streams.UnicodeEncoding.utf16BE;
146-
}
147-
Windows.Storage.FileIO.readTextAsync(storageFile, value).done(
148-
function (fileContent) {
149-
win(fileContent);
150-
},
151-
function () {
152-
fail && fail(FileError.ENCODING_ERR);
153-
}
154-
);
155-
}, function () {
148+
Windows.Storage.StorageFile.getFileFromPathAsync(fileName).then(function(file) {
149+
return file.openReadAsync();
150+
}).then(function (stream) {
151+
startPos = (startPos < 0) ? Math.max(stream.size + startPos, 0) : Math.min(stream.size, startPos);
152+
endPos = (endPos < 0) ? Math.max(endPos + stream.size, 0) : Math.min(stream.size, endPos);
153+
stream.seek(startPos);
154+
155+
var readSize = endPos - startPos,
156+
buffer = new Windows.Storage.Streams.Buffer(readSize);
157+
158+
return stream.readAsync(buffer, readSize, Windows.Storage.Streams.InputStreamOptions.none);
159+
}).done(function(buffer) {
160+
win(Windows.Security.Cryptography.CryptographicBuffer.convertBinaryToString(encoding, buffer));
161+
},function() {
156162
fail && fail(FileError.NOT_FOUND_ERR);
157-
}
158-
);
163+
});
159164
},
160165

161166
readAsDataURL:function(win,fail,args) {
@@ -222,7 +227,13 @@ module.exports = {
222227
function (storageFolder) {
223228
win(new DirectoryEntry(storageFolder.name, storageFolder.path));
224229
}, function () {
225-
fail && fail(FileError.NOT_FOUND_ERR);
230+
// check if path actually points to a file
231+
storageFolder.getFileAsync(path).done(
232+
function () {
233+
fail && fail(FileError.TYPE_MISMATCH_ERR);
234+
}, function() {
235+
fail && fail(FileError.NOT_FOUND_ERR);
236+
});
226237
}
227238
);
228239
}
@@ -399,7 +410,13 @@ module.exports = {
399410
function (storageFile) {
400411
win(new FileEntry(storageFile.name, storageFile.path));
401412
}, function () {
402-
fail && fail(FileError.NOT_FOUND_ERR);
413+
// check if path actually points to a folder
414+
storageFolder.getFolderAsync(path).done(
415+
function () {
416+
fail && fail(FileError.TYPE_MISMATCH_ERR);
417+
}, function () {
418+
fail && fail(FileError.NOT_FOUND_ERR);
419+
});
403420
}
404421
);
405422
}
@@ -439,23 +456,44 @@ module.exports = {
439456
},
440457

441458
write:function(win,fail,args) {
442-
var fileName = args[0];
443-
var text = args[1];
444-
var position = args[2];
459+
var fileName = args[0],
460+
data = args[1],
461+
position = args[2],
462+
isBinary = args[3];
445463

446-
Windows.Storage.StorageFile.getFileFromPathAsync(fileName).done(
447-
function (storageFile) {
448-
Windows.Storage.FileIO.writeTextAsync(storageFile,text,Windows.Storage.Streams.UnicodeEncoding.utf8).done(
449-
function() {
450-
win(String(text).length);
451-
}, function () {
464+
if (data instanceof ArrayBuffer) {
465+
data = Array.apply(null, new Uint8Array(data));
466+
}
467+
468+
var writePromise = isBinary ? Windows.Storage.FileIO.writeBytesAsync : Windows.Storage.FileIO.writeTextAsync;
469+
470+
471+
fileName = fileName.split("/").join("\\");
472+
473+
474+
// split path to folder and file name
475+
var path = fileName.substring(0, fileName.lastIndexOf('\\')),
476+
file = fileName.split('\\').pop();
477+
478+
479+
Windows.Storage.StorageFolder.getFolderFromPathAsync(path).done(
480+
function(storageFolder) {
481+
storageFolder.createFileAsync(file, Windows.Storage.CreationCollisionOption.openIfExists).done(
482+
function(storageFile) {
483+
writePromise(storageFile, data).
484+
done(function () {
485+
win(data.length);
486+
}, function () {
487+
fail && fail(FileError.INVALID_MODIFICATION_ERR);
488+
});
489+
}, function() {
452490
fail && fail(FileError.INVALID_MODIFICATION_ERR);
453491
}
454492
);
493+
455494
}, function() {
456495
fail && fail(FileError.NOT_FOUND_ERR);
457-
}
458-
);
496+
});
459497
},
460498

461499
truncate:function(win,fail,args) { // ["fileName","size"]
@@ -581,8 +619,10 @@ module.exports = {
581619
for (var i = 0; i < fileListTop.length; i++) {
582620
filePromiseArr.push(fileListTop[i].copyAsync(targetStorageFolder));
583621
}
584-
WinJS.Promise.join(filePromiseArr).then(function () {
622+
WinJS.Promise.join(filePromiseArr).done(function () {
585623
coreCopy(storageFolderTop, complete);
624+
}, function() {
625+
fail && fail(FileError.INVALID_MODIFICATION_ERR);
586626
});
587627
});
588628
});
@@ -809,36 +849,34 @@ module.exports = {
809849

810850
// support for special path start with file:///
811851
if (path.substr(0, 8) == "file:///") {
812-
path = Windows.Storage.ApplicationData.current.localFolder.path + "\\" + String(path).substr(8).split("/").join("\\");
813-
Windows.Storage.StorageFile.getFileFromPathAsync(path).then(
814-
function (storageFile) {
815-
success(new FileEntry(storageFile.name, storageFile.path));
816-
}, function () {
817-
Windows.Storage.StorageFolder.getFolderFromPathAsync(path).then(
818-
function (storageFolder) {
819-
success(new DirectoryEntry(storageFolder.name, storageFolder.path));
820-
}, function () {
821-
fail && fail(FileError.NOT_FOUND_ERR);
822-
}
823-
);
824-
}
825-
);
852+
path = Windows.Storage.ApplicationData.current.localFolder.path + "\\" + String(path).substr(8);
826853
} else {
827-
Windows.Storage.StorageFile.getFileFromPathAsync(path).then(
828-
function (storageFile) {
829-
success(new FileEntry(storageFile.name, storageFile.path));
830-
}, function () {
831-
Windows.Storage.StorageFolder.getFolderFromPathAsync(path).then(
832-
function (storageFolder) {
833-
success(new DirectoryEntry(storageFolder.name, storageFolder.path));
834-
}, function () {
835-
fail && fail(FileError.ENCODING_ERR);
836-
}
837-
);
838-
}
839-
);
854+
// method should not let read files outside of the [APP HASH]/Local or [APP HASH]/temp folders
855+
if (path.indexOf(Windows.Storage.ApplicationData.current.temporaryFolder.path) != 0 &&
856+
path.indexOf(Windows.Storage.ApplicationData.current.localFolder.path) != 0) {
857+
fail && fail(FileError.ENCODING_ERR);
858+
return;
859+
}
840860
}
861+
862+
// refine path format to make sure it is correct
863+
path = path.split("/").join("\\");
864+
865+
Windows.Storage.StorageFile.getFileFromPathAsync(path).then(
866+
function (storageFile) {
867+
success(new FileEntry(storageFile.name, storageFile.path));
868+
}, function () {
869+
Windows.Storage.StorageFolder.getFolderFromPathAsync(path).then(
870+
function (storageFolder) {
871+
success(new DirectoryEntry(storageFolder.name, storageFolder.path));
872+
}, function () {
873+
fail && fail(FileError.NOT_FOUND_ERR);
874+
}
875+
);
876+
}
877+
);
841878
}
879+
842880

843881
};
844882

test/autotest/tests/file.tests.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ describe('File API', function() {
406406
expect(entry.isFile).toBe(true);
407407
expect(entry.isDirectory).toBe(false);
408408
expect(entry.name).toCanonicallyMatch(fileName);
409-
expect(entry.fullPath).toBe(filePath);
409+
expect(entry.fullPath).toCanonicallyMatch(filePath);
410410
// cleanup
411411
entry.remove(null, null);
412412
}),
@@ -432,7 +432,7 @@ describe('File API', function() {
432432
expect(entry.isFile).toBe(true);
433433
expect(entry.isDirectory).toBe(false);
434434
expect(entry.name).toBe(fileName);
435-
expect(entry.fullPath).toBe(filePath);
435+
expect(entry.fullPath).toCanonicallyMatch(filePath);
436436

437437
// cleanup
438438
entry.remove(null, null);
@@ -473,7 +473,7 @@ describe('File API', function() {
473473
expect(entry.isFile).toBe(true);
474474
expect(entry.isDirectory).toBe(false);
475475
expect(entry.name).toCanonicallyMatch(fileName);
476-
expect(entry.fullPath).toBe(filePath);
476+
expect(entry.fullPath).toCanonicallyMatch(filePath);
477477

478478
// cleanup
479479
entry.remove(null, fail);
@@ -632,7 +632,7 @@ describe('File API', function() {
632632
waitsFor(function() { return getDir.wasCalled; }, "getDir never called", Tests.TEST_TIMEOUT);
633633
});
634634
it("file.spec.25 DirectoryEntry.getDirectory: create new dir with space resolveFileSystemURI with encoded URI", function() {
635-
var dirName = "de create dir",
635+
var dirName = "de create dir2",
636636
dirPath = root.fullPath + '/' + dirName,
637637
getDir = jasmine.createSpy().andCallFake(function(dirEntry) {
638638
var dirURI = dirEntry.toURL();

0 commit comments

Comments
 (0)