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.
This commit is contained in:
parent
6cf355d7ce
commit
0d0354f4da
4 changed files with 21 additions and 17 deletions
|
@ -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)
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue