File size: 3,388 Bytes
b2c626c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
try:
    from aiohttp import web
    from server import PromptServer
except Exception:
    web = None
    PromptServer = None

from .auto_detect import calculate_resolution, safe_float, safe_int
from .log_system import create_module_logger


log = create_module_logger(__name__)
_routes_registered = False


def _payload_bool(payload, key, default=False):
    value = payload.get(key, default)
    if isinstance(value, bool):
        return value
    if isinstance(value, str):
        return value.lower() in ("1", "true", "yes", "on")
    return bool(value)


def _normalize_payload(payload):
    return {
        "action": payload.get("action", "rescale"),
        "width": safe_int(payload.get("width"), 512),
        "height": safe_int(payload.get("height"), 512),
        "auto_fit_on_change": _payload_bool(payload, "auto_fit_on_change"),
        "auto_resize_on_change": _payload_bool(payload, "auto_resize_on_change"),
        "auto_snap_on_change": _payload_bool(payload, "auto_snap_on_change"),
        "smart_fit": _payload_bool(payload, "smart_fit"),
        "use_custom_calc": _payload_bool(payload, "use_custom_calc"),
        "preserve_scaling_ratio": _payload_bool(payload, "preserve_scaling_ratio"),
        "selected_category": payload.get("selected_category") or "",
        "snap_value": safe_int(payload.get("snap_value"), 64),
        "upscale_value": safe_float(payload.get("upscale_value"), 1.0),
        "target_resolution": safe_int(payload.get("target_resolution"), 1080),
        "target_megapixels": safe_float(payload.get("target_megapixels"), 2.0),
        "rescale_mode": payload.get("rescale_mode") or "resolution",
        "presets_json": payload.get("presets_json") or "{}",
        "scale_value": safe_float(payload.get("scale_value"), 1.0),
    }


def register_calculation_routes():
    global _routes_registered
    if _routes_registered:
        log.debug("Calculation routes already registered")
        return
    if PromptServer is None or getattr(PromptServer, "instance", None) is None or web is None:
        log.warning("Calculation routes unavailable because PromptServer or aiohttp is missing")
        return

    @PromptServer.instance.routes.post("/resolutionmaster/calculate")
    async def calculate_resolutionmaster(request):
        try:
            payload = await request.json()
            if not isinstance(payload, dict):
                raise ValueError("Request body must be a JSON object.")

            normalized = _normalize_payload(payload)
            log.debug(
                "Calculation request",
                "action=",
                normalized["action"],
                "input=",
                f"{normalized['width']}x{normalized['height']}",
            )
            result = calculate_resolution(**normalized)
            log.debug("Calculation response", result)
            return web.json_response({"ok": True, **result})
        except ValueError as error:
            log.warning("Invalid calculation request", error)
            return web.json_response({"ok": False, "error": str(error)}, status=400)
        except Exception as error:
            log.exception("Unhandled calculation request error")
            return web.json_response({"ok": False, "error": str(error)}, status=500)

    _routes_registered = True
    log.info("Registered calculation route", "/resolutionmaster/calculate")