[preview] Fix problem with too long filenames

Fixes #365
This commit is contained in:
Philipp Hörist
2018-12-08 20:26:12 +01:00
parent 9bb2bc5c0d
commit 214fb16286

View File

@@ -21,6 +21,7 @@ import binascii
import logging import logging
import math import math
from urllib.parse import urlparse from urllib.parse import urlparse
from urllib.parse import unquote
from io import BytesIO from io import BytesIO
import shutil import shutil
from functools import partial from functools import partial
@@ -174,7 +175,7 @@ class Base(object):
log.debug('Url with text will not be displayed: %s', real_text) log.debug('Url with text will not be displayed: %s', real_text)
return return
urlparts = urlparse(real_text) urlparts = urlparse(unquote(real_text))
if not self._accept_uri(urlparts, real_text, additional_data): if not self._accept_uri(urlparts, real_text, additional_data):
return return
@@ -245,6 +246,12 @@ class Base(object):
filename = os.path.basename(urlparts.path) filename = os.path.basename(urlparts.path)
ext = os.path.splitext(filename)[1] ext = os.path.splitext(filename)[1]
name = os.path.splitext(filename)[0] name = os.path.splitext(filename)[0]
if len(name) > 90:
# Many Filesystems have a limit on filename length
# Most have 255, some encrypted ones only 143
# We add around 50 chars for the hash,
# so the filename should not exceed 90
name = name[:90]
namehash = hashlib.sha1(real_text.encode('utf-8')).hexdigest() namehash = hashlib.sha1(real_text.encode('utf-8')).hexdigest()
newfilename = name + '_' + namehash + ext newfilename = name + '_' + namehash + ext
thumbfilename = name + '_' + namehash + '_thumb_' \ thumbfilename = name + '_' + namehash + '_thumb_' \
@@ -447,7 +454,7 @@ class Base(object):
log.error('Could not download image for URL: %s', url) log.error('Could not download image for URL: %s', url)
return return
urlparts = urlparse(url) urlparts = urlparse(unquote(url))
filename = os.path.basename(urlparts.path) filename = os.path.basename(urlparts.path)
if os.path.basename(filepath).startswith('location_'): if os.path.basename(filepath).startswith('location_'):
filename = os.path.basename(filepath) filename = os.path.basename(filepath)