You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by pu...@apache.org on 2014/01/08 02:29:01 UTC

[1/2] git commit: CB-5602 Windows8. Fix File Api mobile spec tests

Updated Branches:
  refs/heads/dev 68b29ccb4 -> 51be28eeb


CB-5602 Windows8. Fix File Api mobile spec tests


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/2de4d8d4
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/tree/2de4d8d4
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/diff/2de4d8d4

Branch: refs/heads/dev
Commit: 2de4d8d4727d3164c782ce20b3ec83824bb3d0d1
Parents: cfdb4ed
Author: sgrebnov <se...@gmail.com>
Authored: Mon Dec 9 15:09:06 2013 +0400
Committer: sgrebnov <se...@gmail.com>
Committed: Mon Dec 9 15:09:06 2013 +0400

----------------------------------------------------------------------
 src/windows8/FileProxy.js         | 164 ++++++++++++++++++++-------------
 test/autotest/tests/file.tests.js |   8 +-
 2 files changed, 105 insertions(+), 67 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/2de4d8d4/src/windows8/FileProxy.js
----------------------------------------------------------------------
diff --git a/src/windows8/FileProxy.js b/src/windows8/FileProxy.js
index 1445ae7..0fa16d2 100644
--- a/src/windows8/FileProxy.js
+++ b/src/windows8/FileProxy.js
@@ -1,4 +1,4 @@
-/*
+/*
  *
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file
@@ -133,29 +133,34 @@ module.exports = {
     },
 
     readAsText:function(win,fail,args) {
-        var fileName = args[0];
-        var enc = args[1];
+        var fileName = args[0],
+            enc = args[1],
+            startPos = args[2],
+            endPos = args[3];
+        
+        var encoding = Windows.Storage.Streams.UnicodeEncoding.utf8;
+        if (enc == 'Utf16LE' || enc == 'utf16LE') {
+            encoding = Windows.Storage.Streams.UnicodeEncoding.utf16LE;
+        } else if (enc == 'Utf16BE' || enc == 'utf16BE') {
+            encoding = Windows.Storage.Streams.UnicodeEncoding.utf16BE;
+        }
 
-        Windows.Storage.StorageFile.getFileFromPathAsync(fileName).done(
-            function (storageFile) {
-                var value = Windows.Storage.Streams.UnicodeEncoding.utf8;
-                if (enc == 'Utf16LE' || enc == 'utf16LE') {
-                    value = Windows.Storage.Streams.UnicodeEncoding.utf16LE;
-                }else if (enc == 'Utf16BE' || enc == 'utf16BE') {
-                    value = Windows.Storage.Streams.UnicodeEncoding.utf16BE;
-                }
-                Windows.Storage.FileIO.readTextAsync(storageFile, value).done(
-                    function (fileContent) {
-                        win(fileContent);
-                    },
-                    function () {
-                        fail && fail(FileError.ENCODING_ERR);
-                    }
-                );
-            }, function () {
+        Windows.Storage.StorageFile.getFileFromPathAsync(fileName).then(function(file) {
+                return file.openReadAsync();
+            }).then(function (stream) {
+                startPos = (startPos < 0) ? Math.max(stream.size + startPos, 0) : Math.min(stream.size, startPos);
+                endPos = (endPos < 0) ? Math.max(endPos + stream.size, 0) : Math.min(stream.size, endPos);
+                stream.seek(startPos);
+                
+                var readSize = endPos - startPos,
+                    buffer = new Windows.Storage.Streams.Buffer(readSize);
+
+                return stream.readAsync(buffer, readSize, Windows.Storage.Streams.InputStreamOptions.none);
+            }).done(function(buffer) {
+                win(Windows.Security.Cryptography.CryptographicBuffer.convertBinaryToString(encoding, buffer));
+            },function() {
                 fail && fail(FileError.NOT_FOUND_ERR);
-            }
-        );
+            });
     },
 
     readAsDataURL:function(win,fail,args) {
@@ -222,7 +227,13 @@ module.exports = {
                         function (storageFolder) {
                             win(new DirectoryEntry(storageFolder.name, storageFolder.path));
                         }, function () {
-                            fail && fail(FileError.NOT_FOUND_ERR);
+                            // check if path actually points to a file
+                            storageFolder.getFileAsync(path).done(
+                                function () {
+                                    fail && fail(FileError.TYPE_MISMATCH_ERR);
+                                }, function() {
+                                    fail && fail(FileError.NOT_FOUND_ERR);
+                                });
                         }
                     );
                 }
@@ -399,7 +410,13 @@ module.exports = {
                         function (storageFile) {
                             win(new FileEntry(storageFile.name, storageFile.path));
                         }, function () {
-                            fail && fail(FileError.NOT_FOUND_ERR);
+                            // check if path actually points to a folder
+                            storageFolder.getFolderAsync(path).done(
+                                function () {
+                                    fail && fail(FileError.TYPE_MISMATCH_ERR);
+                                }, function () {
+                                    fail && fail(FileError.NOT_FOUND_ERR);
+                                });
                         }
                     );
                 }
@@ -439,23 +456,44 @@ module.exports = {
     },
 
     write:function(win,fail,args) {
-        var fileName = args[0];
-        var text = args[1];
-        var position = args[2];
+        var fileName = args[0],
+            data = args[1],
+            position = args[2],
+            isBinary = args[3];
 
-        Windows.Storage.StorageFile.getFileFromPathAsync(fileName).done(
-            function (storageFile) {
-                Windows.Storage.FileIO.writeTextAsync(storageFile,text,Windows.Storage.Streams.UnicodeEncoding.utf8).done(
-                    function() {
-                        win(String(text).length);
-                    }, function () {
+        if (data instanceof ArrayBuffer) {
+            data = Array.apply(null, new Uint8Array(data));
+        }
+        
+        var writePromise = isBinary ? Windows.Storage.FileIO.writeBytesAsync : Windows.Storage.FileIO.writeTextAsync;
+
+        
+        fileName = fileName.split("/").join("\\");
+
+
+        // split path to folder and file name
+        var path = fileName.substring(0, fileName.lastIndexOf('\\')),
+            file = fileName.split('\\').pop();
+        
+
+        Windows.Storage.StorageFolder.getFolderFromPathAsync(path).done(
+            function(storageFolder) {
+                storageFolder.createFileAsync(file, Windows.Storage.CreationCollisionOption.openIfExists).done(
+                    function(storageFile) {
+                        writePromise(storageFile, data).
+                            done(function () {
+                                win(data.length);
+                            }, function () {
+                                fail && fail(FileError.INVALID_MODIFICATION_ERR);
+                            });
+                    }, function() {
                         fail && fail(FileError.INVALID_MODIFICATION_ERR);
                     }
                 );
+                
             }, function() {
                 fail && fail(FileError.NOT_FOUND_ERR);
-            }
-        );
+            });
     },
 
     truncate:function(win,fail,args) { // ["fileName","size"]
@@ -581,8 +619,10 @@ module.exports = {
                                     for (var i = 0; i < fileListTop.length; i++) {
                                         filePromiseArr.push(fileListTop[i].copyAsync(targetStorageFolder));
                                     }
-                                    WinJS.Promise.join(filePromiseArr).then(function () {
+                                    WinJS.Promise.join(filePromiseArr).done(function () {
                                         coreCopy(storageFolderTop, complete);
+                                    }, function() {
+                                        fail && fail(FileError.INVALID_MODIFICATION_ERR);
                                     });
                                 });
                             });
@@ -809,36 +849,34 @@ module.exports = {
 
         // support for special path start with file:///
         if (path.substr(0, 8) == "file:///") {
-            path = Windows.Storage.ApplicationData.current.localFolder.path + "\\" + String(path).substr(8).split("/").join("\\");
-            Windows.Storage.StorageFile.getFileFromPathAsync(path).then(
-                function (storageFile) {
-                    success(new FileEntry(storageFile.name, storageFile.path));
-                }, function () {
-                    Windows.Storage.StorageFolder.getFolderFromPathAsync(path).then(
-                        function (storageFolder) {
-                            success(new DirectoryEntry(storageFolder.name, storageFolder.path));
-                        }, function () {
-                            fail && fail(FileError.NOT_FOUND_ERR);
-                        }
-                    );
-                }
-            );
+            path = Windows.Storage.ApplicationData.current.localFolder.path + "\\" + String(path).substr(8);
         } else {
-            Windows.Storage.StorageFile.getFileFromPathAsync(path).then(
-                function (storageFile) {
-                    success(new FileEntry(storageFile.name, storageFile.path));
-                }, function () {
-                    Windows.Storage.StorageFolder.getFolderFromPathAsync(path).then(
-                        function (storageFolder) {
-                            success(new DirectoryEntry(storageFolder.name, storageFolder.path));
-                        }, function () {
-                            fail && fail(FileError.ENCODING_ERR);
-                        }
-                    );
-                }
-            );
+            // method should not let read files outside of the [APP HASH]/Local or [APP HASH]/temp folders
+            if (path.indexOf(Windows.Storage.ApplicationData.current.temporaryFolder.path) != 0 &&
+                path.indexOf(Windows.Storage.ApplicationData.current.localFolder.path) != 0) {
+                fail && fail(FileError.ENCODING_ERR);
+                return;
+            }
         }
+        
+        // refine path format to make sure it is correct
+        path = path.split("/").join("\\");
+
+        Windows.Storage.StorageFile.getFileFromPathAsync(path).then(
+            function (storageFile) {
+                success(new FileEntry(storageFile.name, storageFile.path));
+            }, function () {
+                Windows.Storage.StorageFolder.getFolderFromPathAsync(path).then(
+                    function (storageFolder) {
+                        success(new DirectoryEntry(storageFolder.name, storageFolder.path));
+                    }, function () {
+                        fail && fail(FileError.NOT_FOUND_ERR);
+                    }
+                );
+            }
+        );
     }
+    
 
 };
 

http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/2de4d8d4/test/autotest/tests/file.tests.js
----------------------------------------------------------------------
diff --git a/test/autotest/tests/file.tests.js b/test/autotest/tests/file.tests.js
index 3ebaffc..bfae317 100644
--- a/test/autotest/tests/file.tests.js
+++ b/test/autotest/tests/file.tests.js
@@ -406,7 +406,7 @@ describe('File API', function() {
                     expect(entry.isFile).toBe(true);
                     expect(entry.isDirectory).toBe(false);
                     expect(entry.name).toCanonicallyMatch(fileName);
-                    expect(entry.fullPath).toBe(filePath);
+                    expect(entry.fullPath).toCanonicallyMatch(filePath);
                     // cleanup
                     entry.remove(null, null);
                 }),
@@ -432,7 +432,7 @@ describe('File API', function() {
                     expect(entry.isFile).toBe(true);
                     expect(entry.isDirectory).toBe(false);
                     expect(entry.name).toBe(fileName);
-                    expect(entry.fullPath).toBe(filePath);
+                    expect(entry.fullPath).toCanonicallyMatch(filePath);
 
                     // cleanup
                     entry.remove(null, null);
@@ -473,7 +473,7 @@ describe('File API', function() {
                     expect(entry.isFile).toBe(true);
                     expect(entry.isDirectory).toBe(false);
                     expect(entry.name).toCanonicallyMatch(fileName);
-                    expect(entry.fullPath).toBe(filePath);
+                    expect(entry.fullPath).toCanonicallyMatch(filePath);
 
                     // cleanup
                     entry.remove(null, fail);
@@ -632,7 +632,7 @@ describe('File API', function() {
             waitsFor(function() { return getDir.wasCalled; }, "getDir never called", Tests.TEST_TIMEOUT);
         });
         it("file.spec.25 DirectoryEntry.getDirectory: create new dir with space resolveFileSystemURI with encoded URI", function() {
-            var dirName = "de create dir",
+            var dirName = "de create dir2",
                 dirPath = root.fullPath + '/' + dirName,
                 getDir = jasmine.createSpy().andCallFake(function(dirEntry) {
                     var dirURI = dirEntry.toURL();


[2/2] git commit: Merge branch 'CB-5602' of https://github.com/sgrebnov/cordova-plugin-file into dev

Posted by pu...@apache.org.
Merge branch 'CB-5602' of https://github.com/sgrebnov/cordova-plugin-file into dev


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/51be28ee
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/tree/51be28ee
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/diff/51be28ee

Branch: refs/heads/dev
Commit: 51be28eeb87dfab80624601880ae6a9b8f3dcf06
Parents: 68b29cc 2de4d8d
Author: Jesse MacFadyen <pu...@gmail.com>
Authored: Tue Jan 7 17:26:15 2014 -0800
Committer: Jesse MacFadyen <pu...@gmail.com>
Committed: Tue Jan 7 17:26:15 2014 -0800

----------------------------------------------------------------------
 src/windows8/FileProxy.js         | 164 ++++++++++++++++++++-------------
 test/autotest/tests/file.tests.js |   8 +-
 2 files changed, 105 insertions(+), 67 deletions(-)
----------------------------------------------------------------------