From 0d0354f4dadd89b66b702b48092380e75838b81a Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Thu, 7 Dec 2017 21:15:10 -0800 Subject: [PATCH] Rename normalize_filepath to remove_path_badchars. Because the function does not do any resolving or converting to an absolute path, I feel the name 'normalize' is inaccurate and could be misleading when read. This is more clear. --- etiquette/helpers.py | 28 +++++++++++-------- etiquette/objects.py | 4 +-- etiquette/photodb.py | 2 +- .../etiquette_flask/etiquette_flask.py | 4 +-- 4 files changed, 21 insertions(+), 17 deletions(-) diff --git a/etiquette/helpers.py b/etiquette/helpers.py index ff4a04a..abc1b97 100644 --- a/etiquette/helpers.py +++ b/etiquette/helpers.py @@ -17,7 +17,7 @@ def album_zip_directories(album, recursive=True): ''' directories = {} if album.title: - root_folder = 'album %s - %s' % (album.id, normalize_filepath(album.title)) + root_folder = 'album %s - %s' % (album.id, remove_path_badchars(album.title)) else: root_folder = 'album %s' % album.id @@ -229,17 +229,6 @@ def is_xor(*args): ''' return [bool(a) for a in args].count(True) == 1 -def normalize_filepath(filepath, allowed=''): - ''' - Remove some bad characters. - ''' - badchars = remove_characters(constants.FILENAME_BADCHARS, allowed) - filepath = remove_characters(filepath, badchars) - - filepath = filepath.replace('/', os.sep) - filepath = filepath.replace('\\', os.sep) - return filepath - def now(timestamp=True): ''' Return the current UTC timestamp or datetime object. @@ -307,6 +296,21 @@ def recursive_dict_keys(d): keys.update(subkeys) return keys +def remove_path_badchars(filepath, allowed=''): + ''' + Remove the bad characters seen in constants.FILENAME_BADCHARS, except + those which you explicitly permit. + + 'file*name' -> 'filename' + ('D:\\file*name', allowed=':\\') -> 'D:\\filename' + ''' + badchars = remove_characters(constants.FILENAME_BADCHARS, allowed) + filepath = remove_characters(filepath, badchars) + + filepath = filepath.replace('/', os.sep) + filepath = filepath.replace('\\', os.sep) + return filepath + def remove_characters(text, characters): translator = {ord(c): None for c in characters} text = text.translate(translator) diff --git a/etiquette/objects.py b/etiquette/objects.py index ece5728..1f28c1a 100644 --- a/etiquette/objects.py +++ b/etiquette/objects.py @@ -535,7 +535,7 @@ class Photo(ObjectBase): if isinstance(db_row, (list, tuple)): db_row = dict(zip(constants.SQL_PHOTO_COLUMNS, db_row)) - self.real_filepath = helpers.normalize_filepath(db_row['filepath'], allowed=':\\/') + self.real_filepath = helpers.remove_path_badchars(db_row['filepath'], allowed=':\\/') self.real_path = pathclass.Path(self.real_filepath) self.id = db_row['id'] @@ -941,7 +941,7 @@ class Photo(ObjectBase): old_path = self.real_path old_path.correct_case() - new_filename = helpers.normalize_filepath(new_filename, allowed=':\\/') + new_filename = helpers.remove_path_badchars(new_filename, allowed=':\\/') if os.path.dirname(new_filename) == '': new_path = old_path.parent.with_child(new_filename) else: diff --git a/etiquette/photodb.py b/etiquette/photodb.py index dfde491..d6df5b4 100644 --- a/etiquette/photodb.py +++ b/etiquette/photodb.py @@ -1125,7 +1125,7 @@ class PhotoDB(PDBAlbumMixin, PDBBookmarkMixin, PDBPhotoMixin, PDBTagMixin, PDBUs raise exceptions.NotExclusive(['data_directory', 'ephemeral']) # DATA DIR PREP - data_directory = helpers.normalize_filepath(data_directory, allowed=':/\\') + data_directory = helpers.remove_path_badchars(data_directory, allowed=':/\\') self.data_directory = pathclass.Path(data_directory) os.makedirs(self.data_directory.absolute_path, exist_ok=True) diff --git a/frontends/etiquette_flask/etiquette_flask/etiquette_flask.py b/frontends/etiquette_flask/etiquette_flask/etiquette_flask.py index 3a0c7a5..304ab7c 100644 --- a/frontends/etiquette_flask/etiquette_flask/etiquette_flask.py +++ b/frontends/etiquette_flask/etiquette_flask/etiquette_flask.py @@ -454,7 +454,7 @@ def get_album_zip(album_id): else: download_as = 'album %s.zip' % album.id - download_as = etiquette.helpers.normalize_filepath(download_as) + download_as = etiquette.helpers.remove_path_badchars(download_as) download_as = urllib.parse.quote(download_as) outgoing_headers = { 'Content-Type': 'application/octet-stream', @@ -611,7 +611,7 @@ def get_file(photo_id): else: download_as = photo.id + photo.dot_extension - download_as = etiquette.helpers.normalize_filepath(download_as) + download_as = etiquette.helpers.remove_path_badchars(download_as) download_as = urllib.parse.quote(download_as) response = flask.make_response(send_file(photo.real_filepath)) response.headers['Content-Disposition'] = 'attachment; filename*=UTF-8\'\'%s' % download_as