// multe file filters
const brandFileFilter = (req, file, cb) => {
// reject a file
if(file.mimetype === 'image/jpeg' || file.mimetype === 'image/png') {
cb(null, false)
} else {
cb(new Error('Only .jpeg or .png files are accepted'), true)
}
}
const itemFileFilter = (req, file, cb) => {
// reject a file
if(file.mimetype === 'image/jpeg' || file.mimetype === 'image/png') {
cb(null, false)
} else {
cb(new Error('Only .jpeg or .png files are accepted'), true)
}
}
module.exports.brandFileFilter = brandFileFilter
module.exports.itemFileFilter = itemFileFilter
// multer brand storage setup
const brandStorage = multer.diskStorage({
destination: async (req, file, cb) => {
const brandId = req.body.brandId
const dir = `./public/common/_data/brands/${ brandId }`
//create brand folder if there's no
mkdirp.sync(dir)
cb(null, dir)
},
filename: function (req, file, callback) {
callback(null, file.fieldname + '-' + moment().valueOf() + path.extname(file.originalname))
// I should define file extension
}
})
const brandUpload = multer({
storage : brandStorage,
limits : {
fieldSize: 1024 * 1024 * 10
},
// fileFilter: fileFilter
})
module.exports.brandUpload = brandUpload
// multer item storage setup
const itemStorage = multer.diskStorage({
destination : async (req, file, cb) => {
const brand_shortid = req.body.brand_shortid
const item_shortid = req.body.item_shortid
if (!brand_shortid) {
cb('missing brand_shortid data field')
console.log('missing brand_shortid data field')
return
}
if(!item_shortid){
cb('missing item_shortid data field')
console.log('missing item_shortid data field')
return
}
const dir = `./public/common/_data/brands/${brand_shortid}/${item_shortid}`
mkdirp.sync(dir)
cb(null, dir)
},
filename: function (req, file, callback) {
callback(null, file.fieldname + '-' + moment().valueOf() + path.extname(file.originalname))
// I should define file extension
}
})
const itemUpload = multer({
storage : itemStorage,
limits : {
fieldSize: 1024 * 1024 * 10
},
// fileFilter: fileFilter
})
const itemRegister = itemUpload.fields([
{ name: 'itemThumbnail', maxCount: 1 },
{ name: 'itemImage', maxCount: 5 }
])
const itemUpdate = itemUpload.fields([
{ name: 'itemThumbnail', maxCount: 1 },
{ name: 'itemImage', maxCount: 5 }
])
module.exports.itemRegister = itemRegister
module.exports.itemUpdate = itemUpdate