require('dotenv')
const multer = require('multer')
const sftpMulter = require('multer-sftp')
const ftpMulter = require('multer-ftp')
const path = require('path')
const crypto = require('crypto')
const storageTypes = {
localStorage: multer.diskStorage({
destination: (request, file, cb)=>{
cb(null, path.resolve(__dirname, '..', '/uploads'))
},
filename: (request, file, cb)=>{
crypto.randomBytes(4, (err, hash)=>{
if(err) {
cb(err, 'tente novamente')
}
const nomeEmpresa = request.body.empresa.slice(0, 5).normalize('NFD')
.replace(/:|,|;|`|´|\?|%|+|=|^|~|[|]|||{|}|@|!|\$|&|*|#|\s|[\u0300-\u036f]/gi, "")
const arquivoOriginalNome = file.originalname.slice(file.originalname.length - 5, file.originalname.length).normalize('NFD')
.replace(/:|,|;|`|´|\?|%|\+|\=|\^|~|\[|\]|\||{|}|@|!|\$|&|\*|#|\s|[\u0300-\u036f]/gi, "")
const fileName = `${nomeEmpresa}_${hash.toString('HEX')}_${arquivoOriginalNome}`
cb(null, fileName)
})
},
}),
ftpStorage: multer({ storage: sftpMulter({
sftp: {
host: process.env.REACT_APP_BASE_HOST_FTP,
secure: false,
port: process.env.REACT_APP_BASE_PORT_FTP,
user: process.env.REACT_APP_BASE_USER_FTP,
pass: process.env.REACT_APP_BASE_PWD_FTP,
debugMode: true
},
destination: (request, file, cb)=> {
console.log(file, request.body)
cb(null, '/fastdating')
},
filename: (request, file, cb)=> {
crypto.randomBytes(4, (err, hash)=>{
if(err) {
Console.error(err)
cb(err, 'tente novamente')
}
const nomeEmpresa = request.body.empresa.slice(0, 5).normalize('NFD')
.replace(/:|,|;|`|´|\?|%|\+|\=|\^|~|\[|\]|\||{|}|@|!|\$|&|\*|#|\s|[\u0300-\u036f]/gi, "")
const arquivoOriginalNome = file.originalname.slice(file.originalname.length - 5, file.originalname.length).normalize('NFD')
.replace(/:|,|;|`|´|\?|%|\+|\=|\^|~|\[|\]|\||{|}|@|!|\$|&|\*|#|\s|[\u0300-\u036f]/gi, "")
const fileName = `${nomeEmpresa}_${hash.toString('HEX')}_${arquivoOriginalNome}`
cb(null, fileName)
})
}
})
})
}
module.exports = {
/* saida: (request)=>console.log('entrando...', request), */
storage: storageTypes.ftpStorage,
limits: {
fileSize: 4 * 1024 * 1024,
},
fileFilter: (request, file, cb) => {
const allowedMimes = [ 'application/pdf' ]
if (allowedMimes.includes(file.mimetype.toLowerCase())) {
cb(null, true)
}
if (!(allowedMimes.includes(file.mimetype.toLowerCase()))) {
cb(new Error('Só pdf '))
}else{
new Error()
}
},
}
Top comments (1)
Someone who able to me help me?