Skip to content

Speedy library to detect file type from initial part of file content

License

Notifications You must be signed in to change notification settings

hongquan/Defity

Repository files navigation

Defity

image loveimage pypiDocumentation Status

Speedy Python library to determine MIME type of file.

logo

Defity (Detectfiletype) is a library for Python application to guess file type in a reliable way, not based on filename extension (*.png,*.pdf), but on actual file content. It is like whatfilecommand andlibmagiclibrary do, but with different strategy.

📕 Documentation:defity.readthedocs.io

Install

$pip install defity

Usage

>>>importdefity
>>>defity.from_file('path/to/landscape.png')
'image/png'
>>>withopen('path/to/landscape.png','rb')asf:
...defity.from_file(f)
...
'image/png'

>>>defity.from_bytes(b'some-binary-content')
'image/png'

How different with libmagic-based ones?

There are many Python libraries also do the same thing, most of them are based on wellknownlibmagic.Defity is based on Rusttree_magic_minilibrary, which in turn is a fork oftree_magic,another Rust library. Quote fromtree_magicto see how it differs fromlibmagic:

Unlike the typical approach that libmagic and file(1) uses, this loads all the file types in a tree based on subclasses. (EX: application/vnd.openxmlformats-officedocument.wordprocessingml.document (MS Office 2007) subclasses application/zip which subclasses application/octet-stream) Then, instead of checking the file against every file type, it can traverse down the tree and only check the file types that make sense to check. (After all, the fastest check is the check that never gets run.)

This library also provides the ability to check if a file is a certain type without going through the process of checking it against every file type.

And whattree_magic_minihas improved overtree_magic:

Reduced copying and memory allocation, for a slight increase in speed and decrease in memory use.

So,Defityshould have better performance than other libraries for the same purpose.

Another advantage is that,Defityis static linked to the underlying Rust library, not depend on discretelibmagic.so.It will be easier to deploy to cloud function platforms, where you don't have control over what system libraries is present there.

License

In general, Defity is licensed under Apache-2.0 if it is built withouttree_magic_miniembedded MIME database, and is licensed under GPL-3.0 otherwise. Concretely:

  • On Linux, it is licensed under Apache-2.0.
  • On Windows and MacOS, it is licensed under GPL-3.0.

It is because, Linux boxes already come with FreeDesktop'sMIME database,Defity just uses it. Windows and MacOS don't have this database and Defity has to embed with it.

Credit

About

Speedy library to detect file type from initial part of file content

Resources

License

Stars

Watchers

Forks

Packages

No packages published