start over with nextjs

This commit is contained in:
Thomas Ruoff
2021-02-25 00:31:28 +01:00
parent 3a77e7f22b
commit 214d95d3d7
68 changed files with 3047 additions and 18576 deletions

45
pages/api/pdf/[id].ts Normal file
View File

@@ -0,0 +1,45 @@
import {promises} from 'fs'
import { NextApiRequest, NextApiResponse } from 'next'
import {remove as storeRemove} from '../../../lib/store'
import { getPdfPath } from '../../../lib/utils'
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const { method } = req;
switch (req.method) {
case 'GET':
try {
const {
query: { id: idArg },
} = req
const fileContent = await promises.readFile(getPdfPath(idArg))
res.setHeader('Content-Type', 'application/pdf')
res.status(200).send(fileContent);
} catch (error) {
console.error(error)
res.status(404).json({ statusCode: 404, message: 'Method Not Allowed' })
}
break;
case 'DELETE':
try {
const {
query: { id: idArg },
} = req
storeRemove(idArg)
res.status(202).end()
} catch (error) {
console.error(error)
res.status(404).json({ statusCode: 404, message: 'Method Not Allowed' })
}
break;
default:
res.setHeader('Allow', ['GET', 'DELETE'])
res.status(405).end(`Method ${method} Not Allowed`)
}
}
export default handler;