How to use the file-type.minimumBytes function in file-type

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 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 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 mserajnik / hyve / services / server / src / util / media.js View on Github external
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'
  }
}
github esrlabs / chipmunk / application / electron / src / controllers / files.parsers / file.parser.text.ts View on Github external
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(".", "");
github Emapic / emapic / utils.js View on Github external
getFileMetadata: function(input) {
            return fileType(Buffer.isBuffer(input) ? input : readChunk.sync(input, 0, fileType.minimumBytes));
        },
github rBurgett / system-font-families / src / main.js View on Github external
.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;
    });
github Lymphatus / caesium-image-compressor / src / models / CImage.js View on Github external
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(),
  };

file-type

Detect the file type of a Buffer/Uint8Array/ArrayBuffer

MIT
Latest version published 4 months ago

Package Health Score

90 / 100
Full package analysis