24 lines
941 B
TypeScript
24 lines
941 B
TypeScript
import { Controller, Post, UseGuards, UploadedFile, UseInterceptors, BadRequestException } from '@nestjs/common';
|
|
import { FileInterceptor } from '@nestjs/platform-express';
|
|
import { ApiTags, ApiBearerAuth, ApiBody, ApiConsumes } from '@nestjs/swagger';
|
|
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
|
import { StorageService, StoredFile } from './storage.service';
|
|
|
|
@ApiTags('Storage')
|
|
@Controller('storage')
|
|
export class StorageController {
|
|
constructor(private storage: StorageService) {}
|
|
|
|
@Post('upload')
|
|
@UseGuards(JwtAuthGuard)
|
|
@ApiBearerAuth()
|
|
@ApiConsumes('multipart/form-data')
|
|
@ApiBody({ schema: { type: 'object', properties: { file: { type: 'string', format: 'binary' } } } })
|
|
@UseInterceptors(FileInterceptor('file'))
|
|
async upload(@UploadedFile() file: StoredFile) {
|
|
if (!file) throw new BadRequestException('Archivo requerido');
|
|
const url = await this.storage.save(file);
|
|
return { url };
|
|
}
|
|
}
|