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

[2/4] git commit: [Windows8][CB-4442] Added windows 8 support

[Windows8][CB-4442] Added windows 8 support


Project: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file-transfer/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file-transfer/commit/445f737f
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file-transfer/tree/445f737f
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file-transfer/diff/445f737f

Branch: refs/heads/dev
Commit: 445f737f4365278d9c1b4d15561cbf7539c64b27
Parents: 4dbb483
Author: purplecabbage <pu...@gmail.com>
Authored: Tue Jul 30 18:19:25 2013 -0700
Committer: purplecabbage <pu...@gmail.com>
Committed: Tue Jul 30 18:19:25 2013 -0700

----------------------------------------------------------------------
 plugin.xml                        |   7 +++
 www/windows8/FileTransferProxy.js | 110 +++++++++++++++++++++++++++++++++
 2 files changed, 117 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugin-file-transfer/blob/445f737f/plugin.xml
----------------------------------------------------------------------
diff --git a/plugin.xml b/plugin.xml
index 97d1ca2..8f7b35a 100644
--- a/plugin.xml
+++ b/plugin.xml
@@ -90,4 +90,11 @@
 
     </platform>
 
+    <!-- windows8 -->
+    <platform name="windows8">
+        <js-module src="www/windows8/FileTransferProxy.js" name="FileTransferProxy">
+            <clobbers target="" />
+        </js-module>
+    </platform>     
+
 </plugin>

http://git-wip-us.apache.org/repos/asf/cordova-plugin-file-transfer/blob/445f737f/www/windows8/FileTransferProxy.js
----------------------------------------------------------------------
diff --git a/www/windows8/FileTransferProxy.js b/www/windows8/FileTransferProxy.js
new file mode 100644
index 0000000..dd8ff62
--- /dev/null
+++ b/www/windows8/FileTransferProxy.js
@@ -0,0 +1,110 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+*/
+
+
+var FileTransferError = require('./FileTransferError'),
+    FileUploadResult = require('org.apache.cordova.core.file.FileUploadResult'),
+    FileEntry = require('org.apache.cordova.core.file.FileEntry');
+
+module.exports = {
+
+    upload:function(successCallback, error, 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 = Windows.Storage.ApplicationData.current.localFolder.path + 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(win, error, 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 = Windows.Storage.ApplicationData.current.localFolder.path + 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);
+                });
+            });
+        });
+    }
+};
+
+require("cordova/commandProxy").add("FileTransfer",module.exports);
\ No newline at end of file