diff --git a/src/api/utils.js b/src/api/utils.js index 1485832..f6c8ecb 100644 --- a/src/api/utils.js +++ b/src/api/utils.js @@ -1,3 +1,6 @@ +import {CONFIG }from '../config/config'; +import axiosInstance from './axiosInstance'; + export default { async saveAsFile(data, filename, filetype) { let blob = new Blob([data], { type: filetype }); @@ -23,5 +26,33 @@ export default { } return str; - } + }, +async downloadFileURL(name, asset, url) { + const fullUrl = `${url}api/info/download?challenge=${name}&asset=${asset}`; + + try { + const response = await axiosInstance({ + method: 'get', + url: fullUrl, + responseType: 'blob', // Ensure binary data is handled properly + }); + + const blob = new Blob([response.data]); + const downloadUrl = window.URL.createObjectURL(blob); + + // Create a link element and trigger the download + const link = document.createElement('a'); + link.href = downloadUrl; + link.setAttribute('download', asset); // Set the filename + document.body.appendChild(link); + link.click(); + link.remove(); + + return downloadUrl; + } catch (error) { + console.error('There has been a problem with your axios operation:', error); + } +} + }; + diff --git a/src/components/ChallCard.vue b/src/components/ChallCard.vue index afd1edc..0c8cd51 100644 --- a/src/components/ChallCard.vue +++ b/src/components/ChallCard.vue @@ -110,6 +110,7 @@ import FlagService from "../api/userAPI"; import Button from "@/components/Button.vue"; import HintsService from "../api/admin/hintsAPI"; +import Utils from "../api/utils"; import { CONFIG } from "@/config/config"; export default { @@ -265,8 +266,9 @@ export default { } }, getStaticUrl(name, asset) { - let url = CONFIG.staticRoot; - return `${url}api/info/download?challenge=${name}&asset=${asset}`; + const url = CONFIG.staticRoot; + const downloadUrl = Utils.downloadFileURL(name, asset, url); + return downloadUrl; }, getFileFromAsset(asset) { let paths = asset.split("/"); diff --git a/src/views/AdminChallenge.vue b/src/views/AdminChallenge.vue index aa7557a..25441a9 100644 --- a/src/views/AdminChallenge.vue +++ b/src/views/AdminChallenge.vue @@ -199,6 +199,7 @@ import { play, purge, undeploy, edit } from "../constants/images"; import SpinLoader from "../components/spinLoader.vue"; import { CONFIG } from "@/config/config"; import EditChallModal from "../components/EditChallModal.vue"; +import Utils from "../api/utils" export default { components: { BarGraphVertical, AdminTable, SpinLoader, EditChallModal }, name: "AdminChallenge", @@ -248,8 +249,9 @@ export default { return `${CONFIG.webRoot}:${port}`; }, getStaticUrl(name, asset) { - let url = CONFIG.staticRoot; - return `${url}api/info/download?challenge=${name}&asset=${asset}`; + const url = CONFIG.staticRoot; + const downloadUrl = Utils.downloadFileURL(name, asset,url); + return downloadUrl; }, getFileFromAsset(asset) { let paths = asset.split("/");