more cleanup for photos provider

This commit is contained in:
Dima Gerasimov 2020-03-13 00:10:27 +00:00
parent c37bc6e60e
commit 4ed70eee90
3 changed files with 45 additions and 33 deletions

View file

@ -126,3 +126,22 @@ def mcachew(*args, **kwargs):
import cachew.experimental
cachew.experimental.enable_exceptions() # TODO do it only once?
return cachew.cachew(*args, **kwargs)
@functools.lru_cache(1)
def _magic():
import magic # type: ignore
return magic.Magic(mime=True)
# TODO could reuse in pdf module?
import mimetypes # TODO do I need init()?
def fastermime(path: str) -> str:
# mimetypes is faster
(mime, _) = mimetypes.guess_type(path)
if mime is not None:
return mime
# magic is slower but returns more stuff
# TODO FIXME Result type; it's inherently racey
return _magic().from_file(path)