Skip to content

Commit

Permalink
Added new operation loadZipNoRename to FileService which takes the do…
Browse files Browse the repository at this point in the history
…wnloaded filename from Content-Disposition header, used this operation in all shiny apps downloads
  • Loading branch information
oleg-odysseus committed Sep 9, 2024
1 parent b5f2581 commit b9edf97
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 31 deletions.
11 changes: 3 additions & 8 deletions js/components/analysisExecution/analysis-execution-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,19 +174,14 @@ define([
if (submission) {
submissionId = submission.id;
}
let currentAnalysisId = this.analysisId();

let resultsPathPrefix = this.resultsPathPrefix;
let apiPath = (resultsPathPrefix && resultsPathPrefix.includes('characterizations'))
? shinyConsts.apiPaths.downloadShinyCC(submissionId, source.sourceKey)
: shinyConsts.apiPaths.downloadShinyPW(submissionId, source.sourceKey);

let filePrefix = (resultsPathPrefix && resultsPathPrefix.includes('characterizations'))
? "Characterization_"
: "Pathway_";

FileService.loadZip(
config.api.url + apiPath,
filePrefix + currentAnalysisId + "_gv" + submissionId + "_" + source.sourceKey + ".zip"
FileService.loadZipNoRename(
config.api.url + apiPath
)
.catch((e) => console.error("error when downloading: " + e))
.finally(() => this.loading(false));
Expand Down
5 changes: 2 additions & 3 deletions js/pages/cohort-definitions/cohort-definition-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -1198,9 +1198,8 @@ define(['jquery', 'knockout', 'text!./cohort-definition-manager.html',
}

downloadShinyApp(source) {
FileService.loadZip(
config.api.url + constants.paths.downloadShiny(this.currentCohortDefinition().id(), source.sourceKey),
"Cohort_" + this.currentCohortDefinition().id() + "_" + source.sourceKey + ".zip"
FileService.loadZipNoRename(
config.api.url + constants.paths.downloadShiny(this.currentCohortDefinition().id(), source.sourceKey)
)
.catch((e) => console.error("error when downloading: " + e))
.finally(() => this.loading(false));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,8 @@ define([

downloadShinyApp(source) {
let analysisId = source.info().executionInfo.id.analysisId;
FileService.loadZip(
config.api.url + constants.apiPaths.downloadShiny(analysisId, source.source.sourceKey),
"Incidence_" + analysisId + "_" + source.source.sourceKey + ".zip"
FileService.loadZipNoRename(
config.api.url + constants.apiPaths.downloadShiny(analysisId, source.source.sourceKey)
)
.catch((e) => console.error("error when downloading: " + e))
.finally(() => this.isLoading(false));
Expand Down
64 changes: 47 additions & 17 deletions js/services/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,63 @@ define(
authApi,
) => {
class FileService {
// jQuery won't allow to set responseType other than 'text'
// Helper function to simplify setting up and making the XMLHttpRequest
_makeRequest(url, method, params, callback) {
const xhr = new XMLHttpRequest();
xhr.open(method, url, true);
xhr.setRequestHeader("Authorization", authApi.getAuthorizationHeader());
xhr.setRequestHeader("Content-type", "application/json");
xhr.setRequestHeader("Action-Location", location);
xhr.onreadystatechange = () => {
if (xhr.readyState === 4) {
callback(xhr);
}
};
xhr.onerror = () => reject({
status: xhr.status,
statusText: xhr.statusText
});
xhr.responseType = "arraybuffer";
xhr.send(JSON.stringify(params));
}

loadZip(url, filename, method = 'GET', params = {}) {
const promise = new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open(method, url, true);
xhr.setRequestHeader("Authorization", authApi.getAuthorizationHeader());
xhr.setRequestHeader("Content-type", "application/json");
xhr.setRequestHeader("Action-Location", location);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
return new Promise((resolve, reject) => {
this._makeRequest(url, method, params, (xhr) => {
if (xhr.status === 200) {
resolve();
const blob = new Blob([xhr.response], { type: "octet/stream" });
saveAs(blob, filename);
} else if (xhr.readyState === 4) {
reject({status: xhr.status, statusText: xhr.statusText});
} else {
reject({ status: xhr.status, statusText: xhr.statusText });
}
}
xhr.onerror = reject;
xhr.responseType = "arraybuffer";
xhr.send(JSON.stringify(params));
});
});
}

return promise;
loadZipNoRename(url, method = 'GET', params = {}) {
return new Promise((resolve, reject) => {
this._makeRequest(url, method, params, (xhr) => {
if (xhr.status === 200) {
const filename = xhr.getResponseHeader('Content-Disposition')
.split('filename=')[1]
.split(';')[0]
.replace(/\"/g, ''); // Clean up filename string
const blob = new Blob([xhr.response], { type: "octet/stream" });
saveAs(blob, filename);
resolve();
} else {
reject({
status: xhr.status,
statusText: xhr.statusText
});
}
});
});
}

saveAsJson(data) {
const blob = new Blob([JSON.stringify(data)], {type: "text/json;charset=utf-8"});
const blob = new Blob([JSON.stringify(data)], { type: "text/json;charset=utf-8" });
saveAs(blob, 'data.json');
}
}
Expand Down

0 comments on commit b9edf97

Please sign in to comment.