Merge pull request #4747 from tk0miya/4688_remote_image_having_long_URL

4688 remote image having long url
This commit is contained in:
Takeshi KOMIYA 2018-03-20 01:40:48 +09:00 committed by GitHub
commit c320c29c60
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 9 deletions

View File

@ -30,6 +30,7 @@ Bugs fixed
* #4720: message when an image is mismatched for builder is not clear * #4720: message when an image is mismatched for builder is not clear
* #4655, #4684: Incomplete localization strings in Polish and Chinese * #4655, #4684: Incomplete localization strings in Polish and Chinese
* #2286: Sphinx crashes when error is happens in rendering HTML pages * #2286: Sphinx crashes when error is happens in rendering HTML pages
* #4688: Error to download remote images having long URL
Testing Testing
-------- --------

View File

@ -30,6 +30,8 @@ if False:
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
MAX_FILENAME_LEN = 32
class BaseImageConverter(SphinxTransform): class BaseImageConverter(SphinxTransform):
def apply(self): def apply(self):
@ -66,16 +68,21 @@ class ImageDownloader(BaseImageConverter):
def handle(self, node): def handle(self, node):
# type: (nodes.Node) -> None # type: (nodes.Node) -> None
basename = os.path.basename(node['uri'])
if '?' in basename:
basename = basename.split('?')[0]
if basename == '':
basename = sha1(node['uri'].encode("utf-8")).hexdigest()
dirname = node['uri'].replace('://', '/').translate({ord("?"): u"/",
ord("&"): u"/"})
ensuredir(os.path.join(self.imagedir, dirname))
path = os.path.join(self.imagedir, dirname, basename)
try: try:
basename = os.path.basename(node['uri'])
if '?' in basename:
basename = basename.split('?')[0]
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)
headers = {} headers = {}
if os.path.exists(path): if os.path.exists(path):
timestamp = ceil(os.stat(path).st_mtime) timestamp = ceil(os.stat(path).st_mtime)