How to use file-type - 10 common examples

To help you get started, we’ve selected a few file-type examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github flow-typed / flow-typed / definitions / npm / file-type_v3.x.x / test_file-type_v3.x.x.js View on Github external
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('');
github flow-typed / flow-typed / definitions / npm / file-type_v3.x.x / test_file-type_v3.x.x.js View on Github external
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('');
github oliver-moran / jimp / packages / core / src / utils / image-bitmap.js View on Github external
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;
}
github thelounge / thelounge / src / plugins / uploader.js View on Github external
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";
github FreeFeed / freefeed-server / app / models / attachment.js View on Github external
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')) {
github Yoctol / bottender / src / cli / actions / __tests__ / uploadImages.spec.js View on Github external
it('call pushFile when checksum is different', async () => {
    const mockPushFile = jest.fn();

    hasha.fromFileSync = jest
      .fn()
      .mockReturnValueOnce('qazwsx')
      .mockReturnValueOnce('wsxedc');
    yoctolStatic.__setMockPushFile(mockPushFile);
    fileType.mockReturnValue({
      mime: 'image/png',
    });
    await uploadImages({ folderPath });
    expect(mockPushFile).toHaveBeenCalledTimes(2);
  });
github sypht-team / sypht-node-client / lib / utils.js View on Github external
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) {
github kusti8 / proton-native / src / utils / requireImpl.js View on Github external
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 };
};
github kube-HPC / hkube / core / api-server / lib / service / builds.js View on Github external
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 };
    }
github macacajs / NoSmoke / lib / tesseract / src / node / index.js View on Github external
.catch(err => console.error(err));
    }

    if(typeof image === 'string'){
        fs.readFile(image, function(err, buffer){
            if (err) throw err;
            loadImage(buffer, cb);
        });
        return;
    } else if (image instanceof Buffer){
        if (!image.length) {
            console.log("oops");
            return;
        }

        var mime = require('file-type')(image).mime

        if(mime === 'image/png'){
            var PNGReader = require('png.js');
            var reader = new PNGReader(image);
            reader.parse(function(err, png){
                if (err) throw err;

                var image = {
                    width: png.getWidth(),
                    height: png.getHeight()
                }
                image.data = new Uint8Array(image.width * image.height * 4)
                for(var j = 0; j < image.height; j++){
                    for(var i = 0; i < image.width; i++){
                        var offset = 4 * (i + j * image.width),
                            pix = png.getPixel(i, j);

file-type

Detect the file type of a file, stream, or data

MIT
Latest version published 2 months ago

Package Health Score

97 / 100
Full package analysis