mirror of
https://github.com/YunoHost/yunohost.git
synced 2024-09-03 20:06:10 +02:00
form:FileOption: add file "accept" validation handling
This commit is contained in:
parent
4dc9d35304
commit
2b75c8f036
1 changed files with 10 additions and 1 deletions
|
@ -1272,7 +1272,7 @@ class FileOption(BaseInputOption):
|
||||||
type: Literal[OptionType.file] = OptionType.file
|
type: Literal[OptionType.file] = OptionType.file
|
||||||
# `FilePath` for CLI (path must exists and must be a file)
|
# `FilePath` for CLI (path must exists and must be a file)
|
||||||
# `bytes` for API (a base64 encoded file actually)
|
# `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]
|
default: Union[str, None]
|
||||||
_annotation = str # TODO could be Path at some point
|
_annotation = str # TODO could be Path at some point
|
||||||
_upload_dirs: set[str] = set()
|
_upload_dirs: set[str] = set()
|
||||||
|
@ -1291,6 +1291,9 @@ class FileOption(BaseInputOption):
|
||||||
def _get_field_attrs(self) -> dict[str, Any]:
|
def _get_field_attrs(self) -> dict[str, Any]:
|
||||||
attrs = super()._get_field_attrs()
|
attrs = super()._get_field_attrs()
|
||||||
|
|
||||||
|
if self.accept:
|
||||||
|
attrs["accept"] = self.accept # extra
|
||||||
|
|
||||||
attrs["bind"] = self.bind
|
attrs["bind"] = self.bind
|
||||||
|
|
||||||
return attrs
|
return attrs
|
||||||
|
@ -1317,8 +1320,14 @@ class FileOption(BaseInputOption):
|
||||||
else:
|
else:
|
||||||
content = b64decode(value)
|
content = b64decode(value)
|
||||||
|
|
||||||
|
accept_list = field.field_info.extra.get("accept")
|
||||||
mimetype = Magic(mime=True).from_buffer(content)
|
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)
|
ext = mimetypes.guess_extension(mimetype)
|
||||||
|
|
||||||
return content, ext
|
return content, ext
|
||||||
|
|
Loading…
Add table
Reference in a new issue