CB-6466 Auto-create directories in download

Bringing WP8 in line with the other platforms to automatically create
missing directories in filetransfer.download().
This commit is contained in:
Staci
2014-04-22 13:40:56 -04:00
parent eb63c466e8
commit 2043b4e8b0
+31 -31
View File
@@ -41,7 +41,6 @@ namespace WPCordovaClassLib.Cordova.Commands
} }
} }
public class TransferOptions public class TransferOptions
{ {
/// File path to upload OR File path to download to /// File path to upload OR File path to download to
@@ -87,7 +86,6 @@ namespace WPCordovaClassLib.Cordova.Commands
private static Dictionary<string, DownloadRequestState> InProcDownloads = new Dictionary<string,DownloadRequestState>(); private static Dictionary<string, DownloadRequestState> InProcDownloads = new Dictionary<string,DownloadRequestState>();
/// <summary> /// <summary>
/// Uploading response info /// Uploading response info
/// </summary> /// </summary>
@@ -209,8 +207,6 @@ namespace WPCordovaClassLib.Cordova.Commands
BytesLoaded = bLoaded; BytesLoaded = bLoaded;
BytesTotal = bTotal; BytesTotal = bTotal;
} }
} }
/// <summary> /// <summary>
@@ -249,7 +245,6 @@ namespace WPCordovaClassLib.Cordova.Commands
uploadOptions.MimeType = args[4]; uploadOptions.MimeType = args[4];
uploadOptions.Params = args[5]; uploadOptions.Params = args[5];
bool trustAll = false; bool trustAll = false;
bool.TryParse(args[6],out trustAll); bool.TryParse(args[6],out trustAll);
uploadOptions.TrustAllHosts = trustAll; uploadOptions.TrustAllHosts = trustAll;
@@ -303,15 +298,11 @@ namespace WPCordovaClassLib.Cordova.Commands
InProcDownloads[uploadOptions.Id] = reqState; InProcDownloads[uploadOptions.Id] = reqState;
webRequest.BeginGetRequestStream(uploadCallback, reqState); webRequest.BeginGetRequestStream(uploadCallback, reqState);
} }
catch (Exception /*ex*/) catch (Exception /*ex*/)
{ {
DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, new FileTransferError(ConnectionError)),callbackId); DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, new FileTransferError(ConnectionError)),callbackId);
} }
} }
@@ -350,8 +341,6 @@ namespace WPCordovaClassLib.Cordova.Commands
return null; return null;
} }
public void download(string options) public void download(string options)
{ {
TransferOptions downloadOptions = null; TransferOptions downloadOptions = null;
@@ -374,7 +363,6 @@ namespace WPCordovaClassLib.Cordova.Commands
downloadOptions.Id = optionStrings[3]; downloadOptions.Id = optionStrings[3];
downloadOptions.Headers = optionStrings[4]; downloadOptions.Headers = optionStrings[4];
downloadOptions.CallbackId = callbackId = optionStrings[5]; downloadOptions.CallbackId = callbackId = optionStrings[5];
} }
catch (Exception) catch (Exception)
{ {
@@ -389,6 +377,15 @@ namespace WPCordovaClassLib.Cordova.Commands
{ {
using (IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication()) using (IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication())
{ {
string cleanUrl = downloadOptions.Url.Replace("x-wmapp0:", "").Replace("file:", "");
// pre-emptively create any directories in the FilePath that do not exist
string directoryName = getDirectoryName(downloadOptions.FilePath);
if (!string.IsNullOrEmpty(directoryName) && !isoFile.DirectoryExists(directoryName))
{
isoFile.CreateDirectory(directoryName);
}
// just copy from one area of iso-store to another ... // just copy from one area of iso-store to another ...
if (isoFile.FileExists(downloadOptions.Url)) if (isoFile.FileExists(downloadOptions.Url))
{ {
@@ -397,7 +394,6 @@ namespace WPCordovaClassLib.Cordova.Commands
else else
{ {
// need to unpack resource from the dll // need to unpack resource from the dll
string cleanUrl = downloadOptions.Url.Replace("x-wmapp0:", "").Replace("file:", "");
Uri uri = new Uri(cleanUrl, UriKind.Relative); Uri uri = new Uri(cleanUrl, UriKind.Relative);
var resource = Application.GetResourceStream(uri); var resource = Application.GetResourceStream(uri);
@@ -440,12 +436,10 @@ namespace WPCordovaClassLib.Cordova.Commands
} }
} }
} }
} }
} }
} }
} }
} }
File.FileEntry entry = File.FileEntry.GetEntry(downloadOptions.FilePath); File.FileEntry entry = File.FileEntry.GetEntry(downloadOptions.FilePath);
@@ -459,7 +453,6 @@ namespace WPCordovaClassLib.Cordova.Commands
} }
return; return;
} }
else else
{ {
@@ -511,7 +504,6 @@ namespace WPCordovaClassLib.Cordova.Commands
} }
} }
} }
} }
public void abort(string options) public void abort(string options)
@@ -538,7 +530,6 @@ namespace WPCordovaClassLib.Cordova.Commands
state.options.CallbackId); state.options.CallbackId);
} }
} }
} }
else else
{ {
@@ -564,8 +555,6 @@ namespace WPCordovaClassLib.Cordova.Commands
/// <param name="asynchronousResult"></param> /// <param name="asynchronousResult"></param>
private void downloadCallback(IAsyncResult asynchronousResult) private void downloadCallback(IAsyncResult asynchronousResult)
{ {
DownloadRequestState reqState = (DownloadRequestState)asynchronousResult.AsyncState; DownloadRequestState reqState = (DownloadRequestState)asynchronousResult.AsyncState;
HttpWebRequest request = reqState.request; HttpWebRequest request = reqState.request;
@@ -579,6 +568,13 @@ namespace WPCordovaClassLib.Cordova.Commands
using (IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication()) using (IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication())
{ {
// create any directories in the path that do not exist
string directoryName = getDirectoryName(reqState.options.FilePath);
if (!string.IsNullOrEmpty(directoryName) && !isoFile.DirectoryExists(directoryName))
{
isoFile.CreateDirectory(directoryName);
}
// create the file if not exists // create the file if not exists
if (!isoFile.FileExists(reqState.options.FilePath)) if (!isoFile.FileExists(reqState.options.FilePath))
{ {
@@ -617,21 +613,18 @@ namespace WPCordovaClassLib.Cordova.Commands
System.Threading.Thread.Sleep(1); System.Threading.Thread.Sleep(1);
} }
} }
} }
} }
if (reqState.isCancelled) if (reqState.isCancelled)
{ {
isoFile.DeleteFile(reqState.options.FilePath); isoFile.DeleteFile(reqState.options.FilePath);
} }
} }
if (reqState.isCancelled) if (reqState.isCancelled)
{ {
DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, new FileTransferError(AbortError)), DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, new FileTransferError(AbortError)),
callbackId); callbackId);
} }
else else
{ {
@@ -659,7 +652,6 @@ namespace WPCordovaClassLib.Cordova.Commands
if ((webex.Status == WebExceptionStatus.ProtocolError && response.StatusCode == HttpStatusCode.NotFound) if ((webex.Status == WebExceptionStatus.ProtocolError && response.StatusCode == HttpStatusCode.NotFound)
|| webex.Status == WebExceptionStatus.UnknownError) || webex.Status == WebExceptionStatus.UnknownError)
{ {
// Weird MSFT detection of 404... seriously... just give us the f(*&#$@ status code as a number ffs!!! // 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." // "Numbers for HTTP status codes? Nah.... let's create our own set of enums/structs to abstract that stuff away."
// FACEPALM // FACEPALM
@@ -707,11 +699,8 @@ namespace WPCordovaClassLib.Cordova.Commands
{ {
InProcDownloads.Remove(reqState.options.Id); InProcDownloads.Remove(reqState.options.Id);
} }
} }
/// <summary> /// <summary>
/// Read file from Isolated Storage and sends it to server /// Read file from Isolated Storage and sends it to server
/// </summary> /// </summary>
@@ -731,8 +720,6 @@ namespace WPCordovaClassLib.Cordova.Commands
byte[] boundaryBytes = System.Text.Encoding.UTF8.GetBytes(lineStart + Boundary + lineEnd); byte[] boundaryBytes = System.Text.Encoding.UTF8.GetBytes(lineStart + Boundary + lineEnd);
string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"" + lineEnd + lineEnd + "{1}" + lineEnd; string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"" + lineEnd + lineEnd + "{1}" + lineEnd;
if (!string.IsNullOrEmpty(reqState.options.Params)) if (!string.IsNullOrEmpty(reqState.options.Params))
{ {
Dictionary<string, string> paramMap = parseHeaders(reqState.options.Params); Dictionary<string, string> paramMap = parseHeaders(reqState.options.Params);
@@ -758,7 +745,6 @@ namespace WPCordovaClassLib.Cordova.Commands
using (FileStream fileStream = new IsolatedStorageFileStream(reqState.options.FilePath, FileMode.Open, isoFile)) using (FileStream fileStream = new IsolatedStorageFileStream(reqState.options.FilePath, FileMode.Open, isoFile))
{ {
string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"" + lineEnd + "Content-Type: {2}" + lineEnd + lineEnd; string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"" + lineEnd + "Content-Type: {2}" + lineEnd + lineEnd;
string header = string.Format(headerTemplate, reqState.options.FileKey, reqState.options.FileName, reqState.options.MimeType); string header = string.Format(headerTemplate, reqState.options.FileKey, reqState.options.FileName, reqState.options.MimeType);
byte[] headerBytes = System.Text.Encoding.UTF8.GetBytes(header); byte[] headerBytes = System.Text.Encoding.UTF8.GetBytes(header);
@@ -857,8 +843,22 @@ namespace WPCordovaClassLib.Cordova.Commands
{ {
FileTransferError transferError = new FileTransferError(ConnectionError, reqState.options.Server, reqState.options.FilePath, 403); FileTransferError transferError = new FileTransferError(ConnectionError, reqState.options.Server, reqState.options.FilePath, 403);
DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, transferError), reqState.options.CallbackId); DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, transferError), reqState.options.CallbackId);
}
}
} // Gets the full path without the filename
private string getDirectoryName(String filePath)
{
string directoryName;
try
{
directoryName = filePath.Substring(0, filePath.LastIndexOf('/'));
}
catch
{
directoryName = "";
}
return directoryName;
} }
} }
} }