mirror of
https://github.com/TheFunny/TelegramTwitterMediaBot.git
synced 2026-10-04 01:02:12 +00:00
refactor(download): fold apply_media_headers into media_request
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).
This commit is contained in:
@@ -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
|
/// Prepares a media download: refuses a URL pointing inside the host's own
|
||||||
/// network ([`FetchError::Blocked`], permanent — the same URL would be refused
|
/// network ([`FetchError::Blocked`], permanent — the same URL would be refused
|
||||||
/// again), then applies the site's media headers. One choke point so every
|
/// again), then applies every site's media-header rule (pixiv's `Referer` for
|
||||||
/// download path gets the guard.
|
/// 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<reqwest::RequestBuilder, FetchError> {
|
fn media_request(url: &str) -> Result<reqwest::RequestBuilder, FetchError> {
|
||||||
let parsed = url::Url::parse(url).map_err(|e| {
|
let parsed = url::Url::parse(url).map_err(|e| {
|
||||||
log::warn!("media url is not a url: {e}");
|
log::warn!("media url is not a url: {e}");
|
||||||
@@ -208,13 +210,7 @@ fn media_request(url: &str) -> Result<reqwest::RequestBuilder, FetchError> {
|
|||||||
log::warn!("refusing to fetch media from the host's own network");
|
log::warn!("refusing to fetch media from the host's own network");
|
||||||
return Err(FetchError::Blocked);
|
return Err(FetchError::Blocked);
|
||||||
}
|
}
|
||||||
Ok(apply_media_headers(MEDIA_CLIENT.get(parsed), url))
|
let mut request = MEDIA_CLIENT.get(parsed);
|
||||||
}
|
|
||||||
|
|
||||||
/// 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 {
|
|
||||||
for site in SITES.iter() {
|
for site in SITES.iter() {
|
||||||
if let Some(headers) = site.media_headers(url) {
|
if let Some(headers) = site.media_headers(url) {
|
||||||
for (name, value) in headers {
|
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
|
/// Downloads a media file with a hard size cap: the body is streamed and the
|
||||||
|
|||||||
Reference in New Issue
Block a user