Skip to content

Commit 14e2c56

Browse files
committed
refactor
1 parent d6c2cc6 commit 14e2c56

File tree

2 files changed

+27
-20
lines changed

2 files changed

+27
-20
lines changed

services/static-webserver/client/source/class/osparc/file/FilePicker.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,16 @@ qx.Class.define("osparc.file.FilePicker", {
414414
if (files.length === 1) {
415415
const fileUploader = new osparc.file.FileUploader(this.getNode());
416416
fileUploader.addListener("uploadAborted", () => this.__resetOutput());
417-
fileUploader.addListener("fileUploaded", () => {
417+
fileUploader.addListener("fileUploaded", e => {
418+
const fileMetadata = e.getData();
419+
if (
420+
"location" in fileMetadata &&
421+
"dataset" in fileMetadata &&
422+
"path" in fileMetadata &&
423+
"name" in fileMetadata
424+
) {
425+
osparc.file.FilePicker.setOutputValueFromStore(this.getNode(), fileMetadata["location"], fileMetadata["dataset"], fileMetadata["path"], fileMetadata["name"]);
426+
}
418427
this.fireEvent("fileUploaded");
419428
this.getNode().fireEvent("fileUploaded");
420429
}, this);

services/static-webserver/client/source/class/osparc/file/FileUploader.js

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ qx.Class.define("osparc.file.FileUploader", {
3939

4040
events: {
4141
"uploadAborted": "qx.event.type.Event",
42-
"fileUploaded": "qx.event.type.Event"
42+
"fileUploaded": "qx.event.type.Data",
4343
},
4444

4545
statics: {
@@ -60,6 +60,7 @@ qx.Class.define("osparc.file.FileUploader", {
6060
members: {
6161
__presignedLinkData: null,
6262
__uploadedParts: null,
63+
__fileMetadata: null,
6364

6465
// Request to the server an upload URL.
6566
retrieveUrlAndUpload: function(file) {
@@ -80,6 +81,14 @@ qx.Class.define("osparc.file.FileUploader", {
8081
.then(presignedLinkData => {
8182
if (presignedLinkData.resp.urls) {
8283
this.__presignedLinkData = presignedLinkData;
84+
85+
this.__fileMetadata = {
86+
location: presignedLinkData.locationId,
87+
dataset: studyId,
88+
path: presignedLinkData.fileUuid,
89+
name: file.name
90+
};
91+
8392
try {
8493
this.__uploadFile(file);
8594
} catch (error) {
@@ -162,16 +171,8 @@ qx.Class.define("osparc.file.FileUploader", {
162171
const presignedLinkData = this.__presignedLinkData;
163172
this.getNode().getStatus().setProgress(this.self().PROGRESS_VALUES.COMPLETING);
164173
const completeUrl = presignedLinkData.resp.links.complete_upload;
165-
const location = presignedLinkData.locationId;
166-
const path = presignedLinkData.fileUuid;
167174
const xhr = new XMLHttpRequest();
168175
xhr.onloadend = () => {
169-
const fileMetadata = {
170-
location,
171-
dataset: this.getNode().getStudy().getUuid(),
172-
path,
173-
name: file.name
174-
};
175176
const resp = JSON.parse(xhr.responseText);
176177
if ("error" in resp && resp["error"]) {
177178
console.error(resp["error"]);
@@ -182,9 +183,9 @@ qx.Class.define("osparc.file.FileUploader", {
182183
// @odeimaiz: we need to poll the received new location in the response
183184
// we do have links.state -> poll that link until it says ok
184185
// right now this kind of work if files are small and this happens fast
185-
this.__pollFileUploadState(resp["data"]["links"]["state"], fileMetadata);
186+
this.__pollFileUploadState(resp["data"]["links"]["state"]);
186187
} else if (xhr.status == 200) {
187-
this.__completeUpload(fileMetadata);
188+
this.__completeUpload();
188189
}
189190
}
190191
};
@@ -196,30 +197,27 @@ qx.Class.define("osparc.file.FileUploader", {
196197
xhr.send(JSON.stringify(body));
197198
},
198199

199-
__pollFileUploadState: function(stateLink, fileMetadata) {
200+
__pollFileUploadState: function(stateLink) {
200201
const xhr = new XMLHttpRequest();
201202
xhr.open("POST", stateLink, true);
202203
xhr.setRequestHeader("Content-Type", "application/json");
203204
xhr.onloadend = () => {
204205
const resp = JSON.parse(xhr.responseText);
205206
if ("data" in resp && resp["data"] && resp["data"]["state"] === "ok") {
206-
this.__completeUpload(fileMetadata);
207+
this.__completeUpload();
207208
} else {
208209
const interval = 2000;
209-
qx.event.Timer.once(() => this.__pollFileUploadState(stateLink, fileMetadata), this, interval);
210+
qx.event.Timer.once(() => this.__pollFileUploadState(stateLink), this, interval);
210211
}
211212
};
212213
xhr.send();
213214
},
214215

215-
__completeUpload: function(fileMetadata) {
216+
__completeUpload: function() {
216217
this.getNode()["fileUploadAbortRequested"] = false;
217218

218-
if ("location" in fileMetadata && "dataset" in fileMetadata && "path" in fileMetadata && "name" in fileMetadata) {
219-
osparc.file.FilePicker.setOutputValueFromStore(this.getNode(), fileMetadata["location"], fileMetadata["dataset"], fileMetadata["path"], fileMetadata["name"]);
220-
}
221219
this.__presignedLinkData = null;
222-
this.fireEvent("fileUploaded");
220+
this.fireDataEvent("fileUploaded", this.__fileMetadata);
223221
},
224222

225223
__abortUpload: function() {

0 commit comments

Comments
 (0)