From 2b75c8f0362797ccd2964f4137814279e29cf056 Mon Sep 17 00:00:00 2001 From: axolotle Date: Fri, 17 Nov 2023 17:28:25 +0100 Subject: [PATCH] form:FileOption: add file "accept" validation handling --- src/utils/form.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/utils/form.py b/src/utils/form.py index 25c985a90..54370488d 100644 --- a/src/utils/form.py +++ b/src/utils/form.py @@ -1272,7 +1272,7 @@ class FileOption(BaseInputOption): type: Literal[OptionType.file] = OptionType.file # `FilePath` for CLI (path must exists and must be a file) # `bytes` for API (a base64 encoded file actually) - accept: Union[str, None] = "" # currently only used by the web-admin + accept: Union[list[str], None] = None # currently only used by the web-admin default: Union[str, None] _annotation = str # TODO could be Path at some point _upload_dirs: set[str] = set() @@ -1291,6 +1291,9 @@ class FileOption(BaseInputOption): def _get_field_attrs(self) -> dict[str, Any]: attrs = super()._get_field_attrs() + if self.accept: + attrs["accept"] = self.accept # extra + attrs["bind"] = self.bind return attrs @@ -1317,8 +1320,14 @@ class FileOption(BaseInputOption): else: content = b64decode(value) + accept_list = field.field_info.extra.get("accept") mimetype = Magic(mime=True).from_buffer(content) + if accept_list and mimetype not in accept_list: + raise YunohostValidationError( + f"Unsupported image type : {mimetype}", raw=True + ) + ext = mimetypes.guess_extension(mimetype) return content, ext