Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import fileType from 'file-type';
const buffer = new Buffer(1);
const array = new Uint8Array(buffer);
const type1 = fileType(buffer);
if (type1) {
type1.ext;
}
const type2 = fileType(array);
// $ExpectError
type2.ext;
if (type2) {
type2.mime;
}
// $ExpectError
fileType('');
import fileType from 'file-type';
const buffer = new Buffer(1);
const array = new Uint8Array(buffer);
const type1 = fileType(buffer);
if (type1) {
type1.ext;
}
const type2 = fileType(array);
// $ExpectError
type2.ext;
if (type2) {
type2.mime;
}
// $ExpectError
fileType('');
function getMIMEFromBuffer(buffer, path) {
const fileTypeFromBuffer = fileType(buffer);
if (fileTypeFromBuffer) {
// If fileType returns something for buffer, then return the mime given
return fileTypeFromBuffer.mime;
}
if (path) {
// If a path is supplied, and fileType yields no results, then retry with MIME
// Path can be either a file path or a url
return MIME.getType(path);
}
return null;
}
return request.get({ url, encoding: null, ...options.requsetOptions }).then((res) => {
const buffer = Buffer.from(res, 'utf-8');
const fileExtName = path.extname(url);
const fileExtType = fileType(buffer);
const md5 = crypto.createHash('md5');
const ext =
fileExtType && fileExtType.ext
? `.${fileExtType.ext}`
: fileExtName;
const basename = md5.update(buffer).digest('hex') + ext;
const contextPath = path
.join(
options.relativeCssPath,
options.outputPath,
basename
)
.replace(/\\/g, '/');
const outputPath = path
private setFileTypeDetails(): void {
const buffer = readChunk.sync(
this.sourceFilePath, 0, fileType.minimumBytes
);
const fileTypeDetails = fileType(buffer);
if (typeof fileTypeDetails !== 'undefined' || fileTypeDetails) {
this.fileTypeDetails['ext'] = fileTypeDetails.ext;
this.fileTypeDetails['mime'] = fileTypeDetails.mime;
}
}
.then(({ buffer, url }) => {
callback(null, {
statusCode: 200,
body: JSON.stringify({
provider: video.providerName,
url: video.url,
image: url,
mime: fileType(buffer).mime,
base64: `data:${fileType(buffer).mime};base64,${buffer.toString('base64')}`,
}),
});
})
.catch(error => {
export const isZip = (fileName, content) => {
if (typeof content === 'string') {
content = new Buffer(content);
}
const type = fileType(content);
return (type && type.mime === 'application/zip');
};
res.once('data', chunk => {
res.destroy()
resolve(fileType(chunk))
})
res.once('error', () => { reject(new Error('could not get file type')) })
function checkMimeType(path) {
let supportedMimTypes = config.get("mime_types");
let buffer = readChunk.sync(path, 0, 4100);
let type = fileType(buffer);
if (!supportedMimTypes.includes(type.mime)) {
throw boom.badRequest(
`Uploaded image has unsupported mime type "${type.mime}". Supported mime types are "${supportedMimTypes.join('","')}".`
);
}
return;
}
export async function imageType(file: string | Buffer) {
if (typeof file === 'string') {
file = await readChunk(file, 0, 12)
}
return fileType(file)
}