Raise a specific error if a filename component can't be resolved into anything.
authorChristopher Allan Webber <cwebber@dustycloud.org>
Sat, 9 Apr 2011 16:45:38 +0000 (11:45 -0500)
committerChristopher Allan Webber <cwebber@dustycloud.org>
Sat, 9 Apr 2011 16:45:38 +0000 (11:45 -0500)
mediagoblin/storage.py
mediagoblin/tests/test_storage.py

index c06cb3a8d136e74034b607295f5e1fdd2f393370..805be84cae81204b1bffea632915d5870c9891ed 100644 (file)
 from werkzeug.utils import secure_filename
 
 
+class Error(Exception): pass
+class InvalidFilepath(Error): pass
+
+
 def clean_listy_filepath(listy_filepath):
     """
     Take a listy filepath (like ['dir1', 'dir2', 'filename.jpg']) and
@@ -34,8 +38,14 @@ def clean_listy_filepath(listy_filepath):
     Returns:
       A cleaned list of unicode objects.
     """
-    return [
+    cleaned_filepath = [
         unicode(secure_filename(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
+
 
index b7da467c8c99769bdcbda03c16e509993a236100..cdcddf098130f7f91e9cf08ad3f535a175b8ace2 100644 (file)
@@ -30,3 +30,12 @@ def test_clean_listy_filepath():
     expected = [u'etc', u'passwd']
     assert storage.clean_listy_filepath(
         ['../../../etc/', 'passwd']) == expected
+
+    try:
+        storage.clean_listy_filepath(
+            ['../../', 'linooks.jpg'])
+    except storage.InvalidFilepath:
+        # Yes, this error should be raise
+        pass
+    else:
+        assert "success" == "failboat"