How to use the file-type 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 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 ice-lab / ice-scripts / packages / extract-css-assets-webpack-plugin / src / process-assets.js View on Github external
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
github LordDashMe / nodejs-image-squeezer / src / Utility / FileFormatResolver.ts View on Github external
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;
        }
    }
github marcomontalbano / video-to-markdown / src / lambda / image-json.js View on Github external
.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 => {
github electrode-io / electrode-ota-server / electrode-ota-server-model-manifest / src / manifest.js View on Github external
export const isZip = (fileName, content) => {
    if (typeof content === 'string') {
        content = new Buffer(content);
    }
    const type = fileType(content);
    return (type && type.mime === 'application/zip');
};
github SAPConversationalAI / bot-connector / src / utils / utils.js View on Github external
res.once('data', chunk => {
        res.destroy()
        resolve(fileType(chunk))
      })
      res.once('error', () => { reject(new Error('could not get file type')) })
github motivast / motimize / src / web / middlewares / handlers / image.js View on Github external
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;
}
github meowtec / Imagine / modules / common / file-utils.ts View on Github external
export async function imageType(file: string | Buffer) {
  if (typeof file === 'string') {
    file = await readChunk(file, 0, 12)
  }
  return fileType(file)
}

file-type

Detect the file type of a Buffer/Uint8Array/ArrayBuffer

MIT
Latest version published 5 months ago

Package Health Score

86 / 100
Full package analysis