mirror of
https://github.com/modrinth/code.git
synced 2026-07-31 13:16:38 +00:00
fix: gracefully handle shared instance icons 404ing (#6928)
* fix: icon handling * fix: cleanup impl * fix: fmt
This commit is contained in:
@@ -55,8 +55,12 @@ pub(crate) async fn create_instance(
|
||||
None
|
||||
};
|
||||
|
||||
let icon_path =
|
||||
resolve_icon_path(input.icon_path.as_deref(), state).await?;
|
||||
let icon_path = resolve_icon_path(
|
||||
input.icon_path.as_deref(),
|
||||
matches!(&input.link, InstanceLink::SharedInstance { .. }),
|
||||
state,
|
||||
)
|
||||
.await?;
|
||||
let now = Utc::now();
|
||||
let instance_id = format!("local:{}", Uuid::new_v4());
|
||||
let content_set_id = format!("content-set:{}", Uuid::new_v4());
|
||||
@@ -168,6 +172,7 @@ async fn path_available(
|
||||
|
||||
async fn resolve_icon_path(
|
||||
icon_path: Option<&str>,
|
||||
ignore_missing_remote_icon: bool,
|
||||
state: &State,
|
||||
) -> crate::Result<Option<String>> {
|
||||
let Some(icon) = icon_path else {
|
||||
@@ -175,7 +180,7 @@ async fn resolve_icon_path(
|
||||
};
|
||||
|
||||
let file = if icon.starts_with("https://") || icon.starts_with("http://") {
|
||||
let bytes = fetch::fetch(
|
||||
let bytes = match fetch::fetch(
|
||||
icon,
|
||||
None,
|
||||
None,
|
||||
@@ -183,7 +188,16 @@ async fn resolve_icon_path(
|
||||
&state.fetch_semaphore,
|
||||
&state.pool,
|
||||
)
|
||||
.await?;
|
||||
.await
|
||||
{
|
||||
Ok(bytes) => bytes,
|
||||
Err(error)
|
||||
if ignore_missing_remote_icon && is_not_found_error(&error) =>
|
||||
{
|
||||
return Ok(None);
|
||||
}
|
||||
Err(error) => return Err(error),
|
||||
};
|
||||
crate::api::instance::cache_icon(bytes, state).await?
|
||||
} else {
|
||||
crate::api::instance::cache_icon_from_path(
|
||||
@@ -196,6 +210,18 @@ async fn resolve_icon_path(
|
||||
Ok(Some(file.to_string_lossy().to_string()))
|
||||
}
|
||||
|
||||
fn is_not_found_error(error: &crate::Error) -> bool {
|
||||
match error.raw.as_ref() {
|
||||
crate::ErrorKind::FetchError(error) => {
|
||||
error.status() == Some(reqwest::StatusCode::NOT_FOUND)
|
||||
}
|
||||
crate::ErrorKind::LabrinthError(error) => {
|
||||
error.status == Some(reqwest::StatusCode::NOT_FOUND.as_u16())
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
fn content_source_kind(link: &InstanceLink) -> ContentSourceKind {
|
||||
match link {
|
||||
InstanceLink::Unmanaged => ContentSourceKind::Local,
|
||||
|
||||
Reference in New Issue
Block a user