From c44678466c6c58d629b8da085be7fe3a0ce9a14b Mon Sep 17 00:00:00 2001 From: Jesse MacFadyen Date: Fri, 12 Jul 2013 18:54:23 -0700 Subject: [PATCH] WIP, fixed param handling --- src/wp/FileTransfer.cs | 104 +++++++++++++++++++++++++++++++++++------ 1 file changed, 91 insertions(+), 13 deletions(-) diff --git a/src/wp/FileTransfer.cs b/src/wp/FileTransfer.cs index e585895..d07bf14 100644 --- a/src/wp/FileTransfer.cs +++ b/src/wp/FileTransfer.cs @@ -49,6 +49,9 @@ namespace WPCordovaClassLib.Cordova.Commands public const int InvalidUrlError = 2; public const int ConnectionError = 3; + private static Dictionary InProcDownloads = new Dictionary(); + + /// /// Options for downloading file /// @@ -66,6 +69,18 @@ namespace WPCordovaClassLib.Cordova.Commands /// [DataMember(Name = "url", IsRequired = true)] public string Url { get; set; } + + [DataMember(Name = "trustAllHosts", IsRequired = true)] + public bool TrustAllHosts { get; set; } + + [DataMember(Name = "id", IsRequired = true)] + public string Id { get; set; } + + [DataMember(Name = "headers", IsRequired = true)] + public string Headers { get; set; } + + [DataMember(Name = "callbackId", IsRequired = true)] + public string CallbackId { get; set; } } /// @@ -115,7 +130,12 @@ namespace WPCordovaClassLib.Cordova.Commands /// Flag to recognize if we should trust every host (only in debug environments) /// [DataMember(Name = "debug")] - public bool Debug { get; set; } + public bool TrustAllHosts { get; set; } + + [DataMember(Name = "callbackId", IsRequired = true)] + public string CallbackId { get; set; } + + public string Method { get; set; } /// /// Creates options object with default parameters @@ -242,17 +262,40 @@ namespace WPCordovaClassLib.Cordova.Commands /// sends a file to a server /// /// Upload options + /// exec(win, fail, 'FileTransfer', 'upload', [filePath, server, fileKey, fileName, mimeType, params, trustAllHosts, chunkedMode, headers, this._id, httpMethod]); public void upload(string options) { Debug.WriteLine("options = " + options); options = options.Replace("{}", "null"); - + string callbackId = ""; try { try { string[] args = JSON.JsonHelper.Deserialize(options); - uploadOptions = JSON.JsonHelper.Deserialize(args[0]); + //uploadOptions = JSON.JsonHelper.Deserialize(args[0]); + uploadOptions = new UploadOptions(); + uploadOptions.FilePath = args[0]; + uploadOptions.Server = args[1]; + uploadOptions.FileKey = args[2]; + uploadOptions.FileName = args[3]; + uploadOptions.MimeType = args[4]; + uploadOptions.Params = args[5]; + + bool trustAll = false; + bool.TryParse(args[6],out trustAll); + uploadOptions.TrustAllHosts = trustAll; + + //uploadOptions.ChunkedMode = args[7]; // TODO: bool + //8 : Headers + //9 : id + //10: method + + //uploadOptions.Headers = args[8]; + //uploadOptions.Id = args[9]; + uploadOptions.Method = args[10]; + + uploadOptions.CallbackId = callbackId = args[11]; } catch (Exception) { @@ -272,12 +315,15 @@ namespace WPCordovaClassLib.Cordova.Commands } HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(serverUri); webRequest.ContentType = "multipart/form-data;boundary=" + Boundary; - webRequest.Method = "POST"; + webRequest.Method = uploadOptions.Method; webRequest.BeginGetRequestStream(WriteCallback, webRequest); } - catch (Exception) + catch (Exception ex) { - DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, new FileTransferError(ConnectionError))); + + DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, new FileTransferError(ConnectionError)),callbackId); + + } } @@ -285,14 +331,23 @@ namespace WPCordovaClassLib.Cordova.Commands { DownloadOptions downloadOptions = null; HttpWebRequest webRequest = null; + string callbackId; try { string[] optionStrings = JSON.JsonHelper.Deserialize(options); - downloadOptions = new DownloadOptions();// JSON.JsonHelper.Deserialize(options); + downloadOptions = new DownloadOptions(); downloadOptions.Url = optionStrings[0]; downloadOptions.FilePath = optionStrings[1]; + bool trustAll = false; + bool.TryParse(optionStrings[2],out trustAll); + + downloadOptions.TrustAllHosts = trustAll; + downloadOptions.Id = optionStrings[3]; + downloadOptions.Headers = optionStrings[4]; + downloadOptions.CallbackId = callbackId = optionStrings[5]; + } catch (Exception) { @@ -315,6 +370,7 @@ namespace WPCordovaClassLib.Cordova.Commands DownloadRequestState state = new DownloadRequestState(); state.options = downloadOptions; state.request = webRequest; + InProcDownloads[downloadOptions.Id] = state; webRequest.BeginGetResponse(new AsyncCallback(downloadCallback), state); } @@ -322,6 +378,28 @@ namespace WPCordovaClassLib.Cordova.Commands } + public void abort(string options) + { + string[] optionStrings = JSON.JsonHelper.Deserialize(options); + string id = optionStrings[0]; + string callbackId = optionStrings[1]; + + if (InProcDownloads.ContainsKey(id)) + { + DownloadRequestState state = InProcDownloads[id]; + state.request.Abort(); + state.request = null; + state = null; + InProcDownloads.Remove(id); + DispatchCommandResult(new PluginResult(PluginResult.Status.OK), callbackId); + + } + else + { + DispatchCommandResult(new PluginResult(PluginResult.Status.IO_EXCEPTION), callbackId); // TODO: is it an IO exception? + } + } + /// /// /// @@ -381,17 +459,17 @@ namespace WPCordovaClassLib.Cordova.Commands } } WPCordovaClassLib.Cordova.Commands.File.FileEntry entry = new WPCordovaClassLib.Cordova.Commands.File.FileEntry(reqState.options.FilePath); - DispatchCommandResult(new PluginResult(PluginResult.Status.OK, entry)); + DispatchCommandResult(new PluginResult(PluginResult.Status.OK, entry), reqState.options.CallbackId); } catch (IsolatedStorageException) { // Trying to write the file somewhere within the IsoStorage. - DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, new FileTransferError(FileNotFoundError))); + DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, new FileTransferError(FileNotFoundError)), reqState.options.CallbackId); } catch (SecurityException) { // Trying to write the file somewhere not allowed. - DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, new FileTransferError(FileNotFoundError))); + DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, new FileTransferError(FileNotFoundError)), reqState.options.CallbackId); } catch (WebException webex) { @@ -402,16 +480,16 @@ namespace WPCordovaClassLib.Cordova.Commands // Weird MSFT detection of 404... seriously... just give us the f(*&#$@ status code as a number ffs!!! // "Numbers for HTTP status codes? Nah.... let's create our own set of enums/structs to abstract that stuff away." // FACEPALM - DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, new FileTransferError(ConnectionError, null, null, 404))); + DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, new FileTransferError(ConnectionError, null, null, 404)), reqState.options.CallbackId); } else { - DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, new FileTransferError(ConnectionError))); + DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, new FileTransferError(ConnectionError)), reqState.options.CallbackId); } } catch (Exception) { - DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, new FileTransferError(FileNotFoundError))); + DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, new FileTransferError(FileNotFoundError)), reqState.options.CallbackId); } }