Skip to content

Commit

Permalink
Merge pull request #924 from basedosdados/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
AldemirLucas authored Aug 20, 2024
2 parents 31b0ca3 + 0dba9f2 commit a968024
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions next/pages/api/tables/downloadTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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' })
}
}
}

Expand Down

0 comments on commit a968024

Please sign in to comment.