Refactor render function in website.py to improve HTML handling and caching logic

This commit is contained in:
hlohaus 2025-09-07 01:02:35 +02:00
parent 09b35403e3
commit ffae27b3cf

View file

@ -3,6 +3,7 @@ from __future__ import annotations
import os import os
import requests import requests
from datetime import datetime from datetime import datetime
from urllib.parse import quote, unquote
from flask import send_from_directory, redirect, request from flask import send_from_directory, redirect, request
from ...image.copy_images import secure_filename from ...image.copy_images import secure_filename
@ -17,10 +18,14 @@ def redirect_home():
def render(filename = "home", download_url: str = DOWNLOAD_URL): def render(filename = "home", download_url: str = DOWNLOAD_URL):
if download_url == DOWNLOAD_URL: if download_url == DOWNLOAD_URL:
filename += ("" if "." in filename else ".html") filename += ("" if "." in filename else ".html")
html = None
if os.path.exists(DIST_DIR) and not request.args.get("debug"): if os.path.exists(DIST_DIR) and not request.args.get("debug"):
path = os.path.abspath(os.path.join(os.path.dirname(DIST_DIR), filename)) path = os.path.abspath(os.path.join(os.path.dirname(DIST_DIR), filename))
if os.path.exists(path): if os.path.exists(path):
return send_from_directory(os.path.dirname(path), os.path.basename(path)) if download_url == DOWNLOAD_URL:
html = open(path, 'r', encoding='utf-8').read()
else:
return send_from_directory(os.path.dirname(path), os.path.basename(path))
try: try:
latest_version = version.utils.latest_version latest_version = version.utils.latest_version
except VersionNotFoundError: except VersionNotFoundError:
@ -34,22 +39,24 @@ def render(filename = "home", download_url: str = DOWNLOAD_URL):
is_temp = True is_temp = True
else: else:
os.makedirs(cache_dir, exist_ok=True) os.makedirs(cache_dir, exist_ok=True)
response = requests.get(f"{download_url}{filename}") if html is None:
if not response.ok: response = requests.get(f"{download_url}{filename}")
found = None if not response.ok:
for root, _, files in os.walk(cache_dir): found = None
for file in files: for root, _, files in os.walk(cache_dir):
if file.startswith(secure_filename(filename)): for file in files:
found = os.path.abspath(root), file if file.startswith(secure_filename(filename)):
break found = os.path.abspath(root), file
if found: break
return send_from_directory(found[0], found[1]) if found:
else: return send_from_directory(found[0], found[1])
response.raise_for_status() else:
html = response.text response.raise_for_status()
html = response.text
html = html.replace("../dist/", f"dist/")
html = html.replace("\"dist/", f"\"{STATIC_URL}dist/")
html = html.replace(JSDELIVR_URL, "/") html = html.replace(JSDELIVR_URL, "/")
html = html.replace("../dist/", f"dist/") html = html.replace("{{ v }}", quote(unquote(request.query_string.decode())) or str(version.utils.current_version))
html = html.replace("\"dist/", f"\"{STATIC_URL}dist/")
if is_temp: if is_temp:
return html return html
with open(cache_file, 'w', encoding='utf-8') as f: with open(cache_file, 'w', encoding='utf-8') as f: