Guess mimetype on downloading

This commit is contained in:
Takeshi KOMIYA 2017-04-17 01:18:49 +09:00
parent 5f071bb474
commit 027872f569
2 changed files with 6 additions and 4 deletions

View File

@ -16,6 +16,7 @@ from docutils import nodes
from sphinx.transforms import SphinxTransform from sphinx.transforms import SphinxTransform
from sphinx.util import logging, requests from sphinx.util import logging, requests
from sphinx.util.images import guess_mimetype
from sphinx.util.osutil import ensuredir from sphinx.util.osutil import ensuredir
if False: if False:
@ -74,8 +75,9 @@ class ImageDownloader(BaseImageConverter):
with open(path, 'wb') as f: with open(path, 'wb') as f:
f.write(r.content) f.write(r.content)
mimetype = guess_mimetype(path, default='*')
node['candidates'].pop('?') node['candidates'].pop('?')
node['candidates']['*'] = path node['candidates'][mimetype] = path
node['uri'] = path node['uri'] = path
self.app.env.images.add_file(self.env.docname, path) self.app.env.images.add_file(self.env.docname, path)
except Exception as exc: except Exception as exc:

View File

@ -52,8 +52,8 @@ def get_image_size(filename):
return None return None
def guess_mimetype(filename): def guess_mimetype(filename, default=None):
# type: (unicode) -> unicode # type: (unicode, unicode) -> unicode
_, ext = path.splitext(filename) _, ext = path.splitext(filename)
if ext in mime_suffixes: if ext in mime_suffixes:
return mime_suffixes[ext] return mime_suffixes[ext]
@ -63,4 +63,4 @@ def guess_mimetype(filename):
if imgtype: if imgtype:
return 'image/' + imgtype return 'image/' + imgtype
return None return default