-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #605 from idurar/feat/improve-upload-file-with-filter
✨ Improve Upload Exp with file filter
- Loading branch information
Showing
17 changed files
with
234 additions
and
57 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,80 @@ | ||
const fileFilter = (req, file, cb) => { | ||
// array containing all the possible file types | ||
const _fileType = [ | ||
'image/jpeg', | ||
'image/png', | ||
'image/gif', | ||
'image/webp', | ||
'application/msword', | ||
'text/plain', | ||
'text/csv', | ||
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', | ||
'application/vnd.ms-excel', | ||
'application/pdf', | ||
'application/zip', | ||
'application/vnd.rar', | ||
'video/mp4', | ||
'video/x-msvideo', | ||
'audio/mpeg', | ||
'video/webm', | ||
]; | ||
const fileFilter = | ||
(type = 'default') => | ||
(req, file, cb) => { | ||
// array containing all the possible file types | ||
const _fileType = [ | ||
'image/jpeg', | ||
'image/png', | ||
'image/gif', | ||
'image/webp', | ||
'application/msword', | ||
'text/plain', | ||
'text/csv', | ||
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', | ||
'application/vnd.ms-excel', | ||
'application/pdf', | ||
'application/zip', | ||
'application/vnd.rar', | ||
'video/mp4', | ||
'video/x-msvideo', | ||
'audio/mpeg', | ||
'video/webm', | ||
]; | ||
|
||
let _flag = _fileType.includes(file.mimetype); | ||
let _flag = _fileType.includes(file.mimetype); | ||
|
||
if (_flag) { | ||
return cb(null, true); | ||
} else { | ||
return cb(new Error(`${file.mimetype} File type not supported!`)); | ||
} | ||
}; | ||
if (type === 'image') { | ||
if (!file.mimetype.startsWith('image/')) { | ||
_flag = false; | ||
} | ||
} else if (type === 'pdf') { | ||
if (!file.mimetype.startsWith('application/pdf')) { | ||
_flag = false; | ||
} | ||
} else if (type === 'video') { | ||
if (!file.mimetype.startsWith('video/')) { | ||
_flag = false; | ||
} | ||
} else if (type === 'audio') { | ||
if (!file.mimetype.startsWith('audio/')) { | ||
_flag = false; | ||
} | ||
} else if (type === 'text') { | ||
if ( | ||
!file.mimetype.startsWith('text/') && | ||
!file.mimetype.startsWith('application/vnd.ms-excel') && | ||
!file.mimetype.startsWith('application/msword') && | ||
!file.mimetype.startsWith( | ||
'application/vnd.openxmlformats-officedocument.wordprocessingml.document' | ||
) | ||
) { | ||
_flag = false; | ||
} | ||
} else if (type === 'excel') { | ||
if ( | ||
!file.mimetype.startsWith('application/vnd.ms-excel') && | ||
!file.mimetype.startsWith( | ||
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' | ||
) | ||
) { | ||
_flag = false; | ||
} | ||
} else if (type === 'compressed') { | ||
if ( | ||
!file.mimetype.startsWith('application/zip') && | ||
!file.mimetype.startsWith('application/x-zip-compressed') && | ||
!file.mimetype.startsWith('application/x-zip-compressed') && | ||
!file.mimetype.startsWith('application/vnd.rar') | ||
) { | ||
_flag = false; | ||
} | ||
} | ||
|
||
if (_flag) { | ||
return cb(null, true); | ||
} else { | ||
return cb(new Error(`${file.mimetype} File type not supported!`)); | ||
} | ||
}; | ||
|
||
module.exports = fileFilter; |
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
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
56 changes: 56 additions & 0 deletions
56
backend/middlewares/uploadMiddleware/singleStorageUpload.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
const multer = require('multer'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const { slugify } = require('transliteration'); | ||
|
||
const fileFilter = require('./fileFilter'); | ||
|
||
const singleStorageUpload = ({ | ||
entity, | ||
filename = 'default', | ||
fileType = 'default', | ||
fieldName = 'file', | ||
}) => { | ||
var diskStorage = multer.diskStorage({ | ||
destination: function (req, file, cb) { | ||
cb(null, `public/uploads/${entity}`); | ||
}, | ||
filename: function (req, file, cb) { | ||
// fetching the file extention of the uploaded file | ||
let fileExtension = path.extname(file.originalname); | ||
let uniqueFileID = Math.random().toString(36).slice(2, 7); //generates unique ID of length 5 | ||
|
||
let originalname = ''; | ||
if (req.body.seotitle) { | ||
originalname = slugify(req.body.seotitle.toLocaleLowerCase()); //convert any language to english characters | ||
} else { | ||
originalname = slugify(file.originalname.split('.')[0].toLocaleLowerCase()); //convert any language to english characters | ||
} | ||
|
||
let _fileName = `${originalname}-${uniqueFileID}${fileExtension}`; | ||
|
||
const filePath = `public/uploads/${entity}/${_fileName}`; | ||
// saving file name and extention in request upload object | ||
req.upload = { | ||
fileName: _fileName, | ||
fieldExt: fileExtension, | ||
entity: entity, | ||
fieldName: fieldName, | ||
fileType: fileType, | ||
filePath: filePath, | ||
}; | ||
|
||
req.body[fieldName] = filePath; | ||
|
||
return cb(null, _fileName); | ||
}, | ||
}); | ||
|
||
let filterType = fileFilter(fileType); | ||
|
||
const multerStorage = multer({ storage: diskStorage, fileFilter: filterType }).single('file'); | ||
|
||
return multerStorage; | ||
}; | ||
|
||
module.exports = singleStorageUpload; |
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
Binary file added
BIN
+55.2 KB
backend/public/uploads/admin/_47bb8679-c5c3-475a-8533-96d95ff1f085-zte99.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+341 KB
backend/public/uploads/admin/ed6a298375f3e822683f6ba47750e30b-sbs2c.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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
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
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
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
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
Oops, something went wrong.