mirror of
https://github.com/modrinth/code.git
synced 2026-08-28 18:45:15 +00:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dd8360febd |
@@ -27,7 +27,9 @@ pub(crate) use self::icon::{
|
||||
cache_icon, cache_icon_from_path, migrate_legacy_icons,
|
||||
};
|
||||
pub use self::install::get_optimal_jre_key;
|
||||
pub(crate) use self::lifecycle::create;
|
||||
pub(crate) use self::lifecycle::{
|
||||
create, create_with_missing_icon_fallback,
|
||||
};
|
||||
pub use self::lifecycle::{edit, remove};
|
||||
pub use self::paths::{get_full_path, get_mod_full_path};
|
||||
pub use self::projects::{
|
||||
|
||||
@@ -15,6 +15,49 @@ pub(crate) async fn create(
|
||||
loader_version: Option<String>,
|
||||
icon_path: Option<String>,
|
||||
link: InstanceLink,
|
||||
) -> crate::Result<InstanceMetadata> {
|
||||
create_inner(
|
||||
name,
|
||||
game_version,
|
||||
modloader,
|
||||
loader_version,
|
||||
icon_path,
|
||||
link,
|
||||
false,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub(crate) async fn create_with_missing_icon_fallback(
|
||||
name: String,
|
||||
game_version: String,
|
||||
modloader: ModLoader,
|
||||
loader_version: Option<String>,
|
||||
icon_path: Option<String>,
|
||||
link: InstanceLink,
|
||||
) -> crate::Result<InstanceMetadata> {
|
||||
create_inner(
|
||||
name,
|
||||
game_version,
|
||||
modloader,
|
||||
loader_version,
|
||||
icon_path,
|
||||
link,
|
||||
true,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
async fn create_inner(
|
||||
name: String,
|
||||
game_version: String,
|
||||
modloader: ModLoader,
|
||||
loader_version: Option<String>,
|
||||
icon_path: Option<String>,
|
||||
link: InstanceLink,
|
||||
ignore_missing_remote_icon: bool,
|
||||
) -> crate::Result<InstanceMetadata> {
|
||||
let state = State::get().await?;
|
||||
let instance = crate::state::create_instance(
|
||||
@@ -27,6 +70,7 @@ pub(crate) async fn create(
|
||||
icon_path,
|
||||
link,
|
||||
},
|
||||
ignore_missing_remote_icon,
|
||||
&state,
|
||||
)
|
||||
.await?;
|
||||
|
||||
@@ -455,7 +455,8 @@ async fn prepare_initial_instance(
|
||||
data.instance_icon_url.clone(),
|
||||
)
|
||||
};
|
||||
let metadata = crate::api::instance::create(
|
||||
let metadata =
|
||||
crate::api::instance::create_with_missing_icon_fallback(
|
||||
data.name.clone(),
|
||||
game_version,
|
||||
loader,
|
||||
|
||||
@@ -28,6 +28,7 @@ pub struct CreateInstance {
|
||||
|
||||
pub(crate) async fn create_instance(
|
||||
input: CreateInstance,
|
||||
ignore_missing_remote_icon: bool,
|
||||
state: &State,
|
||||
) -> crate::Result<Instance> {
|
||||
trace!("Creating new instance. {}", input.name);
|
||||
@@ -55,8 +56,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(),
|
||||
ignore_missing_remote_icon,
|
||||
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 +173,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 +181,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 +189,17 @@ 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 +212,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