h2non / filetype.py

Small, dependency-free, fast Python package to infer binary file types checking the magic numbers signature
https://h2non.github.io/filetype.py
MIT License
663 stars 113 forks source link

Font mimetypes are outdated #164

Open bjd183 opened 1 year ago

bjd183 commented 1 year ago

RFC8081 (2017) deprecated e.g. application/font-sfnt in favor of font/sfnt.

From the RFC: Deprecated Alias: The existing registration "application/font-sfnt" is deprecated in favor of "font/sfnt".

Some browsers (i.e. Chrome) issue console warnings and refuse to apply the referenced stylesheet <link rel="stylesheet" href="my.url">

Refused to apply style from 'my.url' because its MIME type ('application/font-sfnt') is not a supported stylesheet MIME type, and strict MIME checking is enabled.

bjd183 commented 1 year ago

Font mimetypes should be updated to reflect this change. Current workaround is to subclass font types, use add_type, and overwrite filetype.types.FONT with a new tuple to ensure correct behavior of is_font().

bjd183 commented 1 year ago

It is not actually possible to subclass because #165

bjd183 commented 1 year ago

Workaround for the workaround:

import re

for instance in filetype.font_matchers:
    match = re.match(r'^application/font-(\w+)$', instance.mime)
    suffix = match.group(1)
    instance.__class__.mime = property(lambda self: f'font/{suffix}')
bjd183 commented 1 year ago

Another option is to use sys.modules['filetype.types']