Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ui):download functionality implemented in the frontend #267

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/api/jobs.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,18 @@ export const scheduleReportApi = (uploadId, reportFormat) => {
});
};

export const downloadFileApi = (uploadId) => {
const url = endpoints.jobs.scheduleDownload(uploadId);
return sendRequest({
url,
method: "GET",
headers: {
Authorization: getToken(),
},
isFile: true,
});
};

export const downloadReportApi = (reportId) => {
const url = endpoints.jobs.downloadReport(reportId);
return sendRequest({
Expand Down
5 changes: 5 additions & 0 deletions src/constants/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ export const actionsOptions = [
},
{
id: 6,
name: "Download",
reportFormat: "download",
},
{
id: 7,
name: "Import Report",
reportFormat: "importReport",
},
Expand Down
1 change: 1 addition & 0 deletions src/constants/endpoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const endpoints = {
scheduleAnalysis: () => `${apiUrl}/jobs`,
allJobs: () => `${apiUrl}/jobs/all`,
scheduleReport: () => `${apiUrl}/report`,
scheduleDownload: (uploadId) => `${apiUrl}/uploads/${uploadId}/download`,
downloadReport: (reportId) => `${apiUrl}/report/${reportId}`,
importReport: (uploadId) => `${apiUrl}/report/import?upload=${uploadId}`,
},
Expand Down
113 changes: 75 additions & 38 deletions src/pages/Browse/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ import TreeContainer from "components/TreeContainer";
// Required functions for calling APIs
import getBrowseData from "services/browse";
import { getAllFolders } from "services/folders";
import { scheduleReport, downloadReport } from "services/jobs";
import {
scheduleReport,
downloadReport,
scheduleDownload,
} from "services/jobs";
import {
getFileNameFromContentDispostionHeader,
handleError,
Expand Down Expand Up @@ -154,48 +158,81 @@ const Browse = () => {
);
return;
}
scheduleReport(uploadId, e.target.value)
.then((res) => {
return res?.message;
})
.then((url) => {
setTimeout(() => {
downloadReport(url)
.then((response) => {
return response;
})
.then((response) => {
const filename = getFileNameFromContentDispostionHeader(
response.headers.get("content-disposition")
);
response
.blob()
.then((blob) => {
const aTag = document.createElement("a");
aTag.href = window.URL.createObjectURL(blob);
aTag.download = filename;
document.body.appendChild(aTag); // Required for this to work in FireFox
aTag.click();
setTimeout(() => {
window.URL.revokeObjectURL(url);
document.body.removeChild(aTag);
}, 150);
})
.catch((error) => {
handleError(error, setMessage);
setShowMessage(true);
});

if (e.target.value !== "download") {
scheduleReport(uploadId, e.target.value)
.then((res) => {
return res?.message;
})
.then((url) => {
setTimeout(() => {
downloadReport(url)
.then((response) => {
return response;
})
.then((response) => {
const filename = getFileNameFromContentDispostionHeader(
response.headers.get("content-disposition")
);
response
.blob()
.then((blob) => {
const aTag = document.createElement("a");
aTag.href = window.URL.createObjectURL(blob);
aTag.download = filename;
document.body.appendChild(aTag); // Required for this to work in FireFox
aTag.click();
setTimeout(() => {
window.URL.revokeObjectURL(url);
document.body.removeChild(aTag);
}, 150);
})
.catch((error) => {
handleError(error, setMessage);
setShowMessage(true);
});
})
.catch((error) => {
handleError(error, setMessage);
setShowMessage(true);
});
}, 1200);
})
.catch((error) => {
handleError(error, setMessage);
setShowMessage(true);
});
} else {
scheduleDownload(uploadId)
.then((response) => {
const filename = getFileNameFromContentDispostionHeader(
response.headers.get("pragma")
);

response
.blob()
.then((blob) => {
const aTag = document.createElement("a");
aTag.href = window.URL.createObjectURL(blob);
aTag.download = filename;

document.body.appendChild(aTag); // Required for this to work in FireFox
aTag.click();
setTimeout(() => {
window.URL.revokeObjectURL(blob);
document.body.removeChild(aTag);
}, 150);
})
.catch((error) => {
handleError(error, setMessage);
setShowMessage(true);
});
}, 1200);
})
.catch((error) => {
handleError(error, setMessage);
setShowMessage(true);
});
})
.catch((error) => {
handleError(error, setMessage);
setShowMessage(true);
});
}
};

const handleClick = (e, id) => {
Expand Down
8 changes: 8 additions & 0 deletions src/services/jobs.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ import {
downloadReportApi,
getAllJobApi,
getAllAdminJobApi,
downloadFileApi,
importReportApi,
} from "api/jobs";

import { getReportIdFromUrl } from "shared/helper";
import { getLocalStorage } from "shared/storageHelper";

Expand Down Expand Up @@ -73,6 +75,12 @@ export const scheduleReport = (uploadId, reportFormat) => {
});
};

export const scheduleDownload = (uploadId) => {
return downloadFileApi(uploadId).then((res) => {
return res;
});
};

export const downloadReport = (url) => {
const reportId = getReportIdFromUrl(url);
if (reportId === null) {
Expand Down
2 changes: 1 addition & 1 deletion src/shared/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export const getReportIdFromUrl = (url) => {

export const getFileNameFromContentDispostionHeader = (header) => {
const contentDispostion = header.split(";");
let fileName = "download.txt";
let fileName = "download";
// eslint-disable-next-line no-restricted-syntax
for (const headerElement of contentDispostion) {
const matches = headerElement.trim().match(/filename="(.*)"/);
Expand Down