-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add validation of file types * Add more file and mime types * Fix function description * Add pdf type --------- Co-authored-by: igoychev <[email protected]>
- Loading branch information
Showing
7 changed files
with
95 additions
and
8 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import path from 'path' | ||
|
||
interface File { | ||
fieldname: string | ||
originalname: string | ||
encoding: string | ||
mimetype: string | ||
size: number | ||
destination: string | ||
filename: string | ||
path: string | ||
buffer: Buffer | ||
} | ||
|
||
/** | ||
* The function is used to validate the type of a file using the file extension and MIME type | ||
* @param file object that represent file | ||
* @param cb callback function which indicates whether file is accepted or not | ||
*/ | ||
export function validateFileType( | ||
file: File, | ||
cb: (error: Error | null, acceptFile: boolean) => void, | ||
) { | ||
const mimeAllowlist = [ | ||
'text/plain', | ||
'application/json', | ||
'application/pdf', | ||
'image/png', | ||
'image/jpeg', | ||
'application/xml', | ||
'text/xml', | ||
'application/msword', | ||
'application/vnd.ms-excel', | ||
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', | ||
] | ||
if (!mimeAllowlist.includes(file.mimetype)) { | ||
return cb(new Error('File mime type is not allowed'), false) | ||
} | ||
|
||
const allowedExtensions = /txt|json|pdf|jpeg|jpg|png|xml|xlsx|xls|docx/ | ||
|
||
const isExtensionSupported = allowedExtensions.test(path.extname(file.originalname).toLowerCase()) | ||
if (!isExtensionSupported) { | ||
return cb(new Error('File extension is not allowed'), false) | ||
} | ||
|
||
cb(null, true) | ||
} |
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