CB-7532 Handle non-existent download dirs properly
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/2b86a85c Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file-transfer/tree/2b86a85c Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file-transfer/diff/2b86a85c Branch: refs/heads/master Commit: 2b86a85c749a476d0998bb0caff6859c160cb92b Parents: 49c57d2 Author: Vladimir Kotikov <[email protected]> Authored: Fri Sep 12 12:07:33 2014 +0400 Committer: Vladimir Kotikov <[email protected]> Committed: Fri Sep 12 16:02:31 2014 +0400 ---------------------------------------------------------------------- src/windows/FileTransferProxy.js | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cordova-plugin-file-transfer/blob/2b86a85c/src/windows/FileTransferProxy.js ---------------------------------------------------------------------- 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(); + } }); },
