2022-03-11 21:48:40 -05:00
|
|
|
import { NextApiRequest, NextApiResponse } from "next"
|
|
|
|
|
|
|
|
const getRawFile = async (req: NextApiRequest, res: NextApiResponse) => {
|
|
|
|
const { id, download } = req.query
|
2022-03-21 20:20:41 -04:00
|
|
|
const file = await fetch(`${process.env.API_URL}/files/raw/${id}`, {
|
|
|
|
headers: {
|
|
|
|
'Accept': 'text/plain',
|
2022-03-21 20:42:37 -04:00
|
|
|
'x-secret-key': process.env.SECRET_KEY || '',
|
|
|
|
'Authorization': `Bearer ${req.cookies['drift-token']}`,
|
2022-03-21 20:20:41 -04:00
|
|
|
}
|
|
|
|
})
|
2022-03-21 20:42:37 -04:00
|
|
|
|
2022-03-21 20:20:41 -04:00
|
|
|
res.setHeader("Content-Type", "text/plain")
|
|
|
|
res.setHeader('Cache-Control', 's-maxage=86400');
|
|
|
|
|
2022-03-11 21:48:40 -05:00
|
|
|
if (file.ok) {
|
|
|
|
const data = await file.json()
|
|
|
|
const { title, content } = data
|
|
|
|
// serve the file raw as plain text
|
2022-03-21 20:20:41 -04:00
|
|
|
|
2022-03-11 21:48:40 -05:00
|
|
|
if (download) {
|
|
|
|
res.setHeader("Content-Disposition", `attachment; filename="${title}"`)
|
|
|
|
} else {
|
|
|
|
res.setHeader("Content-Disposition", `inline; filename="${title}"`)
|
|
|
|
}
|
|
|
|
|
|
|
|
res.status(200).send(content)
|
|
|
|
} else {
|
|
|
|
res.status(404).send("File not found")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default getRawFile
|