32 lines
822 B
TypeScript
32 lines
822 B
TypeScript
![]() |
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 }
|