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.
		
		
		
		
		
			
		
			
				
	
	
		
			35 lines
		
	
	
		
			958 B
		
	
	
	
		
			TypeScript
		
	
			
		
		
	
	
			35 lines
		
	
	
		
			958 B
		
	
	
	
		
			TypeScript
		
	
import { NextApiRequest, NextApiResponse } from "next"
 | 
						|
 | 
						|
const getRawFile = async (req: NextApiRequest, res: NextApiResponse) => {
 | 
						|
	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"]}`
 | 
						|
		}
 | 
						|
	})
 | 
						|
 | 
						|
	res.setHeader("Content-Type", "text/plain; charset=utf-8")
 | 
						|
	res.setHeader("Cache-Control", "s-maxage=86400")
 | 
						|
	if (file.ok) {
 | 
						|
		const json = await file.json()
 | 
						|
		const data = json
 | 
						|
		const { title, content } = data
 | 
						|
		// serve the file raw as plain text
 | 
						|
 | 
						|
		if (download) {
 | 
						|
			res.setHeader("Content-Disposition", `attachment; filename="${title}"`)
 | 
						|
		} else {
 | 
						|
			res.setHeader("Content-Disposition", `inline; filename="${title}"`)
 | 
						|
		}
 | 
						|
 | 
						|
		res.status(200).write(content, "utf-8")
 | 
						|
		res.end()
 | 
						|
	} else {
 | 
						|
		res.status(404).send("File not found")
 | 
						|
	}
 | 
						|
}
 | 
						|
 | 
						|
export default getRawFile
 |