Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
static getFileType(filePath) {
try {
const buffer = readChunk.sync(filePath, 0, fileType.minimumBytes);
// returns {ext, mime} if found, null if not.
const file = fileType(buffer);
// if a file type was detected correctly, return it
if (file) {
return file.mime;
}
// if the buffer is a valid UTF-8 buffer, use text/plain
if (isUtf8(buffer)) {
return "text/plain";
}
// otherwise assume it's random binary data
return "application/octet-stream";
async function mimeTypeDetect(fileName, filePath) {
// The file type is detected by checking the magic number of the buffer.
// It only needs the first "minimumBytes" bytes.
const buffer = await readChunk(filePath, 0, fileType.minimumBytes);
const info = fileType(buffer);
if (info && info.mime && info.mime !== 'application/octet-stream') {
return info.mime;
}
// legacy mmmagic based detection
let mimeType = 'application/octet-stream';
try {
mimeType = await detectMime(filePath);
if (mimeType === 'application/octet-stream') {
const typeOfFile = await detectFile(filePath);
if (typeOfFile.startsWith('Audio file with ID3')) {
validateFileType : async function (filePath) {
let supportedTypes = ['application/pdf', 'image/tiff', 'image/jpeg', 'image/png', 'image/gif'];
try{
const buffer = readChunk.sync(filePath, 0, fileType.minimumBytes);
for (let tp of supportedTypes) {
if (fileType(buffer) !== undefined && fileType(buffer).mime === tp) {
return true;
}
}
return false;
}catch(error){
console.log(error);
throw error;
}
return false;
},
processAPIKey : function (apiKey) {
Module._load = function(request, parent) {
let buffer;
try {
buffer = readChunk.sync(request, 0, fileType.minimumBytes);
} catch {
return originalLoader.apply(this, arguments);
}
const type = fileType(buffer).mime;
if (type.split('/')[0] != 'image')
return originalLoader.apply(this, arguments);
const size = sizeOf(request);
return { uri: request, width: size.width, height: size.height };
};
async _fileInfo(file) {
const bufferExt = readChunk.sync(file.path, 0, fileType.minimumBytes);
let fileExt = fileType(bufferExt);
if (fileExt) {
fileExt = fileExt.ext;
}
else {
const ext = path.extname(file.name).split('.');
fileExt = ext[ext.length - 1];
}
const checksum = await this._checkSum(file.path);
const fileSize = fse.statSync(file.path).size;
return { fileExt, checksum, fileSize };
}
const directory = config.hydrusFilesMode === 'client'
? type === 'thumbnail'
? `t${hash.substring(0, 2)}`
: `f${hash.substring(0, 2)}`
: hash.substring(0, 2)
const extension = type === 'thumbnail' ? '.thumbnail' : ''
const filePath = type !== 'thumbnail' && config.hydrusFilesMode === 'client'
? await getFilePathWithExtension(
path.join(config.hydrusFilesPath, directory),
hash
)
: path.join(basePath, directory, `${hash}${extension}`)
const fileInfo = fileType(
await readChunk(filePath, 0, fileType.minimumBytes)
)
return {
path: filePath,
mimeType: fileInfo && fileInfo.mime
? fileInfo.mime
: 'application/octet-stream'
}
}
fs.open(file, "r", (openError: NodeJS.ErrnoException | null, fd: number) => {
if (openError) {
return reject(openError);
}
const buffer: Buffer = Buffer.alloc(ft.minimumBytes);
fs.read(
fd,
buffer,
0,
ft.minimumBytes,
0,
(readError: NodeJS.ErrnoException | null, read: number, buf: Buffer) => {
if (readError) {
return reject(readError);
}
const type: ft.FileTypeResult | undefined = ft(buf);
if (type === undefined) {
const extname: string = path
.extname(file)
.toLowerCase()
.replace(".", "");
getFileMetadata: function(input) {
return fileType(Buffer.isBuffer(input) ? input : readChunk.sync(input, 0, fileType.minimumBytes));
},
.filter(f => {
const extension = path.extname(f).toLowerCase();
if (extension === '.ttf' || extension === '.otf') {
return true;
}
const fontFileHeader = readChunk.sync(f, 0, getFileType.minimumBytes);
const fileType = getFileType(fontFileHeader);
if (!fileType) {
return false;
}
if (fileType.ext === 'ttf' || fileType.ext === 'otf') {
return true;
}
return false;
});
function create(filepath) {
const buffer = readChunk.sync(filepath, 0, fileType.minimumBytes);
if (!SUPPORTED_FILETYPES.includes(fileType(buffer).mime)) {
return {};
}
const resolution = sizeOf(filepath);
const { size } = fs.statSync(filepath);
return {
fullpath: filepath,
name: path.basename(filepath),
size,
formattedSize: filesize(size),
width: resolution.width,
height: resolution.height,
resolution: `${resolution.width}x${resolution.height}`,
status: FILE_STATUSES.PENDING,
uuid: uuidv4(),
};