Add (unworking) route handler for raw files

This commit is contained in:
Max Leiter 2023-02-21 23:19:55 -08:00
parent 072516bdb0
commit 64cfe9033e

View file

@ -0,0 +1,62 @@
import { parseQueryParam } from "@lib/server/parse-query-param"
import { notFound } from "next/navigation"
import { NextResponse } from "next/server"
import { prisma } from "src/lib/server/prisma"
export async function GET(
req: Request,
{
params
}: {
params: {
fileId: string
}
}
) {
console.log("GET /api/file/raw/[fileId]/route.ts")
const id = params.fileId
const download = new URL(req.url).searchParams.get("download") === "true"
if (!id) {
return notFound()
}
const file = await prisma.file.findUnique({
where: {
id: parseQueryParam(id)
}
})
if (!file) {
return notFound()
}
const { title, content: contentBuffer } = file
const content = contentBuffer.toString("utf-8")
console.log("title", title)
let headers: HeadersInit = {
"Content-Type": "text/plain; charset=utf-8",
"Cache-Control": "s-maxage=86400"
}
if (download) {
headers = {
...headers,
"Content-Disposition": `attachment; filename="${title}"`
}
} else {
headers = {
...headers,
"Content-Disposition": `inline; filename="${title}"`
}
}
// TODO: we need to return the raw content for this to work. not currently implemented in next.js
return NextResponse.json(
{ data: content },
{
headers
}
)
}