core.common: move mime-related stuff to my.core.mime

no backward compat, unlikely it was used by anyone else
This commit is contained in:
Dima Gerasimov 2024-08-15 18:44:12 +03:00 committed by karlicoss
parent c45c51af22
commit 88f3c17c27
3 changed files with 36 additions and 23 deletions

View file

@ -2,7 +2,6 @@ from glob import glob as do_glob
from pathlib import Path
from datetime import datetime
from dataclasses import is_dataclass, asdict as dataclasses_asdict
import functools
import os
from typing import (
Any,
@ -117,27 +116,6 @@ def get_files(
return tuple(paths)
@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()?
# todo wtf? fastermime thinks it's mime is application/json even if the extension is xz??
# whereas magic detects correctly: application/x-zstd and application/x-xz
def fastermime(path: PathIsh) -> str:
paths = str(path)
# mimetypes is faster
(mime, _) = mimetypes.guess_type(paths)
if mime is not None:
return mime
# magic is slower but returns more stuff
# TODO Result type?; it's kinda racey, but perhaps better to let the caller decide?
return _magic().from_file(paths)
Json = Dict[str, Any]