use own mapping before relying on the 'mimetypes' module

This commit is contained in:
Mike Fährmann 2017-11-30 22:30:01 +01:00
parent 214972bc9a
commit 3dc1169736
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88

View File

@ -61,10 +61,39 @@ class Downloader(DownloaderBase):
def get_extension(self):
mtype = self.response.headers.get("Content-Type", "image/jpeg")
mtype = mtype.partition(";")[0]
if mtype in MIMETYPE_MAP:
return MIMETYPE_MAP[mtype]
exts = mimetypes.guess_all_extensions(mtype, strict=False)
if exts:
exts.sort()
return exts[-1][1:]
self.log.warning(
"No filename extension found for MIME type '%s'", mtype)
return "txt"
MIMETYPE_MAP = {
"image/jpeg": "jpg",
"image/jpg": "jpg",
"image/png": "png",
"image/gif": "gif",
"image/bmp": "bmp",
"image/webp": "webp",
"image/svg+xml": "svg",
"video/webm": "webm",
"video/ogg": "ogg",
"video/mp4": "mp4",
"audio/wav": "wav",
"audio/x-wav": "wav",
"audio/webm": "webm",
"audio/ogg": "ogg",
"audio/mpeg": "mp3",
"application/ogg": "ogg",
"application/octet-stream": "bin",
}