🎉 Start project but-3
This commit is contained in:
31
src/routes/file.ts
Normal file
31
src/routes/file.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import express, { Response } from 'express'
|
||||
|
||||
import { getFile } from '@controllers'
|
||||
import { authenticated } from '@helpers'
|
||||
import { AuthenticatedRequest, IFile, IUser } from '@types'
|
||||
|
||||
import path from 'path'
|
||||
|
||||
const router = express.Router()
|
||||
|
||||
router.get('/:_id', authenticated, async (req: AuthenticatedRequest, res: Response) => {
|
||||
const { _id } = req.params
|
||||
|
||||
const response = await getFile(<IFile['_id']>_id, <IUser>req.user)
|
||||
|
||||
if (!response.data) {
|
||||
return res.status(response.status).send(response.error)
|
||||
}
|
||||
|
||||
const file = Buffer.from(response.data.data, 'base64')
|
||||
|
||||
res.writeHead(response.status, {
|
||||
'Content-Type': response.data.mimetype,
|
||||
'Content-Length': file.length,
|
||||
'Content-Disposition': `inline; filename="${response.data.name}`,
|
||||
})
|
||||
|
||||
res.end(file)
|
||||
})
|
||||
|
||||
export { router as fileRouter }
|
Reference in New Issue
Block a user