PiedTeam / DreameCloneTraining-BE-Project

0 stars 0 forks source link

Feature/issue 2/validate file format #24

Closed lcaohoanq closed 7 months ago

lcaohoanq commented 7 months ago

I've added the route "validate file format" when uploading file. The changed is too much because i have merge from main to take a newest structure when working with my branch on the old structure.

See #2 for more information

In this issue, i'm using:

interface IFileType {
  fieldname: string;
  originalname: string;
  encoding: string;
  mimetype: string;
  destination: string;
  filename: string;
  path: string;
  size: number;
}
const uploadDir = './src/module/upload';

export function initUploadDir(): string {
  if (!fs.existsSync(uploadDir)) {
    fs.mkdirSync(uploadDir);
  }
  return uploadDir;
}
export function configUploadFile(uploadDir: string): StorageEngine {
  const storage = diskStorage({
    destination: function (req, file, cb) {
      cb(null, uploadDir);
    },
    filename: function (req, file, cb) {
      cb(null, file.originalname + '-' + Date.now());
    },
  });
  return storage;
}
const upload = multer({ storage: multer.memoryStorage() }).single('myFile')
fileRouter.post('/upload', upload, fileValidator, uploadController)
nnh53 commented 7 months ago

@hoangday185 review for me