fix(api): check serve and service dl defaults at job admission

This commit is contained in:
imSp4rky
2026-11-28 14:32:31 -07:00
parent 5f675bd5ff
commit daa9caa02b
5 changed files with 81 additions and 10 deletions
+29 -9
View File
@@ -13,9 +13,9 @@ from unshackle.core.api import handlers
from unshackle.core.api.download_manager import plan_keys, stamped_keys
from unshackle.core.api.errors import APIError
from unshackle.core.config import config
from unshackle.core.dl_options import DEFAULT_DOWNLOAD_PARAMS, DL_OPTION_API_NAMES
from unshackle.core.dl_options import DEFAULT_DOWNLOAD_PARAMS, DL_OPTION_ALIASES, DL_OPTION_API_NAMES
from unshackle.core.remote_protocol import APIErrorCode
from unshackle.core.run_plan import PlanError, api_plan
from unshackle.core.run_plan import PlanError, api_plan, cast_plan
from unshackle.core.services import ROUTING_PARAMS, Services
from unshackle.core.utils.collections import as_list, ci_get
@@ -81,10 +81,13 @@ def job_spec(data: dict[str, Any], server: str | None, request: web.Request | No
sent = {k: v for k, v in data.items() if k not in ENVELOPE_KEYS}
serve_defaults = {k: v for k, v in (config.serve or {}).items() if k in DEFAULT_DOWNLOAD_PARAMS and v is not None}
# A relayed job gets no local service overrides, so none of its keys may count as defaults.
service_defaults = {} if server else service_dl_defaults(service)
check_defaults("serve", serve_defaults, {**service_defaults, **sent}, data)
check_defaults(f"services.{service}.dl", service_defaults, sent, data)
params = {
**DEFAULT_DOWNLOAD_PARAMS,
**serve_defaults,
**({} if server else service_dl_defaults(service)),
**service_defaults,
**sent,
"client_keys": sorted(sent),
}
@@ -148,11 +151,6 @@ def check_parameters(data: dict[str, Any]) -> dict[str, Any]:
raise invalid("output_dir must be a path under the server's downloads directory.", data)
checked["output_dir"] = str(target)
if data.get("all_drm") and data.get("cdm"):
raise invalid(
"all_drm needs a CDM for each DRM system, so it cannot be used with cdm, which selects one device.", data
)
for name in POSITIVE_INT_KEYS:
value = data.get(name)
if value is not None and (isinstance(value, bool) or not isinstance(value, int) or value <= 0):
@@ -184,10 +182,32 @@ def check_parameters(data: dict[str, Any]) -> dict[str, Any]:
return checked
def check_defaults(source: str, defaults: dict[str, Any], overrides: dict[str, Any], data: dict[str, Any]) -> None:
"""INVALID_PARAMETERS, prefixed with the config block ``source``, for a default that a request body could not send.
Only the defaults that ``overrides`` does not replace are read, as click reads a default only for an option not
given. They are read as dl casts them, so a value in the CLI form of the config (``lang: en``) passes. The
operator's ``output_dir`` and ``profile`` are not held to the client rules.
"""
used = {k: v for k, v in defaults.items() if k not in overrides and k not in ("output_dir", "profile")}
if not used:
return
try:
cast = cast_plan(used, ()).params
check_parameters({k: cast[DL_OPTION_ALIASES.get(k, k)] for k in used})
except (PlanError, APIError) as e:
raise invalid(f"{source}: {e.message}", data) from None
def check_plan(params: dict[str, Any]) -> None:
"""INVALID_PARAMETERS when dl would refuse the job's merged parameters, as the worker plans them."""
"""INVALID_PARAMETERS when dl would refuse the job's merged parameters, as the worker plans them, or when they
pair ``all_drm`` with a ``cdm``."""
from unshackle.commands.dl import subtitle_look
if params.get("all_drm") and params.get("cdm"):
raise invalid(
"all_drm needs a CDM for each DRM system, so it cannot be used with cdm, which selects one device.", params
)
try:
plan = api_plan(params, plan_keys(params)).params
except PlanError as e:
+3
View File
@@ -46,6 +46,9 @@ def cast_service_options(params: Iterable[click.Parameter], ctx: Any, options: d
raise click.BadParameter("expected a number, not a boolean", ctx=ctx, param=param)
try:
cast[k] = param.type_cast_value(ctx, v)
except click.BadParameter as e:
e.param = e.param or param
raise
except (TypeError, ValueError, AttributeError, OverflowError) as e:
raise click.BadParameter(f"not a valid {param.type.name} value", ctx=ctx, param=param) from e
return cast