incubator-callback-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From purplecabb...@apache.org
Subject [40/45] js commit: Add FileTransferProxy.js
Date Sat, 06 Oct 2012 09:19:21 GMT
Add FileTransferProxy.js


Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/commit/bb85ff1d
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/tree/bb85ff1d
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/diff/bb85ff1d

Branch: refs/heads/master
Commit: bb85ff1dbe1de1e011f0692bea1ed872942c7c8b
Parents: 60c0fbb
Author: mingfeng <mingfengwang01@gmail.com>
Authored: Wed Aug 29 09:38:27 2012 +0800
Committer: mingfeng <mingfengwang01@gmail.com>
Committed: Wed Aug 29 09:38:27 2012 +0800

----------------------------------------------------------------------
 lib/windows8/exec.js                              |    2 +
 lib/windows8/plugin/windows8/FileProxy.js         |   23 ++--
 lib/windows8/plugin/windows8/FileTransferProxy.js |   87 ++++++++++++++++
 3 files changed, 100 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/bb85ff1d/lib/windows8/exec.js
----------------------------------------------------------------------
diff --git a/lib/windows8/exec.js b/lib/windows8/exec.js
index 60bee46..759798c 100644
--- a/lib/windows8/exec.js
+++ b/lib/windows8/exec.js
@@ -32,9 +32,11 @@ var CommandProxy  = {
     "Compass":require('cordova/plugin/windows8/CompassProxy'),
     "Device":require('cordova/plugin/windows8/DeviceProxy'),
     "File":require('cordova/plugin/windows8/FileProxy'),
+    "FileTransfer":require('cordova/plugin/windows8/FileTransferProxy'),
     "Media":require('cordova/plugin/windows8/MediaProxy'),
     "NetworkStatus":require('cordova/plugin/windows8/NetworkStatusProxy'),
     "Notification":require('cordova/plugin/windows8/NotificationProxy')
+    
 };
 
 module.exports = function(success, fail, service, action, args) {

http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/bb85ff1d/lib/windows8/plugin/windows8/FileProxy.js
----------------------------------------------------------------------
diff --git a/lib/windows8/plugin/windows8/FileProxy.js b/lib/windows8/plugin/windows8/FileProxy.js
index 86f7af3..6ceef36 100644
--- a/lib/windows8/plugin/windows8/FileProxy.js
+++ b/lib/windows8/plugin/windows8/FileProxy.js
@@ -28,7 +28,7 @@ module.exports = { //Merges with common
 		// this.isDirectory
 		// this.isFile
 		if (this.isFile) {
-			Windows.Storage.StorageFile.getFilFromPathAsync(this.fullPath).done(
+			Windows.Storage.StorageFile.getFilFromPathAsync(args[0]).done(
 				function (storageFile) {
 					storageFile.getBasicPropertiesAsync().then(
 						function (basicProperties) {
@@ -65,9 +65,9 @@ module.exports = { //Merges with common
     },
 
     getParent:function(win,fail,args) { // ["fullPath"]
-        //var fullPath = args[0];
+        var fullPath = args[0];
 
-        var fullPath = this.fullPath;
+        
     
 	    var storageFolderPer = Windows.Storage.ApplicationData.current.localFolder;
 	    var storageFolderTem = Windows.Storage.ApplicationData.current.temporaryFolder;
@@ -154,7 +154,7 @@ module.exports = { //Merges with common
 
 		path = String(path).split(" ").join("\ ");
 
-		Windows.Storage.StorageFolder.getFolderFromPathAsync(this.fullPath).then(
+		Windows.Storage.StorageFolder.getFolderFromPathAsync(args[0]).then(
 			function (storageFolder) {
 				if (flag.create == true && flag.exclusive == true) {
 					storageFolder.createFolderAsync(path, Windows.Storage.CreationCollisionOption.failIfExists).done(
@@ -193,12 +193,12 @@ module.exports = { //Merges with common
     },
 
     remove:function(win,fail,args) {
-        // this.fullPath
+        var fullPath = args[0];
         // this.isDirectory
         // this.isFile
 
         if (this.isFile) {
-	        Windows.Storage.StorageFile.getFileFromPathAsync(this.fullPath).done(function (storageFile)
{
+	        Windows.Storage.StorageFile.getFileFromPathAsync(fullPath).done(function (storageFile)
{
 	            storageFile.deleteAsync().done(successCallback, function () {
 	                fail(FileError.INVALID_MODIFICATION_ERR);
 
@@ -207,7 +207,6 @@ module.exports = { //Merges with common
     	}
 	    if (this.isDirectory) {
 	       
-	        var fullPath = this.fullPath;
 	        var removeEntry = function () {
 	            var storageFolder = null;
 	            
@@ -307,7 +306,7 @@ module.exports = { //Merges with common
     },
 
     getFile:function(win,fail,args) {
-        // this.fullPath
+        var fullPath = args[0];
 		var path = args[1];
         var options = args[2];
 
@@ -320,7 +319,7 @@ module.exports = { //Merges with common
 
     	path = String(path).split(" ").join("\ ");
 
-    	Windows.Storage.StorageFolder.getFolderFromPathAsync(this.fullPath).then(
+    	Windows.Storage.StorageFolder.getFolderFromPathAsync(fullPath).then(
 			function (storageFolder) {
 		        if (flag.create == true && flag.exclusive == true) {
 		            storageFolder.createFileAsync(path, Windows.Storage.CreationCollisionOption.failIfExists).done(
@@ -686,8 +685,8 @@ module.exports = { //Merges with common
     tempFileSystem:null,
     persistentFileSystem:null,
     requestFileSystem:function(win,fail,args) {
-		// type
-		// size
+		var type = args[0];
+		var size = args[1];
 
 		var filePath = "";
 		var result = null;
@@ -716,7 +715,7 @@ module.exports = { //Merges with common
     },
 
     resolveLocalFileSystemURI:function(win,fail,args) {
-		// uri
+		var uri = args[0];
 
 		var path = uri;
 		path = path.split(" ").join("\ ");

http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/bb85ff1d/lib/windows8/plugin/windows8/FileTransferProxy.js
----------------------------------------------------------------------
diff --git a/lib/windows8/plugin/windows8/FileTransferProxy.js b/lib/windows8/plugin/windows8/FileTransferProxy.js
new file mode 100644
index 0000000..60d7ccd
--- /dev/null
+++ b/lib/windows8/plugin/windows8/FileTransferProxy.js
@@ -0,0 +1,87 @@
+´╗┐var FileTransferError = require('cordova/plugin/FileTransferError');
+var FileUploadResult = require('cordova/plugin/FileUploadResult');
+
+module.exports = {
+    
+    upload:function(successCallback, errorCallback, options) {
+        var filePath = options[0];
+        var server = options[1];
+
+
+        var win = function (fileUploadResult) {
+            successCallback(fileUploadResult);
+        }
+
+        if (filePath == null || typeof filePath == 'undefined') {
+            error(FileTransferError.FILE_NOT_FOUND_ERR);
+            return;
+        }
+
+        if (String(filePath).substr(0, 8) == "file:///") {
+            filePath = FileSystemPersistentRoot + String(filePath).substr(8).split("/").join("\\");
+        }
+        
+        Windows.Storage.StorageFile.getFileFromPathAsync(filePath).then(function (storageFile)
{
+            storageFile.openAsync(Windows.Storage.FileAccessMode.read).then(function (stream)
{
+                var blob = MSApp.createBlobFromRandomAccessStream(storageFile.contentType,
stream);
+                var formData = new FormData();
+                formData.append("source\";filename=\"" + storageFile.name + "\"", blob);
+                WinJS.xhr({ type: "POST", url: server, data: formData }).then(function (response)
{
+                    var code = response.status;
+                    storageFile.getBasicPropertiesAsync().done(function (basicProperties)
{
+
+                        Windows.Storage.FileIO.readBufferAsync(storageFile).done(function
(buffer) {
+                            var dataReader = Windows.Storage.Streams.DataReader.fromBuffer(buffer);
+                            var fileContent = dataReader.readString(buffer.length);
+                            dataReader.close();
+                            win(new FileUploadResult(basicProperties.size, code, fileContent));
+
+                        })
+
+                    })
+                }, function () {
+                    error(FileTransferError.INVALID_URL_ERR);
+                })
+            })
+            
+        },function(){error(FileTransferError.FILE_NOT_FOUND_ERR);})
+    },
+
+    download:function(successCallback, errorCallback, options) {
+        var source = options[0];
+        var target = options[1];
+
+
+        if (target == null || typeof target == undefined) {
+            error(FileTransferError.FILE_NOT_FOUND_ERR);
+            return;
+        }
+        if (String(target).substr(0, 8) == "file:///") {
+            target = FileSystemPersistentRoot + String(target).substr(8).split("/").join("\\");
+        }
+        var path = target.substr(0, String(target).lastIndexOf("\\"));
+        var fileName = target.substr(String(target).lastIndexOf("\\") + 1);
+        if (path == null || fileName == null) {
+            error(FileTransferError.FILE_NOT_FOUND_ERR);
+            return;
+        }
+
+        var download = null;
+      
+        
+        Windows.Storage.StorageFolder.getFolderFromPathAsync(path).then(function (storageFolder)
{
+            storageFolder.createFileAsync(fileName, Windows.Storage.CreationCollisionOption.generateUniqueName).then(function
(storageFile) {
+                var uri = Windows.Foundation.Uri(source);
+                var downloader = new Windows.Networking.BackgroundTransfer.BackgroundDownloader();
+                download = downloader.createDownload(uri, storageFile);
+                download.startAsync().then(function () {
+                    win(new FileEntry(storageFile.name, storageFile.path));
+                }, function () {
+                    error(FileTransferError.INVALID_URL_ERR);
+                });
+            })
+        })
+    }
+
+    
+}    


Mime
View raw message