Spaces:
Sleeping
Sleeping
| 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'; | |
| () | |
| export class AppController { | |
| constructor(private readonly appService: AppService) {} | |
| () | |
| getHello(): string { | |
| return this.appService.getHello(); | |
| } | |
| ('api/upload') | |
| ( | |
| 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(() 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}`, | |
| }; | |
| } | |
| } | |