From c8191d08276e0e0d462a1a145aeebbd705103a16 Mon Sep 17 00:00:00 2001 From: z0z0r4 Date: Sat, 12 Sep 2026 14:33:15 +0800 Subject: [PATCH 1/6] =?UTF-8?q?fix:=20=E4=B8=8D=E5=BA=94=E8=AF=A5=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E5=BC=82=E5=B8=B8=E7=8A=B6=E6=80=81=E7=A0=81=E7=9A=84?= =?UTF-8?q?=E6=90=9C=E7=B4=A2=E5=93=8D=E5=BA=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/services/modrinth.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/services/modrinth.rs b/src/services/modrinth.rs index ef5450d..550dfa2 100644 --- a/src/services/modrinth.rs +++ b/src/services/modrinth.rs @@ -368,6 +368,7 @@ impl ModrinthService { params.push(("index", i.to_string())); } + // 在此处要打断 4xx/5xx 错误,返回 ServiceError::ExternalServiceError,以免被缓存 let response = client .get(api_url) .query(¶ms) @@ -376,9 +377,14 @@ impl ModrinthService { .map_err(|e| ServiceError::ExternalServiceError { service: String::from("Modrinth API"), message: format!("Failed to send request: {}", e), + })? + .error_for_status() + .map_err(|e| ServiceError::ExternalServiceError { + service: String::from("Modrinth API"), + message: format!("Upstream HTTP error: {}", e), })?; - let status = response.status(); + let status: reqwest::StatusCode = response.status(); let bytes = response .bytes() .await From e97df6125645a36f1415fecc53b2c946843416cf Mon Sep 17 00:00:00 2001 From: z0z0r4 Date: Sat, 12 Sep 2026 14:38:33 +0800 Subject: [PATCH 2/6] =?UTF-8?q?fix:=20=E4=B8=8D=E5=BA=94=E8=AF=A5=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E5=BC=82=E5=B8=B8=E7=8A=B6=E6=80=81=E7=A0=81=E7=9A=84?= =?UTF-8?q?=E6=90=9C=E7=B4=A2=E5=93=8D=E5=BA=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/services/modrinth.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/services/modrinth.rs b/src/services/modrinth.rs index 550dfa2..4507086 100644 --- a/src/services/modrinth.rs +++ b/src/services/modrinth.rs @@ -383,6 +383,15 @@ impl ModrinthService { service: String::from("Modrinth API"), message: format!("Upstream HTTP error: {}", e), })?; + + // 在此处要打断 4xx/5xx 错误,返回 ServiceError::ExternalServiceError,以免被缓存 + if let Err(e) = response.error_for_status_ref() { + let error_body = response.text().await.unwrap_or_else(|_| String::from("")); + return Err(ServiceError::ExternalServiceError { + service: String::from("Modrinth API"), + message: format!("Upstream HTTP error: {}, body: {}", e, error_body), + }); + } let status: reqwest::StatusCode = response.status(); let bytes = response @@ -392,6 +401,7 @@ impl ModrinthService { service: String::from("Modrinth API"), message: format!("Failed to read response body: {}", e), })?; + let search_result = serde_json::from_slice(&bytes).map_err(|e| { ServiceError::UnexpectedError(format!( "Failed to parse JSON: {}, text: {}", From 92abedbed0a4e3e668b76966a31d82915c74bfb6 Mon Sep 17 00:00:00 2001 From: "deepsource-autofix[bot]" <62050782+deepsource-autofix[bot]@users.noreply.github.com> Date: Sat, 12 Sep 2026 06:40:23 +0000 Subject: [PATCH 3/6] style: format code with Rustfmt This commit fixes the style issues introduced in e97df61 according to the output from Rustfmt. Details: https://github.com/mcmod-info-mirror/mcim-rust-api/pull/41 --- src/services/modrinth.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/services/modrinth.rs b/src/services/modrinth.rs index 4507086..b11e1a3 100644 --- a/src/services/modrinth.rs +++ b/src/services/modrinth.rs @@ -383,10 +383,13 @@ impl ModrinthService { service: String::from("Modrinth API"), message: format!("Upstream HTTP error: {}", e), })?; - + // 在此处要打断 4xx/5xx 错误,返回 ServiceError::ExternalServiceError,以免被缓存 if let Err(e) = response.error_for_status_ref() { - let error_body = response.text().await.unwrap_or_else(|_| String::from("")); + let error_body = response + .text() + .await + .unwrap_or_else(|_| String::from("")); return Err(ServiceError::ExternalServiceError { service: String::from("Modrinth API"), message: format!("Upstream HTTP error: {}, body: {}", e, error_body), From 0f7c8c97cae46088c1824f94d44ba2560ec7cc6b Mon Sep 17 00:00:00 2001 From: z0z0r4 Date: Sat, 12 Sep 2026 14:58:13 +0800 Subject: [PATCH 4/6] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20get=5Fversion?= =?UTF-8?q?=5Ffiles=20=E7=BB=93=E6=9E=9C=E5=8F=AF=E8=83=BD=E8=A2=AB?= =?UTF-8?q?=E9=81=97=E6=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/services/modrinth.rs | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/services/modrinth.rs b/src/services/modrinth.rs index b11e1a3..d7a0a3f 100644 --- a/src/services/modrinth.rs +++ b/src/services/modrinth.rs @@ -944,14 +944,31 @@ impl ModrinthService { // 创建哈希值到版本的映射 let mut result: HashMap = HashMap::new(); + // 用 HashSet 来优化查找速度,避免 Vec contains O(n) 查找 + let requested_hashes_set: std::collections::HashSet<&str> = hashes.iter().map(|s| s.as_str()).collect(); + for version in versions { - if let Some(first_file) = version.files.first() { + // 此处导致只有是第一个文件的哈希值会被映射到版本,其他文件的哈希值不会被映射 + // if let Some(first_file) = version.files.first() { + // let hash_value = match algorithm.as_str() { + // "sha1" => &first_file.hashes.sha1, + // "sha512" => &first_file.hashes.sha512, + // _ => continue, + // }; + // result.insert(hash_value.clone(), version); + // } + + // 遍历版本的所有文件,找到匹配的哈希值 + for file in &version.files { let hash_value = match algorithm.as_str() { - "sha1" => &first_file.hashes.sha1, - "sha512" => &first_file.hashes.sha512, + "sha1" => &file.hashes.sha1, + "sha512" => &file.hashes.sha512, _ => continue, }; - result.insert(hash_value.clone(), version); + // 只有客户端查询列表中包含的 hash,才插入到结果中 + if requested_hashes_set.contains(hash_value.as_str()) { + result.insert(hash_value.clone(), version.clone()); + } } } From 800fee6d467511f52acd2edc67645982e179e898 Mon Sep 17 00:00:00 2001 From: z0z0r4 Date: Sat, 12 Sep 2026 15:14:15 +0800 Subject: [PATCH 5/6] =?UTF-8?q?fix:=20=E5=93=8D=E5=BA=94=E4=B8=BA=E7=A9=BA?= =?UTF-8?q?=E6=97=B6=E4=B8=8D=E5=BA=94=E8=AF=A5=E8=B7=B3=E8=BF=87=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E5=88=B0=20Redis=20=E9=98=9F=E5=88=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/services/curseforge.rs | 46 ++++++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 12 deletions(-) diff --git a/src/services/curseforge.rs b/src/services/curseforge.rs index a9aa728..75acb10 100644 --- a/src/services/curseforge.rs +++ b/src/services/curseforge.rs @@ -306,21 +306,11 @@ impl CurseforgeService { mods.push(doc); } - // empty 则直接返回 { "data": [] } - if mods.is_empty() { - return Err(ServiceError::NotFound { - resource: String::from("Mods"), - detail: Some(format!( - "No mods found for the provided modIds: {:?}", - mod_ids - )), - }); - } - // 检查是否有未找到的 mod_id let found_mod_ids: Vec = mods.iter().map(|m| m.id).collect(); let not_found_mod_ids: Vec = mod_ids - .into_iter() + .iter() + .copied() .filter(|id| !found_mod_ids.contains(id)) .collect(); if !not_found_mod_ids.is_empty() { @@ -333,6 +323,38 @@ impl CurseforgeService { log::trace!("All Mods have been found in the database."); } + /* + curl -i -X 'POST' \ + 'https://api.curseforge.com/v1/mods' \ + -H 'accept: application/json' \ + -H 'Content-Type: application/json' \ + -H 'x-api-key: $API_KEY' \ + -d '{ + "filterPcOnly": true, + "modIds": [ + 238222112 + ] + }' + HTTP/2 404 + content-length: 0 + date: Sat, 12 Sep 2026 07:06:03 GMT + x-cache: Error from cloudfront + via: 1.1 715cc2fe99ad4f311de46e74c166c5a2.cloudfront.net (CloudFront) + x-amz-cf-pop: LOS50-P4 + x-amz-cf-id: EGanUHlrFZiDD8YG6hWQpedqhiWQ30pKnB7DX8CBBNyTsyixGDDjNQ== + */ + // 不会返回 Body,可以任意填写 + // 但是必须返回 404,不能返回 200 + if mods.is_empty() { + return Err(ServiceError::NotFound { + resource: String::from("Mods"), + detail: Some(format!( + "No mods found for the provided modIds: {:?}", + mod_ids + )), + }); + } + let response_mods = mods.into_iter().map(|m| m.into()).collect(); Ok(ModsResponse { data: response_mods, From 52a0d7db205979c3a5861612f38e4629b1f61962 Mon Sep 17 00:00:00 2001 From: "deepsource-autofix[bot]" <62050782+deepsource-autofix[bot]@users.noreply.github.com> Date: Sat, 12 Sep 2026 07:15:08 +0000 Subject: [PATCH 6/6] style: format code with Rustfmt This commit fixes the style issues introduced in 800fee6 according to the output from Rustfmt. Details: https://github.com/mcmod-info-mirror/mcim-rust-api/pull/44 --- src/services/curseforge.rs | 4 ++-- src/services/modrinth.rs | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/services/curseforge.rs b/src/services/curseforge.rs index 75acb10..f19c613 100644 --- a/src/services/curseforge.rs +++ b/src/services/curseforge.rs @@ -323,7 +323,7 @@ impl CurseforgeService { log::trace!("All Mods have been found in the database."); } - /* + /* curl -i -X 'POST' \ 'https://api.curseforge.com/v1/mods' \ -H 'accept: application/json' \ @@ -342,7 +342,7 @@ impl CurseforgeService { via: 1.1 715cc2fe99ad4f311de46e74c166c5a2.cloudfront.net (CloudFront) x-amz-cf-pop: LOS50-P4 x-amz-cf-id: EGanUHlrFZiDD8YG6hWQpedqhiWQ30pKnB7DX8CBBNyTsyixGDDjNQ== - */ + */ // 不会返回 Body,可以任意填写 // 但是必须返回 404,不能返回 200 if mods.is_empty() { diff --git a/src/services/modrinth.rs b/src/services/modrinth.rs index d7a0a3f..608d8c9 100644 --- a/src/services/modrinth.rs +++ b/src/services/modrinth.rs @@ -945,8 +945,9 @@ impl ModrinthService { let mut result: HashMap = HashMap::new(); // 用 HashSet 来优化查找速度,避免 Vec contains O(n) 查找 - let requested_hashes_set: std::collections::HashSet<&str> = hashes.iter().map(|s| s.as_str()).collect(); - + let requested_hashes_set: std::collections::HashSet<&str> = + hashes.iter().map(|s| s.as_str()).collect(); + for version in versions { // 此处导致只有是第一个文件的哈希值会被映射到版本,其他文件的哈希值不会被映射 // if let Some(first_file) = version.files.first() {