From 485d9ea709739cddbabc8311a91a86f0e062655e Mon Sep 17 00:00:00 2001 From: "N0\\A" Date: Wed, 29 Oct 2025 09:43:22 +0100 Subject: [PATCH] A **LOT** better broswer share UI --- core/http_share.py | 307 ++++++++++++++++++++++++++++++++------------- 1 file changed, 221 insertions(+), 86 deletions(-) diff --git a/core/http_share.py b/core/http_share.py index 58cdfb7..f68173f 100644 --- a/core/http_share.py +++ b/core/http_share.py @@ -7,6 +7,7 @@ from pathlib import Path from typing import List, Optional, Callable from http.server import HTTPServer, BaseHTTPRequestHandler import html +import json def format_size(bytes_val: int) -> str: if bytes_val is None: return "" @@ -28,78 +29,193 @@ class FileShareHandler(BaseHTTPRequestHandler): def log_message(self, format, *args): pass - def _get_base_html(self, title: str, body_content: str) -> str: + 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} + """ @@ -108,64 +224,83 @@ class FileShareHandler(BaseHTTPRequestHandler): self.send_combined_index_page() elif self.path.startswith('/download/'): self.handle_download() + elif self.path == '/api/data': + self.send_api_data() else: self.send_error(404, "Not Found") + def _get_api_data_dict(self): + files_data = [] + for i, filepath in enumerate(self.shared_files): + try: + path = Path(filepath) + if path.exists() and path.is_file(): + files_data.append({ + "name": html.escape(path.name), + "size": format_size(path.stat().st_size), + "url": f"/download/{i}" + }) + except Exception: + continue + + return { + "text": html.escape(self.shared_text or ""), + "files": files_data + } + def send_combined_index_page(self): - body_parts = ["

CLARA Share

"] - has_content = False + has_content = bool(self.shared_text or self.shared_files) + + main_content = f"""
+
+
+
+
+
+
+

Quick Info

+

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

+

Status:
Server running

+
+
+
+

No content is currently being shared.

+
+
""" % self.server.server_address[1] #type: ignore - # Text section - if self.shared_text: - has_content = True - escaped_text = html.escape(self.shared_text) - body_parts.append(f"""
-

Shared Text

-

Select the text below and copy it.

- -
""") + initial_data_dict = self._get_api_data_dict() + json_string = json.dumps(initial_data_dict) + initial_data_script = f'' - # Files section - if self.shared_files: - has_content = True - file_rows = [] - for i, filepath in enumerate(self.shared_files): - try: - path = Path(filepath) - if path.exists() and path.is_file(): - file_rows.append( - f'' - f'{html.escape(path.name)}' - f'{format_size(path.stat().st_size)}' - f'Download' - f'' - ) - except Exception: - continue - - if not file_rows: - file_table = '

No valid files are currently being shared.

' - else: - file_table = f""" - - {''.join(file_rows)} -
FilenameSizeLink
""" - - body_parts.append(f"""
-

Shared Files

-{file_table} -
""") - - if not has_content: - body_parts.append("

No content is currently being shared.

") - - html_content = self._get_base_html("CLARA Share", "".join(body_parts)).encode('utf-8') + html_content = self._get_base_html( + "CLARA Share", + main_content, + initial_data_script=initial_data_script + ).encode('utf-8') self.send_response(200) self.send_header('Content-Type', 'text/html; charset=utf-8') self.send_header('Content-Length', str(len(html_content))) self.end_headers() self.wfile.write(html_content) + + def send_api_data(self): + data = self._get_api_data_dict() + json_response = json.dumps(data).encode('utf-8') + + self.send_response(200) + self.send_header('Content-Type', 'application/json; charset=utf-8') + self.send_header('Content-Length', str(len(json_response))) + self.end_headers() + self.wfile.write(json_response) + + def _get_local_ip(self) -> str: + try: + with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s: + s.connect(("8.8.8.8", 80)) + return s.getsockname()[0] + except Exception: + return "127.0.0.1" def handle_download(self): try: