import { Controller, Get, Post, UseInterceptors, UploadedFile, } from '@nestjs/common'; import { FileInterceptor } from '@nestjs/platform-express'; import { AppService } from './app.service'; import { diskStorage } from 'multer'; import { extname } from 'path'; @Controller() export class AppController { constructor(private readonly appService: AppService) {} @Get() getHello(): string { return this.appService.getHello(); } @Post('api/upload') @UseInterceptors( FileInterceptor('file', { storage: diskStorage({ destination: './uploads', filename: (req, file, cb) => { const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1e9); cb(null, `${uniqueSuffix}${extname(file.originalname)}`); }, }), }), ) uploadFile(@UploadedFile() file: Express.Multer.File) { // Return the URL to access the uploaded file // Note: ensure we serve this directory statically return { success: true, url: `/uploads/${file.filename}`, }; } }