Created
September 29, 2020 20:24
-
-
Save anushshukla/2000a3dfbedb3ff83a8b75b3aea7dc2a to your computer and use it in GitHub Desktop.
File uploading validations for MIME
This file contains hidden or 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
| const util = require('util'); | |
| const FileType = require('file-type'); | |
| var mmm = require('mmmagic'), | |
| Magic = mmm.Magic; | |
| var magic = new Magic(mmm.MAGIC_MIME_TYPE); | |
| const exec = util.promisify(require('child_process').exec); | |
| (async () => { | |
| console.log('FileType csv', await FileType.fromFile('/path/to/file-name.csv')); | |
| console.log('FileType png', await FileType.fromFile('/path/to/file-name.png')); | |
| magic.detectFile('/path/to/file-name.csv', (err, res) => | |
| console.log('magic csv', res) | |
| ) | |
| magic.detectFile('/path/to/file-name.png', (err, res) => | |
| console.log('magic csv', res) | |
| ) | |
| console.log('file csv', await exec('file --mime-type /path/to/file-name.csv | grep -oh "[a-z]*/[a-z]*$"', { shell: true })); | |
| console.log('file png', await exec('file --mime-type /path/to/file-png | grep -oh "[a-z]*/[a-z]*$"', { shell: true })); | |
| })().catch(console.error); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment