Fix #4688: Error to download remote images having long URL

This commit is contained in:
Takeshi KOMIYA 2018-03-17 13:43:42 +09:00
parent 27abce6306
commit 0eb5e0865a
2 changed files with 9 additions and 2 deletions

View File

@ -27,6 +27,7 @@ Bugs fixed
* #4725: Sphinx does not work with python 3.5.0 and 3.5.1
* #4716: Generation PDF file with TexLive on Windows, file not found error
* #4574: vertical space before equation in latex
* #4688: Error to download remote images having long URL
Testing
--------

View File

@ -30,6 +30,8 @@ if False:
logger = logging.getLogger(__name__)
MAX_FILENAME_LEN = 32
class BaseImageConverter(SphinxTransform):
def apply(self):
@ -70,10 +72,14 @@ class ImageDownloader(BaseImageConverter):
basename = os.path.basename(node['uri'])
if '?' in basename:
basename = basename.split('?')[0]
if basename == '':
basename = sha1(node['uri'].encode("utf-8")).hexdigest()
if basename == '' or len(basename) > MAX_FILENAME_LEN:
filename, ext = os.path.splitext(node['uri'])
basename = sha1(filename.encode("utf-8")).hexdigest() + ext
dirname = node['uri'].replace('://', '/').translate({ord("?"): u"/",
ord("&"): u"/"})
if len(dirname) > MAX_FILENAME_LEN:
dirname = sha1(dirname.encode('utf-8')).hexdigest()
ensuredir(os.path.join(self.imagedir, dirname))
path = os.path.join(self.imagedir, dirname, basename)