Updated Branches: refs/heads/master c42bbd50e -> 9f21cfc2e
[CB-795] Added HTTP status code to WP7 FileTransferError interface. Catches 404 on download. Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-wp7/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-wp7/commit/9f21cfc2 Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-wp7/tree/9f21cfc2 Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-wp7/diff/9f21cfc2 Branch: refs/heads/master Commit: 9f21cfc2e2659e7300aff334c97aca7d5a98e709 Parents: c42bbd5 Author: filmaj <[email protected]> Authored: Mon Jun 18 16:02:18 2012 -0700 Committer: filmaj <[email protected]> Committed: Mon Jun 18 16:02:18 2012 -0700 ---------------------------------------------------------------------- framework/Cordova/Commands/FileTransfer.cs | 76 ++++++++++++++++++----- 1 files changed, 60 insertions(+), 16 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-cordova-wp7/blob/9f21cfc2/framework/Cordova/Commands/FileTransfer.cs ---------------------------------------------------------------------- diff --git a/framework/Cordova/Commands/FileTransfer.cs b/framework/Cordova/Commands/FileTransfer.cs index 1cbf451..eee682a 100644 --- a/framework/Cordova/Commands/FileTransfer.cs +++ b/framework/Cordova/Commands/FileTransfer.cs @@ -20,6 +20,7 @@ using System.Net; using System.Runtime.Serialization; using System.Windows; using System.Security; +using System.Diagnostics; namespace WP7CordovaClassLib.Cordova.Commands { @@ -190,12 +191,40 @@ namespace WP7CordovaClassLib.Cordova.Commands public int Code { get; set; } /// <summary> + /// The source URI + /// </summary> + [DataMember(Name = "source", IsRequired = true)] + public string Source { get; set; } + + /// <summary> + /// The target URI + /// </summary> + [DataMember(Name = "target", IsRequired = true)] + public string Target { get; set; } + + /// <summary> + /// The http status code response from the remote URI + /// </summary> + [DataMember(Name = "http_status", IsRequired = true)] + public int HttpStatus { get; set; } + + /// <summary> /// Creates FileTransferError object /// </summary> /// <param name="errorCode">Error code</param> public FileTransferError(int errorCode) { this.Code = errorCode; + this.Source = null; + this.Target = null; + this.HttpStatus = 0; + } + public FileTransferError(int errorCode, string source, string target, int status) + { + this.Code = errorCode; + this.Source = source; + this.Target = target; + this.HttpStatus = status; } } @@ -236,7 +265,7 @@ namespace WP7CordovaClassLib.Cordova.Commands } catch (Exception) { - DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR,new FileTransferError(InvalidUrlError))); + DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR,new FileTransferError(InvalidUrlError, uploadOptions.Server, null, 0))); return; } HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(serverUri); @@ -271,7 +300,7 @@ namespace WP7CordovaClassLib.Cordova.Commands } catch (Exception) { - DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, new FileTransferError(InvalidUrlError))); + DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, new FileTransferError(InvalidUrlError, downloadOptions.Url, null, 0))); return; } @@ -300,7 +329,7 @@ namespace WP7CordovaClassLib.Cordova.Commands { HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult); - using (IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication()) + using (IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication()) { // create the file if not exists if (!isoFile.FileExists(reqState.options.FilePath)) @@ -315,7 +344,7 @@ namespace WP7CordovaClassLib.Cordova.Commands int bytesRead = 0; using (BinaryReader reader = new BinaryReader(response.GetResponseStream())) { - + using (BinaryWriter writer = new BinaryWriter(fileStream)) { int BUFFER_SIZE = 1024; @@ -338,31 +367,46 @@ namespace WP7CordovaClassLib.Cordova.Commands break; } } - } + } } - + } } - WP7CordovaClassLib.Cordova.Commands.File.FileEntry entry = new WP7CordovaClassLib.Cordova.Commands.File.FileEntry(reqState.options.FilePath); - DispatchCommandResult(new PluginResult(PluginResult.Status.OK, entry)); + WP7CordovaClassLib.Cordova.Commands.File.FileEntry entry = new WP7CordovaClassLib.Cordova.Commands.File.FileEntry(reqState.options.FilePath); + DispatchCommandResult(new PluginResult(PluginResult.Status.OK, entry)); } catch (IsolatedStorageException) { - // DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, new ErrorCode(INVALID_MODIFICATION_ERR))); + // Trying to write the file somewhere within the IsoStorage. + DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, new FileTransferError(FileNotFoundError))); } catch (SecurityException) { - //DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, new ErrorCode(SECURITY_ERR))); + // Trying to write the file somewhere not allowed. + DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, new FileTransferError(FileNotFoundError))); } - catch (Exception ex) + catch (WebException webex) { - DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, new File.ErrorCode(File.NOT_FOUND_ERR))); + // TODO: probably need better work here to properly respond with all http status codes back to JS + // Right now am jumping through hoops just to detect 404. + if ((webex.Status == WebExceptionStatus.ProtocolError && ((HttpWebResponse)webex.Response).StatusCode == HttpStatusCode.NotFound) || webex.Status == WebExceptionStatus.UnknownError) + { + // 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))); + } + else + { + DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, new FileTransferError(ConnectionError))); + } } - - - + catch (Exception) + { + DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, new FileTransferError(FileNotFoundError))); + } } @@ -399,7 +443,7 @@ namespace WP7CordovaClassLib.Cordova.Commands { if (!isoFile.FileExists(uploadOptions.FilePath)) { - DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, new FileTransferError(FileNotFoundError))); + DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, new FileTransferError(FileNotFoundError, uploadOptions.Server, uploadOptions.FilePath, 0))); return; }
