"""Setup wizard: form parsing rules and the two admin-facing HTML pages. Pure presentation/logic — no router here; routes live in apps.base.pages. """ import html from urllib.parse import parse_qs from typing import Any from fastapi import Request from fastapi.responses import HTMLResponse from core.settings import DEFAULT_CONFIG, settings from core.version import APP_VERSION FILE_SIZE_UNITS = {"KB": 1024, "MB": 1024**2, "GB": 1024**3} SAVE_TIME_UNITS = {"second": 1, "minute": 60, "hour": 3600, "day": 86400} EXPIRE_STYLE_OPTIONS = [ ("day", "按天"), ("hour", "按小时"), ("minute", "按分钟"), ("forever", "永久"), ("count", "按取件次数"), ] def normalize_public_flag(value) -> int: if isinstance(value, str): return int(value.strip().lower() in {"1", "true", "on", "yes"}) return int(bool(value)) def build_public_config() -> dict: return { "name": settings.name, "description": settings.description, "explain": settings.page_explain, "upload_size": settings.upload_size, "allowed_file_types": settings.allowed_file_types, "expire_style": settings.expire_style, "enable_chunk": settings.enable_chunk, "open_upload": settings.open_upload, "notify_title": settings.notify_title, "notify_content": settings.notify_content, "show_admin_address": normalize_public_flag(settings.show_admin_addr), "max_save_seconds": settings.max_save_seconds, "background": str(settings.background or ""), } def build_public_meta() -> dict: return { "version": APP_VERSION, "api": { "legacy_config": "/", "public_config": "/api/v1/config", "health": "/health", }, "features": { "chunk_upload": bool(settings.enable_chunk), "guest_upload": bool(settings.open_upload), "admin_address_visible": bool(normalize_public_flag(settings.show_admin_addr)), "expiration_modes": settings.expire_style, }, "limits": { "upload_size": settings.upload_size, "allowed_file_types": settings.allowed_file_types, "max_save_seconds": settings.max_save_seconds, "upload_window_minutes": settings.upload_minute, "upload_window_count": settings.upload_count, }, } def get_form_value(data: dict[str, Any], key: str, default: str = "") -> str: value = data.get(key, default) if isinstance(value, list): value = value[-1] if value else default return str(value if value is not None else default) def get_form_list(data: dict[str, Any], key: str) -> list[str]: value = data.get(key, []) if isinstance(value, list): return [str(item) for item in value if str(item)] if value: return [str(value)] return [] def normalize_bool_field(data: dict, key: str, default: bool) -> bool: if key not in data: return default return get_form_value(data, key).lower() in {"1", "true", "on", "yes"} def parse_int_field( data: dict[str, Any], key: str, default: int, label: str, min_value: int = 0, max_value: int | None = None, ) -> int: raw_value = get_form_value(data, key, str(default)).strip() try: value = int(raw_value) except ValueError: raise ValueError(f"{label} 必须是整数") if value < min_value: raise ValueError(f"{label} 不能小于 {min_value}") if max_value is not None and value > max_value: raise ValueError(f"{label} 不能大于 {max_value}") return value def parse_allowed_file_types(value: str) -> list[str]: items = [item.strip() for item in value.split(",") if item.strip()] return items or ["*"] def parse_setup_options(data: dict) -> dict: upload_size_unit = get_form_value(data, "upload_size_unit", "MB").upper() if upload_size_unit not in FILE_SIZE_UNITS: raise ValueError("文件大小单位不正确") upload_size_value = parse_int_field( data, "upload_size_value", 10, "文件大小限制", min_value=1 ) save_time_unit = get_form_value(data, "save_time_unit", "day") if save_time_unit not in SAVE_TIME_UNITS: raise ValueError("最长保存时间单位不正确") save_time_value = parse_int_field( data, "save_time_value", 0, "最长保存时间", min_value=0 ) expire_styles = get_form_list(data, "expire_style") valid_expire_styles = {style for style, _label in EXPIRE_STYLE_OPTIONS} expire_styles = [style for style in expire_styles if style in valid_expire_styles] if not expire_styles: raise ValueError("至少需要选择一种过期方式") code_generate_type = get_form_value( data, "code_generate_type", DEFAULT_CONFIG["code_generate_type"] ) if code_generate_type not in {"number", "secret"}: raise ValueError("提取码类型不正确") return { "allowed_file_types": parse_allowed_file_types( get_form_value(data, "allowed_file_types", "*") ), "code_generate_type": code_generate_type, "enable_chunk": int(normalize_bool_field(data, "enable_chunk", False)), "error_count": parse_int_field( data, "error_count", DEFAULT_CONFIG["error_count"], "取件错误次数限制", 1 ), "error_minute": parse_int_field( data, "error_minute", DEFAULT_CONFIG["error_minute"], "取件错误检测窗口", 1 ), "login_count": parse_int_field( data, "login_count", DEFAULT_CONFIG["login_count"], "登录失败次数限制", 1 ), "login_minute": parse_int_field( data, "login_minute", DEFAULT_CONFIG["login_minute"], "登录失败检测窗口", 1 ), "expire_style": expire_styles, "max_save_seconds": save_time_value * SAVE_TIME_UNITS[save_time_unit], "open_upload": int(normalize_bool_field(data, "open_upload", True)), "upload_count": parse_int_field( data, "upload_count", DEFAULT_CONFIG["upload_count"], "上传次数限制", 1 ), "upload_minute": parse_int_field( data, "upload_minute", DEFAULT_CONFIG["upload_minute"], "上传检测窗口", 1 ), "upload_size": upload_size_value * FILE_SIZE_UNITS[upload_size_unit], } def build_expire_style_inputs(selected_styles: list[str]) -> str: inputs = [] selected = set(selected_styles) for style, label in EXPIRE_STYLE_OPTIONS: checked = " checked" if style in selected else "" inputs.append( f'' ) return "\n ".join(inputs) def build_setup_page(error: str = "", form: dict | None = None) -> str: form = form or {} escaped_error = html.escape(error) escaped_site_name = html.escape( get_form_value(form, "site_name", DEFAULT_CONFIG["name"]) ) escaped_allowed_types = html.escape(get_form_value(form, "allowed_file_types", "*")) upload_size_value = html.escape(get_form_value(form, "upload_size_value", "10")) upload_size_unit = get_form_value(form, "upload_size_unit", "MB").upper() save_time_value = html.escape(get_form_value(form, "save_time_value", "0")) save_time_unit = get_form_value(form, "save_time_unit", "day") upload_minute = html.escape( get_form_value(form, "upload_minute", str(DEFAULT_CONFIG["upload_minute"])) ) upload_count = html.escape( get_form_value(form, "upload_count", str(DEFAULT_CONFIG["upload_count"])) ) error_minute = html.escape( get_form_value(form, "error_minute", str(DEFAULT_CONFIG["error_minute"])) ) error_count = html.escape( get_form_value(form, "error_count", str(DEFAULT_CONFIG["error_count"])) ) login_minute = html.escape( get_form_value(form, "login_minute", str(DEFAULT_CONFIG["login_minute"])) ) login_count = html.escape( get_form_value(form, "login_count", str(DEFAULT_CONFIG["login_count"])) ) open_upload_checked = ( " checked" if normalize_bool_field(form, "open_upload", True) else "" ) chunk_checked = ( " checked" if normalize_bool_field(form, "enable_chunk", False) else "" ) code_generate_type = get_form_value( form, "code_generate_type", DEFAULT_CONFIG["code_generate_type"] ) selected_expire_styles = get_form_list(form, "expire_style") or list( DEFAULT_CONFIG["expire_style"] ) expire_style_inputs = build_expire_style_inputs(selected_expire_styles) size_unit_options = "\n".join( f'' for unit in FILE_SIZE_UNITS ) save_time_unit_options = "\n".join( f'' for unit, label in [ ("second", "秒"), ("minute", "分钟"), ("hour", "小时"), ("day", "天"), ] ) code_type_options = "\n".join( f'' for value, label in [("number", "数字"), ("secret", "随机字符")] ) error_block = ( f'' if escaped_error else "" ) return f""" 初始化 FileCodeBox
FCB

初始化 FileCodeBox

首次配置管理员密码、上传限制和取件策略,后续可在后台调整。

首次配置向导
{error_block}
基础设置
上传设置
取件与保存
可用策略
{expire_style_inputs}
""" def build_setup_success_page() -> str: return """ 初始化完成

初始化完成

管理员密码已设置,请使用刚才的密码登录后台。

进入后台
""" def setup_response(content: str, status_code: int = 200) -> HTMLResponse: return HTMLResponse( content=content, status_code=status_code, media_type="text/html", headers={"Cache-Control": "no-store"}, ) def is_setup_path(path: str) -> bool: return path.rstrip("/") == "/setup" def wants_html_response(request: Request) -> bool: if request.method not in {"GET", "HEAD"}: return False accept = request.headers.get("accept", "") return not accept or "text/html" in accept or "*/*" in accept async def read_setup_payload(request: Request) -> dict: content_type = request.headers.get("content-type", "") if "application/json" in content_type: data = await request.json() return data if isinstance(data, dict) else {} body = (await request.body()).decode("utf-8") return { key: values if len(values) > 1 else values[-1] for key, values in parse_qs(body).items() }