File size: 2,336 Bytes
d988ae4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
import {
Controller,
Post,
Get,
Delete,
Param,
UploadedFile,
UseInterceptors,
Body,
Res,
MaxFileSizeValidator,
ParseFilePipe,
HttpStatus,
HttpException,
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { Response } from 'express';
import { FileService } from './file.service';
@Controller('clipboard/:roomCode/files')
export class FileController {
constructor(private readonly fileService: FileService) {}
@Post()
@UseInterceptors(FileInterceptor('file'))
async uploadFile(
@Param('roomCode') roomCode: string,
@UploadedFile(
new ParseFilePipe({
validators: [
new MaxFileSizeValidator({ maxSize: 10 * 1024 * 1024 }), // 10MB
],
}),
)
file: Express.Multer.File,
@Body('clientId') clientId: string,
) {
try {
const fileEntry = await this.fileService.uploadFile(
roomCode,
file,
clientId,
);
return fileEntry;
} catch (error) {
throw new HttpException(
error.message || 'Failed to upload file',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@Get(':fileId')
async getFile(
@Param('roomCode') roomCode: string,
@Param('fileId') fileId: string,
@Res() res: Response,
) {
try {
const fileData = await this.fileService.getFileData(roomCode, fileId);
const fileUrl = fileData.url;
// If there's a filename parameter in the query, append it to the redirect URL
const originalFilename = res.req.query.filename;
const redirectUrl = originalFilename
? `${fileUrl}?download=true&originalName=${encodeURIComponent(originalFilename.toString())}`
: fileUrl;
return res.redirect(redirectUrl);
} catch (error) {
throw new HttpException(
error.message || 'Failed to get file',
HttpStatus.NOT_FOUND,
);
}
}
@Delete(':fileId')
async deleteFile(
@Param('roomCode') roomCode: string,
@Param('fileId') fileId: string,
) {
try {
const success = await this.fileService.deleteFile(roomCode, fileId);
return { success };
} catch (error) {
throw new HttpException(
error.message || 'Failed to delete file',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
}
|