npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@yiiran/get-file-type

v1.0.3

Published

Determine the file type of 'pdf,xls,xlsx,doc,docx,ppt,pptx' according to the file arraybuffer. File suffix or file name is not required. Further more, it can avoid wrong judgment by the falsy changes of file suffix.

Downloads

511

Readme

getFileTypeFromArrayBuffer

Purpose:

Determine the file type of 'pdf,xls,xlsx,doc,docx,ppt,pptx' according to the file arraybuffer achieved by js. File suffix or file name is not required. Further more, it can avoid wrong judgment by the falsy changes of file suffix.

用途: 通过arraybuffer格式的文件流判断文件类型。类型范围是‘pdf,xls,xlsx,doc,docx,ppt,pptx'’中的一种。无需后端开发同事传递给你文件名称。同时它可以防止由于文件后缀名被非法更改导致的判断错误。

Usage:

Step1. Install

 $ npm install --save @yiiran/get-file-type  

Step2. Get arraybuffer file data

You may often get it in the following circumstance:
输入的arraybuffer数据通常在以下场景中得到:

import getFileTypeFromArrayBuffer from '@yiiran/get-file-type';

let req = new XMLHttpRequest();
req.open("GET", "http://someFileUrl");
req.responseType = "arraybuffer";  //arraybuffer
req.onload = function (e) {
    let fileType = getFileTypeFromArrayBuffer(req.response);
    console.log('fileType', fileType)
};
req.send();

Output:

One of 'pdf',' xlsx', 'xls', 'docx', 'doc', 'pptx','ppt', 'file2003', 'file2007', 'other'.

'file2003' meas the file belongs to the versions under Microsoft Office 2007 standard but dosen't belong to any kind of 'xls','doc','ppt'.
'file2007' meas the file belongs to the version of Microsoft Office 2007 standard but dosen't belong to any kind of 'xlsx','docx','pptx'.
'other' means matching failure.

输出:以上十种字符串中的一种。
‘file2003’代表该文件遵守的是Microsoft Office 2007标准之前的旧标准,但不属于'xls','doc','ppt'类型;
‘file2007’代表该文件遵守Microsoft Office 2007新标准,但不属于'xlsx','docx','pptx'类型;
如果结果为‘other’,说明未匹配成功以上任何一种类型。

Notes:

1.Don't transfer the original ArrayBuffer Type to TypedArray or DataView.
2.Due to possible differences in a few file streams, it's possible to get result incorrectly. The util is recommended to use with other tools.
说明:
1.不要将原始的ArrayBuffer数据转化成类型化数组(Uint8Array等),或DataView视图。
2.由于少数文件结构存在差异的可能,因此判断结果可能出现失误。建议结合其他判断方式共同使用,以增加保险。