diff --git a/src/api/controllers/v1/path.controller.js b/src/api/controllers/v1/path.controller.js index 4fd871e..2465907 100644 --- a/src/api/controllers/v1/path.controller.js +++ b/src/api/controllers/v1/path.controller.js @@ -357,3 +357,4 @@ exports.deleteMultiple = (req, res, next) => { }; + diff --git a/src/common/services/adapters/upload-adapter.js b/src/common/services/adapters/upload-adapter.js index 6c0e17e..bf4a872 100644 --- a/src/common/services/adapters/upload-adapter.js +++ b/src/common/services/adapters/upload-adapter.js @@ -1,3 +1,4 @@ +<<<<<<< HEAD import multer from 'multer'; // import moment from 'moment-timezone'; import { storage as storageConfig } from '../../../config/vars'; @@ -122,3 +123,119 @@ module.exports = { uploader, fileFilter }; +======= +import multer from 'multer'; +// import moment from 'moment-timezone'; +import { storage as storageConfig } from '../../../config/vars'; +import { forIn } from 'lodash'; +import { equal } from 'joi'; + +/** 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` }); + // console.log(multer); + 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.replace(/\s/g, '')}` + ); + } +}); + +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 * 5048 // 5MB + }, + fileFilter +}); + +module.exports = { + createDefaultFolder, + createFolder, + uploader, + fileFilter +}; +>>>>>>> 7bd4b58a0df174b4a8c26c34b9d388fc8ab3c4fd