Last active
February 7, 2025 10:24
-
-
Save igorfonseca05/1bc3864aa153bdfdd5502441332a0d09 to your computer and use it in GitHub Desktop.
Setting up multer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Usando multer para uploads de arquivos | |
const upload = multer({ | |
storage: multer.diskStorage({ | |
destination: (req, file, cb) => { | |
cb(null, 'src/uploads') | |
}, | |
filename: (req, file, cb) => { | |
const uniqueName = `${Date.now()}-${Math.random() * 1e9}${path.extname(file.originalname)}` | |
cb(null, uniqueName) | |
} | |
}), | |
limits: { fileSize: 1 * 1024 * 1024 }, // 1MB | |
fileFilter: (req, file, cb) => { | |
if (file.originalname.match(/\.(png|jpg|jpeg)$/)) { | |
return cb(null, file) | |
} | |
cb(new Error('Formato de arquivo inválido')) | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment