diff --git a/src/api/jobs.js b/src/api/jobs.js index 0534e057c..16c757731 100644 --- a/src/api/jobs.js +++ b/src/api/jobs.js @@ -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({ diff --git a/src/constants/constants.js b/src/constants/constants.js index b35014697..f5050008e 100644 --- a/src/constants/constants.js +++ b/src/constants/constants.js @@ -82,6 +82,11 @@ export const actionsOptions = [ }, { id: 6, + name: "Download", + reportFormat: "download", + }, + { + id: 7, name: "Import Report", reportFormat: "importReport", }, diff --git a/src/constants/endpoints.js b/src/constants/endpoints.js index 1923eafff..7fb04685d 100644 --- a/src/constants/endpoints.js +++ b/src/constants/endpoints.js @@ -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}`, }, diff --git a/src/pages/Browse/index.jsx b/src/pages/Browse/index.jsx index 637ec49e9..f98e1d5e1 100644 --- a/src/pages/Browse/index.jsx +++ b/src/pages/Browse/index.jsx @@ -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, @@ -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) => { diff --git a/src/services/jobs.js b/src/services/jobs.js index e6507b2a3..bdb85f247 100644 --- a/src/services/jobs.js +++ b/src/services/jobs.js @@ -24,8 +24,10 @@ import { downloadReportApi, getAllJobApi, getAllAdminJobApi, + downloadFileApi, importReportApi, } from "api/jobs"; + import { getReportIdFromUrl } from "shared/helper"; import { getLocalStorage } from "shared/storageHelper"; @@ -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) { diff --git a/src/shared/helper.js b/src/shared/helper.js index f60764ffe..0d9ed430b 100644 --- a/src/shared/helper.js +++ b/src/shared/helper.js @@ -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="(.*)"/);