Skip to content

Commit cd18bc8

Browse files
authored
Change puremagic to python-magic (#17)
1 parent d46bf2e commit cd18bc8

File tree

2 files changed

+4
-12
lines changed

2 files changed

+4
-12
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ dependencies = [
2222
"pydantic",
2323
"pyvips",
2424
"pyvips-binary",
25-
"puremagic",
25+
"magika",
2626
]
2727

2828
[dependency-groups]

safe_s3_storage/file_validator.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
import enum
33
import typing
44

5-
import puremagic
65
import pyvips # type: ignore[import-untyped]
6+
from magika import Magika
77

88
from safe_s3_storage import exceptions
99
from safe_s3_storage.kaspersky_scan_engine import KasperskyScanEngineClient
@@ -48,16 +48,8 @@ class FileValidator:
4848
image_quality: int = 85
4949

5050
def _validate_mime_type(self, *, file_name: str, file_content: bytes) -> str:
51-
try:
52-
mime_type = puremagic.from_string(file_content, mime=True)
53-
except puremagic.PureError:
54-
# unlike python-magic, puremagic doesn't recognize if text is binary
55-
try:
56-
file_content.decode()
57-
except UnicodeDecodeError:
58-
mime_type = "application/octet-stream"
59-
else:
60-
mime_type = "text/plain"
51+
mime_type_prediction: typing.Final = Magika().identify_bytes(file_content)
52+
mime_type: typing.Final = mime_type_prediction.prediction.dl.mime_type
6153
if self.allowed_mime_types is None or mime_type in self.allowed_mime_types:
6254
return mime_type
6355

0 commit comments

Comments
 (0)