CoastalCommitsPastes/client/pages/api/raw/[id].ts

36 lines
978 B
TypeScript
Raw Normal View History

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