mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Guess mimetype on downloading
This commit is contained in:
parent
5f071bb474
commit
027872f569
@ -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:
|
||||||
|
@ -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
|
||||||
|
Loading…
Reference in New Issue
Block a user