full project: admin panel, backend modules, docs
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
import { Controller, Post, UseGuards, Req, UploadedFile, UseInterceptors } from '@nestjs/common';
|
||||
import { Controller, Post, UseGuards, UploadedFile, UseInterceptors, BadRequestException } from '@nestjs/common';
|
||||
import { FileInterceptor } from '@nestjs/platform-express';
|
||||
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
||||
import { ApiTags, ApiBearerAuth, ApiBody, ApiConsumes } from '@nestjs/swagger';
|
||||
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
||||
import { StorageService } from './storage.service';
|
||||
import { StorageService, StoredFile } from './storage.service';
|
||||
|
||||
@ApiTags('Storage')
|
||||
@Controller('storage')
|
||||
@@ -12,8 +12,12 @@ export class StorageController {
|
||||
@Post('upload')
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
@ApiConsumes('multipart/form-data')
|
||||
@ApiBody({ schema: { type: 'object', properties: { file: { type: 'string', format: 'binary' } } } })
|
||||
@UseInterceptors(FileInterceptor('file'))
|
||||
upload(@UploadedFile() file: any) {
|
||||
return { url: this.storage.getUploadUrl(file?.originalname) };
|
||||
async upload(@UploadedFile() file: StoredFile) {
|
||||
if (!file) throw new BadRequestException('Archivo requerido');
|
||||
const url = await this.storage.save(file);
|
||||
return { url };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,45 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { writeFile, mkdir, unlink } from 'fs/promises';
|
||||
import { join } from 'path';
|
||||
import { randomUUID } from 'crypto';
|
||||
|
||||
export interface StoredFile {
|
||||
originalname: string;
|
||||
buffer: Buffer;
|
||||
mimetype: string;
|
||||
size: number;
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class StorageService {
|
||||
private uploadDir: string;
|
||||
private baseUrl: string;
|
||||
|
||||
constructor() {
|
||||
this.baseUrl = process.env.STORAGE_URL || 'http://localhost:9000';
|
||||
this.uploadDir = process.env.UPLOAD_DIR || join(process.cwd(), 'uploads');
|
||||
this.baseUrl = process.env.STORAGE_URL || `http://localhost:3000/uploads`;
|
||||
}
|
||||
|
||||
getUploadUrl(fileName: string) {
|
||||
return `${this.baseUrl}/uploads/${fileName}`;
|
||||
async save(file: StoredFile, subfolder = 'general'): Promise<string> {
|
||||
const dir = join(this.uploadDir, subfolder);
|
||||
await mkdir(dir, { recursive: true });
|
||||
|
||||
const ext = file.originalname.split('.').pop() || 'bin';
|
||||
const filename = `${randomUUID()}.${ext}`;
|
||||
const filepath = join(dir, filename);
|
||||
|
||||
await writeFile(filepath, file.buffer);
|
||||
|
||||
return `${this.baseUrl}/${subfolder}/${filename}`;
|
||||
}
|
||||
|
||||
async delete(url: string): Promise<void> {
|
||||
const relativePath = url.replace(this.baseUrl, '');
|
||||
const filepath = join(this.uploadDir, relativePath);
|
||||
await unlink(filepath).catch(() => {});
|
||||
}
|
||||
|
||||
getUploadUrl(fileName: string): string {
|
||||
return `${this.baseUrl}/${fileName}`;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user