upload.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. const multer = require('multer');
  2. const path = require('path');
  3. require('dotenv').config();
  4. const DIR_UPLOAD = path.join(process.cwd(), process.env.DIR_UPLOAD);
  5. const storage = multer.diskStorage({
  6. destination: function (_req, _file, cb) {
  7. cb(null, DIR_UPLOAD);
  8. },
  9. filename: function (_req, file, cb) {
  10. cb(null, file.originalname);
  11. },
  12. });
  13. const uploadImage = multer({
  14. storage: storage,
  15. limits: { fileSize: 8000000 },
  16. fileFilter: (_req, file, cb) => {
  17. if (file.mimetype.includes('image')) return cb(null, true);
  18. cb(null, false);
  19. },
  20. });
  21. const uploadAudio = multer({
  22. storage: storage,
  23. limits: { fileSize: 20000000 },
  24. fileFilter: (_req, file, cb) => {
  25. if (file.mimetype.includes('audio')) return cb(null, true);
  26. cb(null, false);
  27. },
  28. });
  29. const uploadVideo = multer({
  30. storage: storage,
  31. limits: { fileSize: 200000000 },
  32. fileFilter: (_req, file, cb) => {
  33. if (file.mimetype.includes('video')) return cb(null, true);
  34. cb(null, false);
  35. },
  36. });
  37. const uploadFile = multer({
  38. storage: storage,
  39. limits: { fileSize: 200000000 },
  40. fileFilter: (_req, file, cb) => {
  41. if (file.mimetype.includes('application')) return cb(null, true);
  42. cb(null, false);
  43. },
  44. });
  45. module.exports = { uploadImage, uploadAudio, uploadVideo, uploadFile };