|
1 | | -/* |
| 1 | +/* |
2 | 2 | * |
3 | 3 | * Licensed to the Apache Software Foundation (ASF) under one |
4 | 4 | * or more contributor license agreements. See the NOTICE file |
@@ -133,29 +133,34 @@ module.exports = { |
133 | 133 | }, |
134 | 134 |
|
135 | 135 | 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 | + } |
138 | 147 |
|
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() { |
156 | 162 | fail && fail(FileError.NOT_FOUND_ERR); |
157 | | - } |
158 | | - ); |
| 163 | + }); |
159 | 164 | }, |
160 | 165 |
|
161 | 166 | readAsDataURL:function(win,fail,args) { |
@@ -222,7 +227,13 @@ module.exports = { |
222 | 227 | function (storageFolder) { |
223 | 228 | win(new DirectoryEntry(storageFolder.name, storageFolder.path)); |
224 | 229 | }, 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 | + }); |
226 | 237 | } |
227 | 238 | ); |
228 | 239 | } |
@@ -399,7 +410,13 @@ module.exports = { |
399 | 410 | function (storageFile) { |
400 | 411 | win(new FileEntry(storageFile.name, storageFile.path)); |
401 | 412 | }, 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 | + }); |
403 | 420 | } |
404 | 421 | ); |
405 | 422 | } |
@@ -439,23 +456,44 @@ module.exports = { |
439 | 456 | }, |
440 | 457 |
|
441 | 458 | 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]; |
445 | 463 |
|
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() { |
452 | 490 | fail && fail(FileError.INVALID_MODIFICATION_ERR); |
453 | 491 | } |
454 | 492 | ); |
| 493 | + |
455 | 494 | }, function() { |
456 | 495 | fail && fail(FileError.NOT_FOUND_ERR); |
457 | | - } |
458 | | - ); |
| 496 | + }); |
459 | 497 | }, |
460 | 498 |
|
461 | 499 | truncate:function(win,fail,args) { // ["fileName","size"] |
@@ -581,8 +619,10 @@ module.exports = { |
581 | 619 | for (var i = 0; i < fileListTop.length; i++) { |
582 | 620 | filePromiseArr.push(fileListTop[i].copyAsync(targetStorageFolder)); |
583 | 621 | } |
584 | | - WinJS.Promise.join(filePromiseArr).then(function () { |
| 622 | + WinJS.Promise.join(filePromiseArr).done(function () { |
585 | 623 | coreCopy(storageFolderTop, complete); |
| 624 | + }, function() { |
| 625 | + fail && fail(FileError.INVALID_MODIFICATION_ERR); |
586 | 626 | }); |
587 | 627 | }); |
588 | 628 | }); |
@@ -809,36 +849,34 @@ module.exports = { |
809 | 849 |
|
810 | 850 | // support for special path start with file:/// |
811 | 851 | 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); |
826 | 853 | } 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 | + } |
840 | 860 | } |
| 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 | + ); |
841 | 878 | } |
| 879 | + |
842 | 880 |
|
843 | 881 | }; |
844 | 882 |
|
|
0 commit comments