Skip to content

Instantly share code, notes, and snippets.

@vagnercardosoweb
Created April 29, 2022 18:23
Show Gist options
  • Select an option

  • Save vagnercardosoweb/a202283a507747ef34db2a0b8167c92c to your computer and use it in GitHub Desktop.

Select an option

Save vagnercardosoweb/a202283a507747ef34db2a0b8167c92c to your computer and use it in GitHub Desktop.
import fs from 'fs';
import path from 'path';
import { mkdirp } from '@/utils';
interface RequestSave {
file: Express.Multer.File;
directory: string;
name: string;
}
class UploadFile {
public async save({ file, name, directory }: RequestSave): Promise<string> {
await mkdirp(directory);
const ext = `${path.extname(file.originalname).slice(1)}`;
const filename = `${name}.${ext}`;
await fs.promises.writeFile(path.resolve(directory, filename), file.buffer);
return filename;
}
public async remove(directoryWithName: string): Promise<void> {
try {
await fs.promises.rm(directoryWithName);
} catch {
//
}
}
}
const sharedUploadFile = new UploadFile();
export default sharedUploadFile;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment