From c641f099c5ae8162a91a3e020c16aa944a1df221 Mon Sep 17 00:00:00 2001 From: axolotle Date: Thu, 7 Sep 2023 17:57:08 +0200 Subject: [PATCH] add temp messy file handling for portal custom logo --- share/config_domain.toml | 3 +-- src/domain.py | 46 +++++++++++++++++++++++++++++++--------- 2 files changed, 37 insertions(+), 12 deletions(-) diff --git a/share/config_domain.toml b/share/config_domain.toml index b6a0b51a7a..27493b4e75 100644 --- a/share/config_domain.toml +++ b/share/config_domain.toml @@ -16,8 +16,7 @@ name = "Features" default = "YunoHost" [feature.portal.portal_logo] - type = "string" - default = "" + type = "file" [feature.portal.portal_theme] type = "select" diff --git a/src/domain.py b/src/domain.py index 9ddb67a769..a476112b29 100644 --- a/src/domain.py +++ b/src/domain.py @@ -728,15 +728,6 @@ def _apply(self): other_app=app_map(raw=True)[self.entity]["/"]["id"], ) - super()._apply() - - # Reload ssowat if default app changed - if ( - "default_app" in self.future_values - and self.future_values["default_app"] != self.values["default_app"] - ): - app_ssowatconf() - portal_options = [ "default_app", "show_other_domains_apps", @@ -755,11 +746,46 @@ def _apply(self): portal_values = { option: self.future_values[option] for option in portal_options } - # FIXME config file should be readable by the portal entity + if portal_values["portal_logo"].startswith("/tmp/ynh_filequestion_"): + # FIXME rework this whole mess + # currently only handling API sent images, need to adapt FileOption + # to handle file extensions and file saving since "bind" is only + # done in bash helpers which are not executed in domain config + if "portal_logo[name]" in self.args or self.values["portal_logo"]: + import mimetypes + import base64 + + if "portal_logo[name]" in self.args: + # FIXME choose where to save the file + filepath = os.path.join("/tmp", self.args["portal_logo[name]"]) + # move the temp file created by FileOption with proper name and extension + os.rename(self.new_values["portal_logo"], filepath) + mimetype = mimetypes.guess_type(filepath) + else: + # image has already been saved, do not overwrite it with the empty temp file created by the FileOption + filepath = self.values["portal_logo"] + mimetype = mimetypes.guess_type(filepath) + + # save the proper path to config panel settings + self.new_values["portal_logo"] = filepath + # save the base64 content with mimetype to portal settings + with open(filepath, "rb") as f: + portal_values["portal_logo"] = mimetype[0] + ":" + base64.b64encode(f.read()).decode("utf-8") + + # FIXME config file should be readable by non-root portal entity write_to_yaml( f"{DOMAIN_SETTINGS_DIR}/{self.entity}.portal.yml", portal_values ) + super()._apply() + + # Reload ssowat if default app changed + if ( + "default_app" in self.future_values + and self.future_values["default_app"] != self.values["default_app"] + ): + app_ssowatconf() + stuff_to_regen_conf = [] if ( "xmpp" in self.future_values