Use alt CDN URL when request header is passed (#4921)

* Use alt CDN URL when request header is passed

* Modify version routes to use alt CDN
This commit is contained in:
aecsocket
2025-12-17 18:12:29 +00:00
committed by GitHub
parent fd08dff1e7
commit 609e3896eb
18 changed files with 282 additions and 43 deletions

View File

@@ -9,6 +9,7 @@ use crate::auth::get_user_from_headers;
use crate::database;
use crate::database::models::legacy_loader_fields::MinecraftGameVersion;
use crate::database::redis::RedisPool;
use crate::file_hosting::{CdnConfig, UseAltCdn};
use crate::models::pats::Scopes;
use crate::models::projects::VersionType;
use crate::queue::session::AuthQueue;
@@ -37,6 +38,8 @@ pub async fn forge_updates(
pool: web::Data<PgPool>,
redis: web::Data<RedisPool>,
session_queue: web::Data<AuthQueue>,
cdn_config: web::Data<CdnConfig>,
UseAltCdn(use_alt_cdn): UseAltCdn,
) -> Result<HttpResponse, ApiError> {
const ERROR: &str = "The specified project does not exist!";
@@ -82,6 +85,7 @@ pub async fn forge_updates(
&user_option,
&pool,
&redis,
&cdn_config.make_choice(use_alt_cdn),
)
.await?;

View File

@@ -1,7 +1,8 @@
use crate::database::models::categories::LinkPlatform;
use crate::database::models::{project_item, version_item};
use crate::database::redis::RedisPool;
use crate::file_hosting::FileHost;
use crate::file_hosting::{CdnChoice, CdnConfig};
use crate::file_hosting::{FileHost, UseAltCdn};
use crate::models::projects::{
Link, MonetizationStatus, Project, ProjectStatus, SearchRequest, Version,
};
@@ -267,6 +268,8 @@ pub async fn dependency_list(
pool: web::Data<PgPool>,
redis: web::Data<RedisPool>,
session_queue: web::Data<AuthQueue>,
cdn_config: web::Data<CdnConfig>,
use_alt_cdn: UseAltCdn,
) -> Result<HttpResponse, ApiError> {
// TODO: tests, probably
let response = v3::projects::dependency_list(
@@ -275,6 +278,8 @@ pub async fn dependency_list(
pool.clone(),
redis.clone(),
session_queue,
cdn_config,
use_alt_cdn,
)
.await
.or_else(v2_reroute::flatten_404_error)?;
@@ -546,7 +551,7 @@ pub async fn project_edit(
version_item::DBVersion::get_many(&version_ids, &**pool, &redis)
.await?;
for version in versions {
let version = Version::from(version);
let version = Version::from(version, &CdnChoice::Default);
let mut fields = version.fields;
let (current_client_side, current_server_side) =
v2_reroute::convert_v3_side_types_to_v2_side_types(

View File

@@ -1,6 +1,7 @@
use super::ApiError;
use crate::database::ReadOnlyPgPool;
use crate::database::redis::RedisPool;
use crate::file_hosting::{CdnConfig, UseAltCdn};
use crate::models::projects::{Project, Version, VersionType};
use crate::models::v2::projects::{LegacyProject, LegacyVersion};
use crate::queue::session::AuthQueue;
@@ -38,6 +39,8 @@ pub async fn get_version_from_hash(
redis: web::Data<RedisPool>,
hash_query: web::Query<HashQuery>,
session_queue: web::Data<AuthQueue>,
cdn_config: web::Data<CdnConfig>,
use_alt_cdn: UseAltCdn,
) -> Result<HttpResponse, ApiError> {
let response = v3::version_file::get_version_from_hash(
req,
@@ -46,6 +49,8 @@ pub async fn get_version_from_hash(
redis,
hash_query,
session_queue,
cdn_config,
use_alt_cdn,
)
.await
.or_else(v2_reroute::flatten_404_error)?;
@@ -69,6 +74,7 @@ pub async fn download_version(
redis: web::Data<RedisPool>,
hash_query: web::Query<HashQuery>,
session_queue: web::Data<AuthQueue>,
use_alt_cdn: UseAltCdn,
) -> Result<HttpResponse, ApiError> {
// Returns TemporaryRedirect, so no need to convert to V2
v3::version_file::download_version(
@@ -78,6 +84,7 @@ pub async fn download_version(
redis,
hash_query,
session_queue,
use_alt_cdn,
)
.await
.or_else(v2_reroute::flatten_404_error)
@@ -122,6 +129,8 @@ pub async fn get_update_from_hash(
hash_query: web::Query<HashQuery>,
update_data: web::Json<UpdateData>,
session_queue: web::Data<AuthQueue>,
cdn_config: web::Data<CdnConfig>,
use_alt_cdn: UseAltCdn,
) -> Result<HttpResponse, ApiError> {
let update_data = update_data.into_inner();
let mut loader_fields = HashMap::new();
@@ -146,6 +155,8 @@ pub async fn get_update_from_hash(
hash_query,
web::Json(update_data),
session_queue,
cdn_config,
use_alt_cdn,
)
.await
.or_else(v2_reroute::flatten_404_error)?;
@@ -175,6 +186,8 @@ pub async fn get_versions_from_hashes(
redis: web::Data<RedisPool>,
file_data: web::Json<FileHashes>,
session_queue: web::Data<AuthQueue>,
cdn_config: web::Data<CdnConfig>,
use_alt_cdn: UseAltCdn,
) -> Result<HttpResponse, ApiError> {
let file_data = file_data.into_inner();
let file_data = v3::version_file::FileHashes {
@@ -187,6 +200,8 @@ pub async fn get_versions_from_hashes(
redis,
web::Json(file_data),
session_queue,
cdn_config,
use_alt_cdn,
)
.await
.or_else(v2_reroute::flatten_404_error)?;
@@ -281,6 +296,8 @@ pub async fn update_files(
pool: web::Data<ReadOnlyPgPool>,
redis: web::Data<RedisPool>,
update_data: web::Json<ManyUpdateData>,
cdn_config: web::Data<CdnConfig>,
use_alt_cdn: UseAltCdn,
) -> Result<HttpResponse, ApiError> {
let update_data = update_data.into_inner();
let update_data = v3::version_file::ManyUpdateData {
@@ -291,10 +308,15 @@ pub async fn update_files(
hashes: update_data.hashes,
};
let response =
v3::version_file::update_files(pool, redis, web::Json(update_data))
.await
.or_else(v2_reroute::flatten_404_error)?;
let response = v3::version_file::update_files(
pool,
redis,
web::Json(update_data),
cdn_config,
use_alt_cdn,
)
.await
.or_else(v2_reroute::flatten_404_error)?;
// Convert response to V2 format
match v2_reroute::extract_ok_json::<HashMap<String, Version>>(response)
@@ -335,6 +357,8 @@ pub async fn update_individual_files(
redis: web::Data<RedisPool>,
update_data: web::Json<ManyFileUpdateData>,
session_queue: web::Data<AuthQueue>,
cdn_config: web::Data<CdnConfig>,
use_alt_cdn: UseAltCdn,
) -> Result<HttpResponse, ApiError> {
let update_data = update_data.into_inner();
let update_data = v3::version_file::ManyFileUpdateData {
@@ -368,6 +392,8 @@ pub async fn update_individual_files(
redis,
web::Json(update_data),
session_queue,
cdn_config,
use_alt_cdn,
)
.await
.or_else(v2_reroute::flatten_404_error)?;

View File

@@ -2,6 +2,7 @@ use std::collections::HashMap;
use super::ApiError;
use crate::database::redis::RedisPool;
use crate::file_hosting::{CdnConfig, UseAltCdn};
use crate::models;
use crate::models::ids::VersionId;
use crate::models::projects::{
@@ -47,6 +48,8 @@ pub async fn version_list(
pool: web::Data<PgPool>,
redis: web::Data<RedisPool>,
session_queue: web::Data<AuthQueue>,
cdn_config: web::Data<CdnConfig>,
use_alt_cdn: UseAltCdn,
) -> Result<HttpResponse, ApiError> {
let loaders = if let Some(loaders) = filters.loaders {
if let Ok(mut loaders) = serde_json::from_str::<Vec<String>>(&loaders) {
@@ -104,6 +107,8 @@ pub async fn version_list(
pool,
redis,
session_queue,
cdn_config,
use_alt_cdn,
)
.await
.or_else(v2_reroute::flatten_404_error)?;
@@ -129,6 +134,8 @@ pub async fn version_project_get(
pool: web::Data<PgPool>,
redis: web::Data<RedisPool>,
session_queue: web::Data<AuthQueue>,
cdn_config: web::Data<CdnConfig>,
use_alt_cdn: UseAltCdn,
) -> Result<HttpResponse, ApiError> {
let id = info.into_inner();
let response = v3::versions::version_project_get_helper(
@@ -137,6 +144,8 @@ pub async fn version_project_get(
pool,
redis,
session_queue,
cdn_config,
use_alt_cdn,
)
.await
.or_else(v2_reroute::flatten_404_error)?;
@@ -162,6 +171,8 @@ pub async fn versions_get(
pool: web::Data<PgPool>,
redis: web::Data<RedisPool>,
session_queue: web::Data<AuthQueue>,
cdn_config: web::Data<CdnConfig>,
use_alt_cdn: UseAltCdn,
) -> Result<HttpResponse, ApiError> {
let ids = v3::versions::VersionIds { ids: ids.ids };
let response = v3::versions::versions_get(
@@ -170,6 +181,8 @@ pub async fn versions_get(
pool,
redis,
session_queue,
cdn_config,
use_alt_cdn,
)
.await
.or_else(v2_reroute::flatten_404_error)?;
@@ -194,12 +207,21 @@ pub async fn version_get(
pool: web::Data<PgPool>,
redis: web::Data<RedisPool>,
session_queue: web::Data<AuthQueue>,
cdn_config: web::Data<CdnConfig>,
use_alt_cdn: UseAltCdn,
) -> Result<HttpResponse, ApiError> {
let id = info.into_inner().0;
let response =
v3::versions::version_get_helper(req, id, pool, redis, session_queue)
.await
.or_else(v2_reroute::flatten_404_error)?;
let response = v3::versions::version_get_helper(
req,
id,
pool,
redis,
session_queue,
cdn_config,
use_alt_cdn,
)
.await
.or_else(v2_reroute::flatten_404_error)?;
// Convert response to V2 format
match v2_reroute::extract_ok_json::<Version>(response).await {
Ok(version) => {
@@ -253,6 +275,8 @@ pub async fn version_edit(
redis: web::Data<RedisPool>,
new_version: web::Json<EditVersion>,
session_queue: web::Data<AuthQueue>,
cdn_config: web::Data<CdnConfig>,
use_alt_cdn: UseAltCdn,
) -> Result<HttpResponse, ApiError> {
let new_version = new_version.into_inner();
@@ -271,6 +295,8 @@ pub async fn version_edit(
pool.clone(),
redis.clone(),
session_queue.clone(),
cdn_config,
use_alt_cdn,
)
.await
.or_else(v2_reroute::flatten_404_error)?;

View File

@@ -9,7 +9,7 @@ use crate::database::models::thread_item::ThreadMessageBuilder;
use crate::database::models::{DBTeamMember, ids as db_ids, image_item};
use crate::database::redis::RedisPool;
use crate::database::{self, models as db_models};
use crate::file_hosting::{FileHost, FileHostPublicity};
use crate::file_hosting::{CdnConfig, FileHost, FileHostPublicity, UseAltCdn};
use crate::models;
use crate::models::ids::{ProjectId, VersionId};
use crate::models::images::ImageContext;
@@ -1068,6 +1068,8 @@ pub async fn dependency_list(
pool: web::Data<PgPool>,
redis: web::Data<RedisPool>,
session_queue: web::Data<AuthQueue>,
cdn_config: web::Data<CdnConfig>,
UseAltCdn(use_alt_cdn): UseAltCdn,
) -> Result<HttpResponse, ApiError> {
let string = info.into_inner().0;
@@ -1136,6 +1138,7 @@ pub async fn dependency_list(
&user_option,
&pool,
&redis,
&cdn_config.make_choice(use_alt_cdn),
)
.await?;

View File

@@ -3,6 +3,7 @@ use crate::auth::checks::{filter_visible_versions, is_visible_version};
use crate::auth::{filter_visible_projects, get_user_from_headers};
use crate::database::ReadOnlyPgPool;
use crate::database::redis::RedisPool;
use crate::file_hosting::{CdnConfig, UseAltCdn};
use crate::models::ids::VersionId;
use crate::models::pats::Scopes;
use crate::models::projects::VersionType;
@@ -41,6 +42,8 @@ pub async fn get_version_from_hash(
redis: web::Data<RedisPool>,
hash_query: web::Query<HashQuery>,
session_queue: web::Data<AuthQueue>,
cdn_config: web::Data<CdnConfig>,
UseAltCdn(use_alt_cdn): UseAltCdn,
) -> Result<HttpResponse, ApiError> {
let user_option = get_user_from_headers(
&req,
@@ -75,8 +78,10 @@ pub async fn get_version_from_hash(
return Err(ApiError::NotFound);
}
Ok(HttpResponse::Ok()
.json(models::projects::Version::from(version)))
Ok(HttpResponse::Ok().json(models::projects::Version::from(
version,
&cdn_config.make_choice(use_alt_cdn),
)))
} else {
Err(ApiError::NotFound)
}
@@ -127,6 +132,8 @@ pub async fn get_update_from_hash(
hash_query: web::Query<HashQuery>,
update_data: web::Json<UpdateData>,
session_queue: web::Data<AuthQueue>,
cdn_config: web::Data<CdnConfig>,
UseAltCdn(use_alt_cdn): UseAltCdn,
) -> Result<HttpResponse, ApiError> {
let user_option = get_user_from_headers(
&req,
@@ -195,9 +202,12 @@ pub async fn get_update_from_hash(
return Err(ApiError::NotFound);
}
return Ok(
HttpResponse::Ok().json(models::projects::Version::from(first))
);
return Ok(HttpResponse::Ok().json(
models::projects::Version::from(
first,
&cdn_config.make_choice(use_alt_cdn),
),
));
}
}
Err(ApiError::NotFound)
@@ -216,6 +226,8 @@ pub async fn get_versions_from_hashes(
redis: web::Data<RedisPool>,
file_data: web::Json<FileHashes>,
session_queue: web::Data<AuthQueue>,
cdn_config: web::Data<CdnConfig>,
UseAltCdn(use_alt_cdn): UseAltCdn,
) -> Result<HttpResponse, ApiError> {
let user_option = get_user_from_headers(
&req,
@@ -248,6 +260,7 @@ pub async fn get_versions_from_hashes(
&user_option,
&pool,
&redis,
&cdn_config.make_choice(use_alt_cdn),
)
.await?;
@@ -335,6 +348,8 @@ pub async fn update_files(
pool: web::Data<ReadOnlyPgPool>,
redis: web::Data<RedisPool>,
update_data: web::Json<ManyUpdateData>,
cdn_config: web::Data<CdnConfig>,
UseAltCdn(use_alt_cdn): UseAltCdn,
) -> Result<HttpResponse, ApiError> {
let algorithm = update_data
.algorithm
@@ -392,10 +407,11 @@ pub async fn update_files(
.find(|x| x.inner.project_id == file.project_id)
&& let Some(hash) = file.hashes.get(&algorithm)
{
response.insert(
hash.clone(),
models::projects::Version::from(version.clone()),
let version = models::projects::Version::from(
version.clone(),
&cdn_config.make_choice(use_alt_cdn),
);
response.insert(hash.clone(), version);
}
}
@@ -422,6 +438,8 @@ pub async fn update_individual_files(
redis: web::Data<RedisPool>,
update_data: web::Json<ManyFileUpdateData>,
session_queue: web::Data<AuthQueue>,
cdn_config: web::Data<CdnConfig>,
UseAltCdn(use_alt_cdn): UseAltCdn,
) -> Result<HttpResponse, ApiError> {
let user_option = get_user_from_headers(
&req,
@@ -524,10 +542,11 @@ pub async fn update_individual_files(
)
.await?
{
response.insert(
hash.clone(),
models::projects::Version::from(version.clone()),
let version = models::projects::Version::from(
version.clone(),
&cdn_config.make_choice(use_alt_cdn),
);
response.insert(hash.clone(), version);
}
}
}
@@ -674,6 +693,7 @@ pub async fn download_version(
redis: web::Data<RedisPool>,
hash_query: web::Query<HashQuery>,
session_queue: web::Data<AuthQueue>,
UseAltCdn(use_alt_cdn): UseAltCdn,
) -> Result<HttpResponse, ApiError> {
let user_option = get_user_from_headers(
&req,
@@ -704,6 +724,15 @@ pub async fn download_version(
database::models::DBVersion::get(file.version_id, &**pool, &redis)
.await?;
let url = if use_alt_cdn {
let cdn_url = dotenvy::var("CDN_URL").unwrap();
let cdn_alt_url = dotenvy::var("CDN_ALT_URL").unwrap();
file.url.replace(&cdn_url, &cdn_alt_url)
} else {
file.url.clone()
};
if let Some(version) = version {
if !is_visible_version(&version.inner, &user_option, &pool, &redis)
.await?
@@ -712,8 +741,8 @@ pub async fn download_version(
}
Ok(HttpResponse::TemporaryRedirect()
.append_header(("Location", &*file.url))
.json(DownloadRedirect { url: file.url }))
.append_header(("Location", &*url))
.json(DownloadRedirect { url }))
} else {
Err(ApiError::NotFound)
}

View File

@@ -14,6 +14,7 @@ use crate::database::models::version_item::{
};
use crate::database::models::{DBOrganization, image_item};
use crate::database::redis::RedisPool;
use crate::file_hosting::{CdnConfig, UseAltCdn};
use crate::models;
use crate::models::ids::VersionId;
use crate::models::images::ImageContext;
@@ -61,16 +62,30 @@ pub async fn version_project_get(
pool: web::Data<PgPool>,
redis: web::Data<RedisPool>,
session_queue: web::Data<AuthQueue>,
cdn_config: web::Data<CdnConfig>,
use_alt_cdn: UseAltCdn,
) -> Result<HttpResponse, ApiError> {
let info = info.into_inner();
version_project_get_helper(req, info, pool, redis, session_queue).await
version_project_get_helper(
req,
info,
pool,
redis,
session_queue,
cdn_config,
use_alt_cdn,
)
.await
}
pub async fn version_project_get_helper(
req: HttpRequest,
id: (String, String),
pool: web::Data<PgPool>,
redis: web::Data<RedisPool>,
session_queue: web::Data<AuthQueue>,
cdn_config: web::Data<CdnConfig>,
UseAltCdn(use_alt_cdn): UseAltCdn,
) -> Result<HttpResponse, ApiError> {
let result =
database::models::DBProject::get(&id.0, &**pool, &redis).await?;
@@ -110,8 +125,12 @@ pub async fn version_project_get_helper(
&& is_visible_version(&version.inner, &user_option, &pool, &redis)
.await?
{
return Ok(HttpResponse::Ok()
.json(models::projects::Version::from(version)));
return Ok(HttpResponse::Ok().json(
models::projects::Version::from(
version,
&cdn_config.make_choice(use_alt_cdn),
),
));
}
}
@@ -129,6 +148,8 @@ pub async fn versions_get(
pool: web::Data<PgPool>,
redis: web::Data<RedisPool>,
session_queue: web::Data<AuthQueue>,
cdn_config: web::Data<CdnConfig>,
UseAltCdn(use_alt_cdn): UseAltCdn,
) -> Result<HttpResponse, ApiError> {
let version_ids =
serde_json::from_str::<Vec<models::ids::VersionId>>(&ids.ids)?
@@ -150,9 +171,14 @@ pub async fn versions_get(
.map(|x| x.1)
.ok();
let versions =
filter_visible_versions(versions_data, &user_option, &pool, &redis)
.await?;
let versions = filter_visible_versions(
versions_data,
&user_option,
&pool,
&redis,
&cdn_config.make_choice(use_alt_cdn),
)
.await?;
Ok(HttpResponse::Ok().json(versions))
}
@@ -163,9 +189,20 @@ pub async fn version_get(
pool: web::Data<PgPool>,
redis: web::Data<RedisPool>,
session_queue: web::Data<AuthQueue>,
cdn_config: web::Data<CdnConfig>,
use_alt_cdn: UseAltCdn,
) -> Result<HttpResponse, ApiError> {
let id = info.into_inner().0;
version_get_helper(req, id, pool, redis, session_queue).await
version_get_helper(
req,
id,
pool,
redis,
session_queue,
cdn_config,
use_alt_cdn,
)
.await
}
pub async fn version_get_helper(
@@ -174,6 +211,8 @@ pub async fn version_get_helper(
pool: web::Data<PgPool>,
redis: web::Data<RedisPool>,
session_queue: web::Data<AuthQueue>,
cdn_config: web::Data<CdnConfig>,
UseAltCdn(use_alt_cdn): UseAltCdn,
) -> Result<HttpResponse, ApiError> {
let version_data =
database::models::DBVersion::get(id.into(), &**pool, &redis).await?;
@@ -192,9 +231,10 @@ pub async fn version_get_helper(
if let Some(data) = version_data
&& is_visible_version(&data.inner, &user_option, &pool, &redis).await?
{
return Ok(
HttpResponse::Ok().json(models::projects::Version::from(data))
);
return Ok(HttpResponse::Ok().json(models::projects::Version::from(
data,
&cdn_config.make_choice(use_alt_cdn),
)));
}
Err(ApiError::NotFound)
@@ -724,6 +764,8 @@ pub async fn version_list(
pool: web::Data<PgPool>,
redis: web::Data<RedisPool>,
session_queue: web::Data<AuthQueue>,
cdn_config: web::Data<CdnConfig>,
UseAltCdn(use_alt_cdn): UseAltCdn,
) -> Result<HttpResponse, ApiError> {
let string = info.into_inner().0;
@@ -856,9 +898,14 @@ pub async fn version_list(
});
response.dedup_by(|a, b| a.inner.id == b.inner.id);
let response =
filter_visible_versions(response, &user_option, &pool, &redis)
.await?;
let response = filter_visible_versions(
response,
&user_option,
&pool,
&redis,
&cdn_config.make_choice(use_alt_cdn),
)
.await?;
Ok(HttpResponse::Ok().json(response))
} else {