Fix #3860: Remote image URIs without filename break builders not supported remote images

This commit is contained in:
Takeshi KOMIYA 2017-06-25 18:49:13 +09:00
parent 8f8f0fb868
commit ea87cfba4f
2 changed files with 13 additions and 1 deletions

View File

@ -39,6 +39,8 @@ Bugs fixed
``sphinx.util.compat.Directive``
* #3874: Bogus warnings for "citation not referenced" for cross-file citations
* #3860: Don't download images when builders not supported images
* #3860: Remote image URIs without filename break builders not supported remote
images
Testing
--------

View File

@ -20,7 +20,7 @@ from sphinx.transforms import SphinxTransform
from sphinx.util import logging, requests
from sphinx.util import epoch_to_rfc1123, rfc1123_to_epoch
from sphinx.util.images import guess_mimetype, get_image_extension, parse_data_uri
from sphinx.util.osutil import ensuredir
from sphinx.util.osutil import ensuredir, movefile
if False:
# For type annotation
@ -69,6 +69,8 @@ class ImageDownloader(BaseImageConverter):
basename = os.path.basename(node['uri'])
if '?' in basename:
basename = basename.split('?')[0]
if basename == '':
basename = sha1(node['uri']).hexdigest()
dirname = node['uri'].replace('://', '/').translate({ord("?"): u"/",
ord("&"): u"/"})
ensuredir(os.path.join(self.imagedir, dirname))
@ -96,6 +98,14 @@ class ImageDownloader(BaseImageConverter):
os.utime(path, (timestamp, timestamp))
mimetype = guess_mimetype(path, default='*')
if mimetype != '*' and os.path.splitext(basename)[1] == '':
# append a suffix if URI does not contain suffix
ext = get_image_extension(mimetype)
newpath = os.path.join(self.imagedir, dirname, basename + ext)
movefile(path, newpath)
self.app.env.original_image_uri.pop(path)
self.app.env.original_image_uri[newpath] = node['uri']
path = newpath
node['candidates'].pop('?')
node['candidates'][mimetype] = path
node['uri'] = path