Skip to content

Commit 784065e

Browse files
committed
Use magic to determin type.
1 parent 0de7cf6 commit 784065e

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ dependencies = [
1818
"isoduration==20.11.0",
1919
"lxml==5.2.2",
2020
"json-stream==2.3.2",
21+
"python-magic==0.4.27",
2122
]
2223
requires-python = ">=3.10"
2324
readme = "README.txt"

src/npoapi/media_backend.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
from typing import Dict, Optional, Union
88
from xml.dom import minidom
99

10+
import magic
11+
12+
1013
import lxml
1114
from typing_extensions import override
1215
from xsdata.models.datatype import XmlDateTime
@@ -429,12 +432,18 @@ def upload(self, mid: str, file: str, content_type: str = None, **kwargs):
429432
priority = kwargs.get("priority", None)
430433
transcode = kwargs.get("transcode", True)
431434
if content_type is None:
432-
if file.endswith(".mp3"):
433-
content_type = "audio/mp3"
434-
elif file.endswith(".mp4"):
435-
content_type = "video/mp4"
436-
else:
437-
return "not supported " + file
435+
try:
436+
f = magic.Magic(mime=True, uncompress=True)
437+
content_type = f.from_file(file)
438+
except Exception as e:
439+
self.logger.error("Error guessing content type for %s: %s" % (file, e))
440+
if file.endswith(".mp3"):
441+
content_type = "audio/mp3"
442+
elif file.endswith(".mp4"):
443+
content_type = "video/mp4"
444+
else:
445+
return "not supported " + file
446+
self.logger.info("Content type not given, guessing %s for %s" % (content_type, file))
438447
if content_type.startswith("video/"):
439448
if transcode:
440449
post_fix = "/%s/%s" % (

0 commit comments

Comments
 (0)