Quick Info
+URL:
{html.escape(f"http://{self._get_local_ip()}:%s/")}
Status:
Server running
No content is currently being shared.
+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"""
URL:
{html.escape(f"http://{self._get_local_ip()}:%s/")}
Status:
Server running
No content is currently being shared.
+Select the text below and copy it.
- -No valid files are currently being shared.
' - else: - file_table = f"""| Filename | Size | Link |
|---|
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: