Nest – Armazenamento local de upload



This content originally appeared on DEV Community and was authored by Leonardo Minora

Códigos-fonte

import { Injectable } from '@nestjs/common';
import { promises as fs } from 'fs';
import * as path from 'path';

@Injectable()
export class ArmazenamentoService {
  private readonly dirLocal = path.join(__dirname, '..', 'uploads');

  async salvar(imagem: Express.Multer.File): Promise<string> {
    const arquivoNome = image.originalname;
    await fs.writeFile(path.join(this.dirLocal, arquivoNome), image.buffer);
    return arquivoNome;
  }

  async getImage(arquivoNome: string): Promise<Buffer | null> {
    try {
      const imagePath = path.join(this.imagesDir, filename);
      return await fs.readFile(imagePath);
    } catch (error) {
      return null;
    }
  }
}


This content originally appeared on DEV Community and was authored by Leonardo Minora