Skip to content

Instantly share code, notes, and snippets.

@anushshukla
Created September 29, 2020 20:24
Show Gist options
  • Select an option

  • Save anushshukla/2000a3dfbedb3ff83a8b75b3aea7dc2a to your computer and use it in GitHub Desktop.

Select an option

Save anushshukla/2000a3dfbedb3ff83a8b75b3aea7dc2a to your computer and use it in GitHub Desktop.
File uploading validations for MIME
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