Raise a specific error if a filename component can't be resolved into anything.

This commit is contained in:
Christopher Allan Webber 2011-04-09 11:45:38 -05:00
parent a6b378ef4d
commit 770c12be8d
2 changed files with 20 additions and 1 deletions

View File

@ -18,6 +18,10 @@
from werkzeug.utils import secure_filename from werkzeug.utils import secure_filename
class Error(Exception): pass
class InvalidFilepath(Error): pass
def clean_listy_filepath(listy_filepath): def clean_listy_filepath(listy_filepath):
""" """
Take a listy filepath (like ['dir1', 'dir2', 'filename.jpg']) and Take a listy filepath (like ['dir1', 'dir2', 'filename.jpg']) and
@ -34,8 +38,14 @@ def clean_listy_filepath(listy_filepath):
Returns: Returns:
A cleaned list of unicode objects. A cleaned list of unicode objects.
""" """
return [ cleaned_filepath = [
unicode(secure_filename(filepath)) unicode(secure_filename(filepath))
for filepath in listy_filepath] for filepath in listy_filepath]
if u'' in cleaned_filepath:
raise InvalidFilepath(
"A filename component could not be resolved into a usable name.")
return cleaned_filepath

View File

@ -30,3 +30,12 @@ def test_clean_listy_filepath():
expected = [u'etc', u'passwd'] expected = [u'etc', u'passwd']
assert storage.clean_listy_filepath( assert storage.clean_listy_filepath(
['../../../etc/', 'passwd']) == expected ['../../../etc/', 'passwd']) == expected
try:
storage.clean_listy_filepath(
['../../', 'linooks.jpg'])
except storage.InvalidFilepath:
# Yes, this error should be raise
pass
else:
assert "success" == "failboat"