diff --git a/next/pages/api/tables/downloadTable.js b/next/pages/api/tables/downloadTable.js index 35c31288..894f6ed0 100644 --- a/next/pages/api/tables/downloadTable.js +++ b/next/pages/api/tables/downloadTable.js @@ -27,15 +27,15 @@ async function downloadTable(url, datasetID, tableId, token, res) { const prefixUrl = "https://storage.googleapis.com/" - try { - let fileUrl = "" + let fileUrl = "" - if(payloadToken?.pro_subscription_status === "active") { - fileUrl = `${prefixUrl}${urlDownloadClosed}${datasetID}/${tableId}/${tableId}_bdpro.csv.gz` - }else if(url === "free") { - fileUrl =`${prefixUrl}${urlDownloadOpen}${datasetID}/${tableId}/${tableId}.csv.gz` - } + if(payloadToken?.pro_subscription_status === "active") { + fileUrl = `${prefixUrl}${urlDownloadClosed}${datasetID}/${tableId}/${tableId}_bdpro.csv.gz` + }else if(url === "free") { + fileUrl =`${prefixUrl}${urlDownloadOpen}${datasetID}/${tableId}/${tableId}.csv.gz` + } + try { const response = await axios({ url: fileUrl, method: 'GET', @@ -47,8 +47,21 @@ async function downloadTable(url, datasetID, tableId, token, res) { response.data.pipe(res) } catch (error) { - console.error(error) - res.status(500).json({ message: 'Error downloading the file' }) + try { + const response = await axios({ + url: `${prefixUrl}${urlDownloadOpen}${datasetID}/${tableId}/${tableId}.csv.gz`, + method: 'GET', + responseType: 'stream' + }) + + res.setHeader('Content-Disposition', `attachment; filename=${datasetID}_${tableId}.csv.gz`) + res.setHeader('Content-Type', 'application/gzip') + + response.data.pipe(res) + } catch (error) { + console.error(error) + res.status(500).json({ message: 'Error downloading the file' }) + } } }