Skip to content

Commit

Permalink
[#2057] Fix filename munging
Browse files Browse the repository at this point in the history
  • Loading branch information
tobes committed Dec 17, 2014
1 parent f6f18b1 commit 9caf037
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
32 changes: 32 additions & 0 deletions ckan/lib/munge.py
Expand Up @@ -4,6 +4,7 @@
# improved.

import re
import os.path

from ckan import model

Expand Down Expand Up @@ -112,6 +113,37 @@ def munge_filename(filename):
filename = _munge_to_length(filename, 3, 100)
return filename


def munge_filename_ext_safe(filename):
''' Munge the filename but keep the extension
this allows data that has not had it's format set
but relies on the extention for its type eg .csv
We also remove the path if one is given by a browser etc'''

# just get the filename ignore the path
path, filename = os.path.split(filename)
# clean up
filename = substitute_ascii_equivalents(filename)
filename = filename.lower().strip()
filename = re.sub(r'[^a-zA-Z0-9. ]', '', filename).replace(' ', '-')
# resize if needed but keep extension
name, ext = os.path.splitext(filename)
# limit overly long extensions
if len(ext) > 21:
ext = ext[:21]
# reduce filename as needed
maxsize = 100 - len(ext)
if len(name) > maxsize:
name = name[:maxsize]

filename = name + ext
# enlarge name to at least 3 chars
if len(filename) < 3:
filename += '_' * (3 - len(filename))

return filename


def _munge_to_length(string, min_length, max_length):
'''Pad/truncates a string'''
if len(string) < min_length:
Expand Down
2 changes: 1 addition & 1 deletion ckan/lib/uploader.py
Expand Up @@ -170,7 +170,7 @@ def __init__(self, resource):

if isinstance(upload_field_storage, cgi.FieldStorage):
self.filename = upload_field_storage.filename
self.filename = munge.munge_filename(self.filename)
self.filename = munge.munge_filename_ext_safe(self.filename)
resource['url'] = self.filename
resource['url_type'] = 'upload'
self.upload_file = upload_field_storage.file
Expand Down

0 comments on commit 9caf037

Please sign in to comment.