|
|
import { Controller, Get, Param, Res, Req, NotFoundException } from '@nestjs/common'; |
|
|
import { Request, Response } from 'express'; |
|
|
import { FilesystemService } from './filesystem.service'; |
|
|
import * as path from 'path'; |
|
|
import * as mime from 'mime-types'; |
|
|
|
|
|
@Controller('files') |
|
|
export class FilesystemController { |
|
|
constructor(private readonly filesystemService: FilesystemService) {} |
|
|
|
|
|
@Get(':roomCode/:fileName') |
|
|
async serveFile( |
|
|
@Param('roomCode') roomCode: string, |
|
|
@Param('fileName') fileName: string, |
|
|
@Req() req: Request, |
|
|
@Res() res: Response, |
|
|
) { |
|
|
try { |
|
|
const fileBuffer = await this.filesystemService.getFile(roomCode, fileName); |
|
|
|
|
|
|
|
|
const contentType = mime.lookup(fileName) || 'application/octet-stream'; |
|
|
|
|
|
|
|
|
res.setHeader('Content-Type', contentType); |
|
|
|
|
|
|
|
|
const originalName = req.query.originalName ? decodeURIComponent(req.query.originalName.toString()) : fileName; |
|
|
|
|
|
|
|
|
const downloadHeader = req.query.download === 'true' |
|
|
? `attachment; filename="${originalName}"` |
|
|
: `inline; filename="${originalName}"`; |
|
|
|
|
|
res.setHeader('Content-Disposition', downloadHeader); |
|
|
|
|
|
|
|
|
return res.send(fileBuffer); |
|
|
} catch (error) { |
|
|
throw new NotFoundException('File not found'); |
|
|
} |
|
|
} |
|
|
} |
|
|
|