diff --git a/api-openbis-javascript/src/v3/openbis.js b/api-openbis-javascript/src/v3/openbis.js index 77b1bddfd9924e819cd3bd6852f15a4dad9bd492..cef01618ba03179a2e380bd8eb5bc1db890e3d54 100644 --- a/api-openbis-javascript/src/v3/openbis.js +++ b/api-openbis-javascript/src/v3/openbis.js @@ -300,111 +300,70 @@ define([ 'jquery', 'util/Json', 'as/dto/datastore/search/DataStoreSearchCriteria ); } - this.uploadFileWorkspaceDSS = function(files) { - var thisFacade = this; + this.uploadFileWorkspaceDSS = async function(files) { var uploadId = getUUID(); - var dfd = jquery.Deferred(); - this._uploadFileWorkspaceDSSEmptyDir(uploadId).done(function() { - thisFacade._uploadFileWorkspaceDSS(files, 0, uploadId, new Set()).done(function() { - dfd.resolve(uploadId); - }).fail(function(error) { - dfd.reject(error); - }); - }).fail(function(error) { - dfd.reject(error); - }); - return dfd.promise(); + await this._uploadFileWorkspaceDSSEmptyDir(uploadId); + return await this._uploadFileWorkspaceDSS(files, uploadId); } - this._uploadFileWorkspaceDSS = function(files, index, parentId, createdDirectories) { - var thisFacade = this; - var dfd = jquery.Deferred(); - if (index < files.length) { - var relativePath = files[index].webkitRelativePath; + this._uploadFileWorkspaceDSS = async function(files, parentId) { + var createdDirectories = new Set(); + var filesCount = files.length; + for (var i = 0; i < filesCount; i++) { + var relativePath = files[i].webkitRelativePath; var directoryRelativePath = relativePath.substring(0, relativePath.lastIndexOf("/") + 1); if (directoryRelativePath && !createdDirectories.has(directoryRelativePath)) { - this._uploadFileWorkspaceDSSEmptyDir(parentId + "/" + directoryRelativePath) - .done(function() { - createdDirectories.add(directoryRelativePath); - thisFacade._uploadFileWorkspaceDSSFile(files[index], parentId) - .done(function() { - thisFacade._uploadFileWorkspaceDSS(files, index + 1, parentId, createdDirectories) - .done(function() { - dfd.resolve(); - }) - .fail(function(error) { - dfd.reject(error); - }); - }) - .fail(function(error) { - dfd.reject(error); - }); - }) - .fail(function(error) { - dfd.reject(error); - }); - } else { - this._uploadFileWorkspaceDSSFile(files[index], parentId) - .done(function() { - thisFacade._uploadFileWorkspaceDSS(files, index + 1, parentId, createdDirectories) - .done(function() { - dfd.resolve(); - }) - .fail(function(error) { - dfd.reject(error); - }); - }) - .fail(function(error) { - dfd.reject(error); - }); + await this._uploadFileWorkspaceDSSEmptyDir(parentId + "/" + directoryRelativePath); + createdDirectories.add(directoryRelativePath); } - } else { - dfd.resolve(); + await this._uploadFileWorkspaceDSSFile(files[i], parentId); } - return dfd.promise(); + return parentId; } this._uploadFileWorkspaceDSSEmptyDir = function(pathToDir) { + var thisFacade = this; var sessionID = facade._private.sessionToken; var filename = encodeURIComponent(pathToDir); - var dfd = jquery.Deferred(); - this._getDataStores().done(function (dataStores) { - if (dataStores.length === 1) { - fetch(createUrlWithParameters(dataStores[0], "session_workspace_file_upload", - "?sessionID=" + sessionID + - "&filename=" + filename + - "&id=1&startByte=0&endByte=0&size=0&emptyFolder=true"), { - method: "POST", - headers: { - "Content-Type": "multipart/form-data" - } - }).then(function (response) { - dfd.resolve(response); - }).catch(function (error) { - dfd.reject(error); - }); - } else { - dfd.reject("Please specify exactly one data store"); - } - }).fail(function(error) { - dfd.reject(error); + return new Promise(function(resolve, reject) { + thisFacade._getDataStores().done(function(dataStores) { + if (dataStores.length === 1) { + fetch(createUrlWithParameters(dataStores[0], "session_workspace_file_upload", + "?sessionID=" + sessionID + + "&filename=" + filename + + "&id=1&startByte=0&endByte=0&size=0&emptyFolder=true"), { + method: "POST", + headers: { + "Content-Type": "multipart/form-data" + } + }).then(function (response) { + resolve(response); + }).catch(function (error) { + reject(error); + }); + } else { + reject("Please specify exactly one data store"); + } + }).fail(function(error) { + reject(error); + }); }); - return dfd.promise(); } this._uploadFileWorkspaceDSSFile = function(file, parentId) { - const dfd = jquery.Deferred(); - this._getDataStores().done(function(dataStores) { - uploadBlob(dataStores[0], parentId, facade._private.sessionToken, file, 0, 1048576) - .done(function() { - dfd.resolve(); - }).fail(function(error) { - dfd.reject(error); + var thisFacade = this; + return new Promise(function(resolve, reject) { + thisFacade._getDataStores().done(function(dataStores) { + uploadBlob(dataStores[0], parentId, facade._private.sessionToken, file, 0, 1048576) + .done(function() { + resolve(); + }).fail(function(error) { + reject(error); }); - }).fail(function(error) { - dfd.reject(error); + }).fail(function(error) { + reject(error); + }); }); - return dfd.promise(); } function uploadBlob(dataStore, parentId, sessionID, file, startByte, chunkSize) {