mirror of https://github.com/MaxLeiter/Drift
				
				
				
			
			You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
	
	
		
			53 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			TypeScript
		
	
			
		
		
	
	
			53 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			TypeScript
		
	
import { getHtmlFromFile } from "@lib/server/get-html-from-drift-file"
 | 
						|
import { parseQueryParam } from "@lib/server/parse-query-param"
 | 
						|
import prisma from "app/prisma"
 | 
						|
import { NextApiRequest, NextApiResponse } from "next"
 | 
						|
 | 
						|
export default async function handler(
 | 
						|
	req: NextApiRequest,
 | 
						|
	res: NextApiResponse
 | 
						|
) {
 | 
						|
	switch (req.method) {
 | 
						|
		case "GET":
 | 
						|
			const query = req.query
 | 
						|
			const fileId = parseQueryParam(query.fileId)
 | 
						|
			const content = parseQueryParam(query.content)
 | 
						|
			const title = parseQueryParam(query.title)
 | 
						|
 | 
						|
			if (fileId && (content || title)) {
 | 
						|
				return res.status(400).json({ error: "Too many arguments" })
 | 
						|
			}
 | 
						|
 | 
						|
			if (fileId) {
 | 
						|
				// TODO: abstract to getFileById
 | 
						|
				const file = await prisma.file.findUnique({
 | 
						|
					where: {
 | 
						|
						id: fileId
 | 
						|
					}
 | 
						|
				})
 | 
						|
 | 
						|
				if (!file) {
 | 
						|
					return res.status(404).json({ error: "File not found" })
 | 
						|
				}
 | 
						|
 | 
						|
				return res.json(file.html)
 | 
						|
			} else {
 | 
						|
				if (!content || !title) {
 | 
						|
					return res.status(400).json({ error: "Missing arguments" })
 | 
						|
				}
 | 
						|
 | 
						|
				const renderedHTML = getHtmlFromFile({
 | 
						|
					title,
 | 
						|
					content
 | 
						|
				})
 | 
						|
 | 
						|
				res.setHeader("Content-Type", "text/plain")
 | 
						|
				res.status(200).write(renderedHTML)
 | 
						|
				res.end()
 | 
						|
				return
 | 
						|
			}
 | 
						|
		default:
 | 
						|
			return res.status(405).json({ error: "Method not allowed" })
 | 
						|
	}
 | 
						|
}
 |