Skip to content

Commit

Permalink
Merge pull request #95 from dosas/feature/add-missing-types
Browse files Browse the repository at this point in the history
Add some missing types from go version
  • Loading branch information
h2non authored Dec 29, 2020
2 parents 54a34c8 + b254b95 commit cff02bf
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 1 deletion.
11 changes: 11 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ Supported types
Image
^^^^^

- **dwg** - ``image/vnd.dwg``
- **xcf** - ``image/x-xcf``
- **jpg** - ``image/jpeg``
- **jpx** - ``image/jpx``
- **png** - ``image/png``
Expand All @@ -78,6 +80,7 @@ Image
Video
^^^^^

- **3gp** - ``video/3gpp``
- **mp4** - ``video/mp4``
- **m4v** - ``video/x-m4v``
- **mkv** - ``video/x-matroska``
Expand All @@ -91,6 +94,7 @@ Video
Audio
^^^^^

- **aac** - ``audio/aac``
- **mid** - ``audio/midi``
- **mp3** - ``audio/mpeg``
- **m4a** - ``audio/m4a``
Expand All @@ -102,6 +106,8 @@ Audio
Archive
^^^^^^^

- **rpm** - ``application/x-rpm``
- **dcm** - ``application/dicom``
- **epub** - ``application/epub+zip``
- **zip** - ``application/zip``
- **tar** - ``application/x-tar``
Expand Down Expand Up @@ -134,6 +140,11 @@ Font
- **ttf** - ``application/font-sfnt``
- **otf** - ``application/font-sfnt``

Application
^^^^

- **wasm** - ``application/wasm``

