From 2b86a85c749a476d0998bb0caff6859c160cb92b Mon Sep 17 00:00:00 2001 From: Vladimir Kotikov Date: Fri, 12 Sep 2014 12:07:33 +0400 Subject: [PATCH] CB-7532 Handle non-existent download dirs properly --- src/windows/FileTransferProxy.js | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/windows/FileTransferProxy.js b/src/windows/FileTransferProxy.js index 9133801..2523c81 100644 --- a/src/windows/FileTransferProxy.js +++ b/src/windows/FileTransferProxy.js @@ -188,8 +188,8 @@ exec(win, fail, 'FileTransfer', 'upload', // Create internal download operation object fileTransferOps[downloadId] = new FileTransferOperation(FileTransferOperation.PENDING, null); - Windows.Storage.StorageFolder.getFolderFromPathAsync(path).then(function (storageFolder) { - storageFolder.createFileAsync(fileName, Windows.Storage.CreationCollisionOption.generateUniqueName).then(function (storageFile) { + var downloadCallback = function(storageFolder) { + storageFolder.createFileAsync(fileName, Windows.Storage.CreationCollisionOption.generateUniqueName).then(function(storageFile) { // check if download isn't already cancelled var downloadOp = fileTransferOps[downloadId]; @@ -272,11 +272,27 @@ exec(win, fail, 'FileTransfer', 'upload', successCallback && successCallback(progressEvent, { keepCallback: true }); }); - }, function (error) { + }, function(error) { errorCallback && errorCallback(new FileTransferError(FileTransferError.FILE_NOT_FOUND_ERR, source, target, null, null, error)); }); - }, function (error) { + }; + + var fileNotFoundErrorCallback = function(error) { errorCallback && errorCallback(new FileTransferError(FileTransferError.FILE_NOT_FOUND_ERR, source, target, null, null, error)); + }; + + Windows.Storage.StorageFolder.getFolderFromPathAsync(path).then(downloadCallback, function (error) { + // Handle non-existent directory + if (error.number === -2147024894) { + var parent = path.substr(0, path.lastIndexOf('\\')), + folderNameToCreate = path.substr(path.lastIndexOf('\\') + 1); + + Windows.Storage.StorageFolder.getFolderFromPathAsync(parent).then(function(parentFolder) { + parentFolder.createFolderAsync(folderNameToCreate).then(downloadCallback, fileNotFoundErrorCallback); + }, fileNotFoundErrorCallback); + } else { + fileNotFoundErrorCallback(); + } }); },