From 2b6cb506c7ff1f76a488309b249f4b1d99aaf4d0 Mon Sep 17 00:00:00 2001 From: YoursFunny Date: Thu, 24 Sep 2026 04:20:02 +0800 Subject: [PATCH] refactor(download): fold apply_media_headers into media_request MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit apply_media_headers had exactly one caller — media_request, the documented 'one choke point' every download already goes through — so the site-header walk sits inside that choke point now instead of one indirection past it, and the two doc comments merge into the one that describes both rules (guard + headers). --- crates/x-media/src/site/download.rs | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/crates/x-media/src/site/download.rs b/crates/x-media/src/site/download.rs index df41761..e6fec8e 100644 --- a/crates/x-media/src/site/download.rs +++ b/crates/x-media/src/site/download.rs @@ -197,8 +197,10 @@ fn media_url_allowed(url: &url::Url) -> bool { /// Prepares a media download: refuses a URL pointing inside the host's own /// network ([`FetchError::Blocked`], permanent — the same URL would be refused -/// again), then applies the site's media headers. One choke point so every -/// download path gets the guard. +/// again), then applies every site's media-header rule (pixiv's `Referer` for +/// pximg.net hotlink protection; sites contribute via `media_headers(url)`, so +/// the central download code carries no other per-site logic). One choke +/// point so every download path gets both. fn media_request(url: &str) -> Result { let parsed = url::Url::parse(url).map_err(|e| { log::warn!("media url is not a url: {e}"); @@ -208,13 +210,7 @@ fn media_request(url: &str) -> Result { log::warn!("refusing to fetch media from the host's own network"); return Err(FetchError::Blocked); } - Ok(apply_media_headers(MEDIA_CLIENT.get(parsed), url)) -} - -/// Applies every site's media-header rule to a download request (pixiv's -/// `Referer` for pximg.net hotlink protection). Sites contribute via their -/// `media_headers(url)` — the central download code carries no per-site logic. -fn apply_media_headers(mut request: reqwest::RequestBuilder, url: &str) -> reqwest::RequestBuilder { + let mut request = MEDIA_CLIENT.get(parsed); for site in SITES.iter() { if let Some(headers) = site.media_headers(url) { for (name, value) in headers { @@ -222,7 +218,7 @@ fn apply_media_headers(mut request: reqwest::RequestBuilder, url: &str) -> reqwe } } } - request + Ok(request) } /// Downloads a media file with a hard size cap: the body is streamed and the