mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Merge pull request #4747 from tk0miya/4688_remote_image_having_long_URL
4688 remote image having long url
This commit is contained in:
commit
c320c29c60
1
CHANGES
1
CHANGES
@ -30,6 +30,7 @@ Bugs fixed
|
||||
* #4720: message when an image is mismatched for builder is not clear
|
||||
* #4655, #4684: Incomplete localization strings in Polish and Chinese
|
||||
* #2286: Sphinx crashes when error is happens in rendering HTML pages
|
||||
* #4688: Error to download remote images having long URL
|
||||
|
||||
Testing
|
||||
--------
|
||||
|
@ -30,6 +30,8 @@ if False:
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
MAX_FILENAME_LEN = 32
|
||||
|
||||
|
||||
class BaseImageConverter(SphinxTransform):
|
||||
def apply(self):
|
||||
@ -66,16 +68,21 @@ class ImageDownloader(BaseImageConverter):
|
||||
|
||||
def handle(self, node):
|
||||
# type: (nodes.Node) -> None
|
||||
try:
|
||||
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)
|
||||
try:
|
||||
|
||||
headers = {}
|
||||
if os.path.exists(path):
|
||||
timestamp = ceil(os.stat(path).st_mtime)
|
||||
|
Loading…
Reference in New Issue
Block a user