import multer from 'multer'; // import moment from 'moment-timezone'; import { storage as storageConfig } from '../../../config/vars'; /** storage will create folder when new date */ // const date = new Date(); // const year = moment(date).format('YYYY'); // const month = moment(date).format('MM'); const filePath = `${storageConfig.uri}`; const createDefaultFolder = ({ id }) => { try { if (id) { multer({ dest: `${filePath}/${id}` }); } else { multer({ dest: `${filePath}` }); } // multer({ dest: `${filePath}/images/games` }); // multer({ dest: `${filePath}/images/stories` }); // multer({ dest: `${filePath}/images/chapters` }); // multer({ dest: `${filePath}/images/vouchers` }); // multer({ dest: `${filePath}/images/products` }); // multer({ dest: `${filePath}/images/customers` }); // multer({ dest: `${filePath}/images/promotions` }); // // configuration // multer({ dest: `${filePath}/images/banners` }); // multer({ dest: `${filePath}/images/categories` }); // multer({ dest: `${filePath}/images/upload/default` }); return true; } catch (ex) { return false; } }; const createFolder = ({ path }) => { try { console.log(`${filePath}/${path}`); multer({ dest: `${filePath}/${path}` }); // multer({ dest: `${filePath}/images/games` }); // multer({ dest: `${filePath}/images/stories` }); // multer({ dest: `${filePath}/images/chapters` }); // multer({ dest: `${filePath}/images/vouchers` }); // multer({ dest: `${filePath}/images/products` }); // multer({ dest: `${filePath}/images/customers` }); // multer({ dest: `${filePath}/images/promotions` }); // // configuration // multer({ dest: `${filePath}/images/banners` }); // multer({ dest: `${filePath}/images/categories` }); // multer({ dest: `${filePath}/images/upload/default` }); return true; } catch (ex) { return false; } }; /** add image to storage follow group */ const storage = multer.diskStorage({ destination: (req, file, cb) => { cb(null, filePath); }, filename: (req, file, cb) => { /** * setup folder follow date */ createDefaultFolder({}); console.log('run in herer', req.query.path, file); /** * save image follow type */ const path = req.query.path; // const fileName = file.originalname.includes('.') // ? file.originalname.slice(0, file.originalname.lastIndexOf('.')) // : file.originalname; cb( null, `/${path}/${file.originalname}` ); } }); const fileFilter = (req, file, cb) => { // if ( // file.mimetype === 'image/jpeg' || // file.mimetype === 'image/webp' || // file.mimetype === 'image/png' || // file.mimetype === 'image/gif' || // ) { // cb(null, true); // } else { // cb(null, false); // } cb(null, true); }; const uploader = multer({ storage, limits: { fileSize: 1024 * 1024 * 2048 // 5MB }, fileFilter }); module.exports = { createDefaultFolder, createFolder, uploader, fileFilter };