Support more mime types for add_extension_based_on_mime (#2562)

* Support more mime types for add_extension_based_on_mime

* Add myself to CONTRIBUTORS
This commit is contained in:
BenYip 2023-06-29 13:22:04 +08:00 committed by GitHub
parent df3fa5a0be
commit 14bc02b431
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 1 deletions

View file

@ -125,6 +125,7 @@ Austin Siew <github.com/Aquafina-water-bottle>
Joel Koen <mail@joelkoen.com> Joel Koen <mail@joelkoen.com>
Christopher Woggon <christopher.woggon@gmail.com> Christopher Woggon <christopher.woggon@gmail.com>
Kavel Rao <github.com/kavelrao> Kavel Rao <github.com/kavelrao>
Ben Yip <github.com/bennyyip>
******************** ********************

View file

@ -101,14 +101,21 @@ class MediaManager(DeprecatedNamesMixin):
return self.col._backend.add_media_file(desired_name=desired_fname, data=data) return self.col._backend.add_media_file(desired_name=desired_fname, data=data)
def add_extension_based_on_mime(self, fname: str, content_type: str) -> str: def add_extension_based_on_mime(self, fname: str, content_type: str) -> str:
"If jpg or png mime, add .png/.jpg if missing extension." "Add extension based on mime for common audio and image format if missing extension."
if not os.path.splitext(fname)[1]: if not os.path.splitext(fname)[1]:
# mimetypes is returning '.jpe' even after calling .init(), so we'll do # mimetypes is returning '.jpe' even after calling .init(), so we'll do
# it manually instead # it manually instead
type_map = { type_map = {
"audio/mpeg": ".mp3",
"audio/ogg": ".oga",
"audio/opus": ".opus",
"audio/wav": ".wav",
"audio/webm": ".weba",
"audio/aac": ".aac",
"image/jpeg": ".jpg", "image/jpeg": ".jpg",
"image/png": ".png", "image/png": ".png",
"image/svg+xml": ".svg", "image/svg+xml": ".svg",
"image/webp": ".webp",
} }
if content_type in type_map: if content_type in type_map:
fname += type_map[content_type] fname += type_map[content_type]