algoo / preview-generator

generates previews of files with cache management
https://pypi.org/project/preview-generator/
MIT License
235 stars 51 forks source link

Use specific mimetype datastore and allow loader to use them to clarify supported type #147

Open inkhey opened 4 years ago

inkhey commented 4 years ago

To handle more properly mimetypes, we should allow to add new mimetype/file_extension mapping in any builder. Those mapping will simplify mimetype determination in manager when file_extension is provided outside of file_path. (file_ext parameter of many manager method). Those mapping will allow us allow to get easily file_extension from mimetype in builder context (where we have only mimetype and file_path), this may be useful for software that work with file_extension instead of mimetype, like imagemagick.

Original comment:

Note: it's possible to add manually new type to default mimetype list, using a specific mimetype datastore, this allow us to easily guess_extension and/or mimetype from both extension/mimetype:

>>>mimetypes_data = mimetypes.MimeTypes()
>>>mimetypes_data.add_type('image/x-sony-arw', 'arw')
>>>mimetypes_data.guess_extension('image/x-sony-arw')
'arw'

This may be useful in builder, if we need to obtain extension related to a file with mimetype known but without file_extension in path (currently we do not have "file_extension" parameter in builder, only file_path which need to exist). this can allow to have specific behavior for builder who really need explicit mimetype. For example with imagemagick builder, we can do, if we get 'arw from mimetype 'image/x-sony-arw' :

convert arw:DSC08523 -layers merge test.jpg

Originally posted by @inkhey in https://github.com/algoo/preview-generator/issues/142#issuecomment-553449301