From 790c9c3778a70bc1c3062236a6340c67bcb77828 Mon Sep 17 00:00:00 2001 From: "N0\\A" Date: Fri, 31 Oct 2025 16:25:56 +0100 Subject: [PATCH] separate share html --- core/http_share.py | 297 +++++++-------------------------------------- core/share.html | 247 +++++++++++++++++++++++++++++++++++++ 2 files changed, 288 insertions(+), 256 deletions(-) create mode 100644 core/share.html diff --git a/core/http_share.py b/core/http_share.py index 0a0aa1e..f51e1e8 100644 --- a/core/http_share.py +++ b/core/http_share.py @@ -25,242 +25,41 @@ class FileShareHandler(BaseHTTPRequestHandler): shared_files: List[str] = [] shared_text: Optional[str] = None on_download: Optional[Callable[[str, str], None]] = None + html_template: Optional[str] = None def log_message(self, format, *args): pass - def _get_base_html(self, title: str, body_content: str, initial_data_script: str = "") -> str: - return f""" - - - - - {html.escape(title)} - - - -
-
-
CLARA Share
-
Simple file & text sharing — local network
-
- {body_content} - -
- {initial_data_script} - - -""" - + @classmethod + def load_html_template(cls): + if cls.html_template is None: + template_path = Path(__file__).parent / "share.html" + try: + with open(template_path, 'r', encoding='utf-8') as f: + cls.html_template = f.read() + except FileNotFoundError: + raise RuntimeError(f"HTML template not found at {template_path}") + return cls.html_template + + def _get_base_html(self, hostname: str, url: str, total_size_info: str, + no_content_display: str, initial_data_json: str) -> str: + template = self.load_html_template() + + replacements = { + '{{TITLE}}': 'CLARA Share', + '{{HOSTNAME}}': html.escape(hostname), + '{{URL}}': html.escape(url), + '{{TOTAL_SIZE_INFO}}': total_size_info, + '{{NO_CONTENT_DISPLAY}}': no_content_display, + '{{INITIAL_DATA_JSON}}': initial_data_json + } + + result = template + for placeholder, value in replacements.items(): + result = result.replace(placeholder, value) + + return result + def do_GET(self): if self.path == '/': self.send_combined_index_page() @@ -305,35 +104,21 @@ class FileShareHandler(BaseHTTPRequestHandler): except (FileNotFoundError, OSError): pass if total_size_bytes > 0: - total_size_info = f'

Total Size:
{format_size(total_size_bytes)}

' - - main_content = f"""
-
-
-
-
-
-
-

Quick Info

-

Host:
{html.escape(hostname)}

-

URL:
{html.escape(f"http://{self._get_local_ip()}:%s/")}

- {total_size_info} -

Status:
Server running

-
-
-
-

No content is currently being shared.

-
-
""" % self.server.server_address[1] #type: ignore + total_size_info = format_size(total_size_bytes) initial_data_dict = self._get_api_data_dict() json_string = json.dumps(initial_data_dict) - initial_data_script = f'' + initial_data_json = json.dumps(json_string) + + url = f"http://{self._get_local_ip()}:{self.server.server_address[1]}/" #type: ignore + no_content_display = 'none' if has_content else 'block' html_content = self._get_base_html( - "CLARA Share", - main_content, - initial_data_script=initial_data_script + hostname=hostname, + url=url, + total_size_info=total_size_info, + no_content_display=no_content_display, + initial_data_json=initial_data_json ).encode('utf-8') self.send_response(200) diff --git a/core/share.html b/core/share.html new file mode 100644 index 0000000..d99bf54 --- /dev/null +++ b/core/share.html @@ -0,0 +1,247 @@ + + + + + + {{TITLE}} + + + +
+
+
CLARA Share
+
Simple file & text sharing — local network
+
+
+
+
+
+
+
+
+

Quick Info

+

Host:
{{HOSTNAME}}

+

URL:
{{URL}}

+

Total Size:
{{TOTAL_SIZE_INFO}}

+

Status:
Server running

+
+
+
+

No content is currently being shared.

+
+
+ +
+ + + + \ No newline at end of file