I saw in documentation of multer, that if folder doesn't exist, the multer will not create folder. How can i create a folder if not exist?
import multer from 'multer'
import crypto from 'crypto'
import { extname, resolve } from 'path'
import slug from 'slug'
export default {
storage: multer.diskStorage({
destination: resolve(__dirname, '..', '..', 'uploads', 'gallery'),
filename: (req, file, cb) => {
const { id, description } = req.body
crypto.randomBytes(8, (err, res) => {
if (err) return cb(err)
return cb(null, id + '/' + res.toString('hex') + '/' + slug(description, { lower: true }) + extname(file.originalname))
// return cb(null, res.toString('hex') + extname(file.originalname))
})
}
})
}
i had change to:
import multer from 'multer'
import crypto from 'crypto'
import { extname } from 'path'
import slug from 'slug'
import fs from 'fs'
export default {
storage: multer.diskStorage({
destination: (req, file, cb) => {
const { id } = req.body
const path = `./uploads/gallery/${id}`
fs.mkdirSync(path, { recursive: true })
return cb(null, path)
},
filename: (req, file, cb) => {
const { description } = req.body
crypto.randomBytes(3, (err, res) => {
if (err) return cb(err)
return cb(null, slug(description, { lower: true }) + '_' + res.toString('hex') + extname(file.originalname))
})
}
})
}