Drop extension overriding

Just check that the extension is valid.
This commit is contained in:
Midgard 2023-05-02 18:30:28 +02:00
parent 96141fc6ea
commit 9784205759
Signed by: midgard
GPG key ID: 511C112F1331BBB4
3 changed files with 11 additions and 18 deletions

View file

@ -3,16 +3,6 @@
from wolkje_files import file_name
def test_set_extension():
assert file_name.set_extension("", ".jpg") == ".jpg"
assert file_name.set_extension("foo", ".jpg") == "foo.jpg"
assert file_name.set_extension("foo.", ".jpg") == "foo.jpg"
assert file_name.set_extension("foo.bar", ".jpg") == "foo.jpg"
assert file_name.set_extension("foo.tar.gz", ".jpg") == "foo.jpg"
assert file_name.set_extension("foo.tafel", ".jpg") == "foo.jpg"
assert file_name.set_extension("foo.tafels", ".jpg") == "foo.tafels.jpg"
def test_sanitize():
assert file_name.sanitize("foo.jpg") == "foo.jpg"
assert file_name.sanitize("foo/bar.jpg") == "foo-bar.jpg"

View file

@ -65,9 +65,13 @@ class FolderResource:
if part.filename is None:
pass
try:
img_store.save_file(folder_id, part.filename, part.stream, part.content_type)
img_store.save_file(folder_id, part.filename, part.stream)
except img_store.NoSuchFolderError:
raise falcon.HTTPNotFound(description="Folder does not exist")
except img_store.UnknownExtensionError:
raise falcon.HTTPNotFound(description="Unknown extension in file name")
except img_store.IllegalFileNameError:
raise falcon.HTTPNotFound(description="Invalid file name")
folder = img_store.get_folder(folder_id)
redirect_to_folder(folder)

View file

@ -10,7 +10,7 @@ from slugify import slugify
from wolkje_files import file_name as fn
class UnknownMimeTypeError(Exception):
class UnknownExtensionError(Exception):
pass
class NoSuchFolderError(Exception):
pass
@ -108,18 +108,17 @@ def ls(folder_id):
_CHUNK_SIZE_BYTES = 4096
def save_file(folder_id, desired_file_name, file_stream, mime_type):
def save_file(folder_id, desired_file_name, file_stream):
folder = get_folder(folder_id)
if not folder:
raise NoSuchFolderError()
ext = mimetypes.guess_extension(mime_type)
if ext is None:
raise UnknownMimeTypeError()
file_name = fn.sanitize(fn.set_extension(desired_file_name, ext))
file_name = fn.sanitize(desired_file_name)
if file_name is None:
raise IllegalFileNameError()
mime_type = mimetypes.guess_type(file_name)
if not mime_type:
raise UnknownExtensionError()
file_path = os.path.join(_get_folder_path(folder_id), file_name)
with open(file_path, "wb") as fp: