251122:1700 Phase 4

This commit is contained in:
admin
2025-11-22 17:21:55 +07:00
parent bf0308e350
commit a3474bff6a
63 changed files with 10062 additions and 109 deletions

View File

@@ -0,0 +1,49 @@
import {
Controller,
Post,
UseInterceptors,
UploadedFile,
UseGuards,
Request,
ParseFilePipe,
MaxFileSizeValidator,
FileTypeValidator,
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { FileStorageService } from './file-storage.service.js';
import { JwtAuthGuard } from '../guards/jwt-auth.guard.js';
// ✅ 1. สร้าง Interface เพื่อระบุ Type ของ Request
interface RequestWithUser {
user: {
userId: number;
username: string;
};
}
@Controller('files')
@UseGuards(JwtAuthGuard)
export class FileStorageController {
constructor(private readonly fileStorageService: FileStorageService) {}
@Post('upload')
@UseInterceptors(FileInterceptor('file')) // รับ field ชื่อ 'file'
async uploadFile(
@UploadedFile(
new ParseFilePipe({
validators: [
new MaxFileSizeValidator({ maxSize: 50 * 1024 * 1024 }), // 50MB
// ตรวจสอบประเภทไฟล์ (Regex)
new FileTypeValidator({
fileType: /(pdf|msword|openxmlformats|zip|octet-stream)/,
}),
],
}),
)
file: Express.Multer.File,
@Request() req: RequestWithUser, // ✅ 2. ระบุ Type ตรงนี้แทน any
) {
// ส่ง userId จาก Token ไปด้วย
return this.fileStorageService.upload(file, req.user.userId);
}
}