.. _Python: http://python.org
.. _magic numbers: https://en.wikipedia.org/wiki/Magic_number_(programming)#Magic_numbers_in_files
.. _filetype: https://github.com/h2non/filetype
Expand Down
18 changes: 18 additions & 0 deletions filetype/match.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from .types import ARCHIVE as archive_matchers
from .types import AUDIO as audio_matchers
from .types import APPLICATION as application_matchers
from .types import FONT as font_matchers
from .types import IMAGE as image_matchers
from .types import VIDEO as video_matchers
Expand Down Expand Up @@ -117,3 +118,20 @@ def archive_match(obj):
TypeError: if obj is not a supported type.
"""
return match(obj, archive_matchers)


def application_match(obj):
"""
Matches the given input againts the available
application type matchers.
Args:
obj: path to file, bytes or bytearray.
Returns:
Type instance if matches. Otherwise None.
Raises:
TypeError: if obj is not a supported type.
"""
return match(obj, application_matchers)
15 changes: 14 additions & 1 deletion filetype/types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@

from . import archive
from . import audio
from . import application
from . import font
from . import image
from . import video
from .base import Type # noqa

# Supported image types
IMAGE = (
image.Dwg(),
image.Xcf(),
image.Jpeg(),
image.Jpx(),
image.Png(),
Expand All @@ -28,6 +31,7 @@

# Supported video types
VIDEO = (
video.M3gp(),
video.Mp4(),
video.M4v(),
video.Mkv(),
Expand All @@ -41,6 +45,7 @@

# Supported audio types
AUDIO = (
audio.Aac(),
audio.Midi(),
audio.Mp3(),
audio.M4a(),
Expand All @@ -55,6 +60,8 @@

# Supported archive container types
ARCHIVE = (
archive.Rpm(),
archive.Dcm(),
archive.Epub(),
archive.Zip(),
archive.Tar(),
Expand All @@ -79,5 +86,11 @@
archive.Lz(),
)

# Supported archive container types
APPLICATION = (
application.Wasm(),
)


# Expose supported type matchers
TYPES = list(VIDEO + IMAGE + AUDIO + FONT + ARCHIVE)
TYPES = list(VIDEO + IMAGE + AUDIO + FONT + ARCHIVE + APPLICATION)
22 changes: 22 additions & 0 deletions filetype/types/application.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-

from __future__ import absolute_import

from .base import Type


class Wasm(Type):
"""Implements the Wasm image type matcher."""

MIME = 'application/wasm'
EXTENSION = 'wasm'

def __init__(self):
super(Wasm, self).__init__(
mime=Wasm.MIME,
extension=Wasm.EXTENSION
)

def match(self, buf):
return buf[:8] == bytearray([0x00, 0x61, 0x73, 0x6d,
0x01, 0x00, 0x00, 0x00])
32 changes: 32 additions & 0 deletions filetype/types/archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,3 +513,35 @@ def match(self, buf):
buf[1] == 0x5A and
buf[2] == 0x49 and
buf[3] == 0x50)


class Dcm(Type):
"""Implements the Dcm image type matcher."""

MIME = 'application/dicom'
EXTENSION = 'dcm'

def __init__(self):
super(Dcm, self).__init__(
mime=Dcm.MIME,
extension=Dcm.EXTENSION
)

def match(self, buf):
return buf[128:131] == bytearray([0x44, 0x49, 0x43, 0x4d])


class Rpm(Type):
"""Implements the Rpm image type matcher."""

MIME = 'application/x-rpm'
EXTENSION = 'rpm'

def __init__(self):
super(Rpm, self).__init__(
mime=Rpm.MIME,
extension=Rpm.EXTENSION
)

def match(self, buf):
return buf[:4] == bytearray([0xed, 0xab, 0xee, 0xdb])
17 changes: 17 additions & 0 deletions filetype/types/audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,20 @@ def match(self, buf):
buf[3] == 0x4D and
buf[4] == 0x52 and
buf[5] == 0x0A)


class Aac(Type):
"""Implements the Aac image type matcher."""

MIME = 'audio/aac'
EXTENSION = 'aac'

def __init__(self):
super(Aac, self).__init__(
mime=Aac.MIME,
extension=Aac.EXTENSION
)

def match(self, buf):
return (buf[:2] == bytearray([0xff, 0xf1]) or
buf[:2] == bytearray([0xff, 0xf9]))
33 changes: 33 additions & 0 deletions filetype/types/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,36 @@ def match(self, buf):
buf[Dcm.OFFSET + 1] == 0x49 and
buf[Dcm.OFFSET + 2] == 0x43 and
buf[Dcm.OFFSET + 3] == 0x4D)


class Dwg(Type):
"""Implements the Dwg image type matcher."""

MIME = 'image/vnd.dwg'
EXTENSION = 'dwg'

def __init__(self):
super(Dwg, self).__init__(
mime=Dwg.MIME,
extension=Dwg.EXTENSION
)

def match(self, buf):
return buf[:4] == bytearray([0x41, 0x43, 0x31, 0x30])


class Xcf(Type):
"""Implements the Xcf image type matcher."""

MIME = 'image/x-xcf'
EXTENSION = 'xcf'

def __init__(self):
super(Xcf, self).__init__(
mime=Xcf.MIME,
extension=Xcf.EXTENSION
)

def match(self, buf):
return buf[:10] == bytearray([0x67, 0x69, 0x6d, 0x70, 0x20,
0x78, 0x63, 0x66, 0x20, 0x76])
16 changes: 16 additions & 0 deletions filetype/types/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,19 @@ def match(self, buf):
buf[2] == 0x1 and
buf[3] >= 0xb0 and
buf[3] <= 0xbf)


class M3gp(Type):
"""Implements the 3gp image type matcher."""

MIME = 'video/3gpp'
EXTENSION = '3gp'

def __init__(self):
super(M3gp, self).__init__(
mime=M3gp.MIME,
extension=M3gp.EXTENSION
)

def match(self, buf):
return buf[:7] == bytearray([0x66, 0x74, 0x79, 0x70, 0x33, 0x67, 0x70])

0 comments on commit cff02bf

Please sign in to comment.