2022-02-03 10:06:44 -06:00
|
|
|
import { HttpException, HttpStatus } from '@nestjs/common';
|
|
|
|
|
import { MulterOptions } from '@nestjs/platform-express/multer/interfaces/multer-options.interface';
|
|
|
|
|
import { existsSync, mkdirSync } from 'fs';
|
|
|
|
|
import { diskStorage } from 'multer';
|
|
|
|
|
import { extname } from 'path';
|
|
|
|
|
import { Request } from 'express';
|
2022-02-11 22:23:06 -06:00
|
|
|
import { APP_UPLOAD_LOCATION } from '../constants/upload_location.constant';
|
2022-02-03 10:06:44 -06:00
|
|
|
|
|
|
|
|
export const multerConfig = {
|
2022-02-11 22:23:06 -06:00
|
|
|
dest: APP_UPLOAD_LOCATION,
|
2022-02-03 10:06:44 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const multerOption: MulterOptions = {
|
|
|
|
|
fileFilter: (req: Request, file: any, cb: any) => {
|
2022-02-09 20:48:06 -06:00
|
|
|
if (file.mimetype.match(/\/(jpg|jpeg|png|gif|mp4|x-msvideo|quicktime|heic|heif)$/)) {
|
2022-02-03 10:06:44 -06:00
|
|
|
cb(null, true);
|
|
|
|
|
} else {
|
|
|
|
|
cb(new HttpException(`Unsupported file type ${extname(file.originalname)}`, HttpStatus.BAD_REQUEST), false);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
storage: diskStorage({
|
|
|
|
|
destination: (req: Request, file: Express.Multer.File, cb: any) => {
|
|
|
|
|
const uploadPath = multerConfig.dest;
|
|
|
|
|
|
|
|
|
|
const userPath = `${uploadPath}/${req.user['id']}/original/${req.body['deviceId']}`;
|
|
|
|
|
|
|
|
|
|
if (!existsSync(userPath)) {
|
|
|
|
|
mkdirSync(userPath, { recursive: true });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cb(null, userPath);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
filename: (req: Request, file: Express.Multer.File, cb: any) => {
|
2022-03-07 11:39:54 -06:00
|
|
|
cb(null, `${file.originalname.split('.')[0]}${req.body['fileExtension']}`);
|
2022-02-03 10:06:44 -06:00
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
};
|