Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions fileValidator/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

const readChunk = require('read-chunk');
const fileType = require('file-type');
const { fileTypeFromBuffer, reasonableDetectionSizeInBytes } = require('file-type');
const textFormat = ['csv', 'txt', 'html', 'htm', 'css', 'ini', 'json', 'tsv', 'xml', 'yaml', 'yml', 'rst', 'md'];

function toArrayBuffer(buf, length) {
Expand Down Expand Up @@ -43,20 +43,20 @@ function validateOldMSOffice(options) {
return hex == 'D0CF11E0A1B11AE1';
}

function vatidateFile(options, ext) {
async function vatidateFile(options, ext) {
if (textFormat.indexOf(ext) > -1) return true; //validateTextFormat(options);
if (['doc', 'xls', 'ppt', 'msg'].indexOf(ext) > -1) return validateOldMSOffice(options);
let buffer = options.type == 'Binary' ? readChunk.sync(options.path, 0, fileType.minimumBytes) : toArrayBuffer(options.data, fileType.minimumBytes);
let buffer = options.type == 'Binary' ? readChunk.sync(options.path, 0, reasonableDetectionSizeInBytes) : toArrayBuffer(options.data, reasonableDetectionSizeInBytes);
//remove BOM encoding
if (ext == 'xml') {
let hex = options.type == 'Binary' ? getHex(readChunk.sync(options.path, 0, 3), 3) : getHex(options.data, 3);
if (hex == 'EFBBBF')
buffer = buffer.slice(3);
}
let fileTypeObj = fileType(buffer);
let fileTypeObj = await fileTypeFromBuffer(buffer);
if (!fileTypeObj) return false;
if ((fileTypeObj.ext == 'jpg' || fileTypeObj.ext == 'jpeg') && (ext == 'jpg' || ext == 'jpeg')) return true;
return fileTypeObj.ext == ext;
}

module.exports = vatidateFile;
module.exports = vatidateFile;