Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escape forward slashes in downloaded image filenames #249

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 21 additions & 16 deletions dumpgenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,16 @@ def getVersion():
return(__VERSION__)


def truncateFilename(other={}, filename=''):
""" Truncate filenames when downloading images with large filenames """
return filename[:other['filenamelimit']] + \
md5(filename.encode('utf-8')).hexdigest() + '.' + filename.split('.')[-1]
def sanitizeFilename(other={}, filename=''):
""" Strip forward slashes and truncate overlong filenames. """
""" Also insert a hash when the filename was modified to try to """
""" avoid name collisions. """
if '/' in filename or len(filename) > other['filenamelimit']:
filename = filename.replace('/', '%2F')
hash = md5(filename.encode('utf-8')).hexdigest()
extension = '.' + filename.split('.')[-1]
filename = filename[:other['filenamelimit']] + hash + extension
return filename


def delay(config={}, session=None):
Expand Down Expand Up @@ -1060,13 +1066,10 @@ def generateImageDump(config={}, other={}, images=[], start='', session=None):
delay(config=config, session=session)

# saving file
# truncate filename if length > 100 (100 + 32 (md5) = 132 < 143 (crash
# limit). Later .desc is added to filename, so better 100 as max)
filename2 = urllib.unquote(filename)
if len(filename2) > other['filenamelimit']:
# split last . (extension) and then merge
filename2 = truncateFilename(other=other, filename=filename2)
print 'Filename is too long, truncating. Now it is:', filename2
# quote forward slashes and truncate filename if length > 100
# (100 + 32 (md5) = 132 < 143 (crash limit). Later .desc is
# added to filename, so better 100 as max)
filename2 = sanitizeFilename(other=other, filename=urllib.unquote(filename))
filename3 = u'%s/%s' % (imagepath, filename2)
imagefile = open(filename3, 'wb')
r = requests.get(url=url)
Expand Down Expand Up @@ -1711,8 +1714,12 @@ def resumePreviousDump(config={}, other={}):
# checking images directory
listdir = []
try:
listdir = [n.decode('utf-8') for n in os.listdir('%s/images' % (config['path']))]
except:
files = os.listdir('%s/images' % (config['path']))
try:
listdir = [n.decode('utf-8') for n in files]
except:
listdir = files
except OSError:
pass # probably directory does not exist
listdir.sort()
complete = True
Expand All @@ -1723,9 +1730,7 @@ def resumePreviousDump(config={}, other={}):
lastfilename2 = lastfilename
# return always the complete filename, not the truncated
lastfilename = filename
filename2 = filename
if len(filename2) > other['filenamelimit']:
filename2 = truncateFilename(other=other, filename=filename2)
filename2 = sanitizeFilename(other=other, filename=filename)
if filename2 not in listdir:
complete = False
break
Expand Down