Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions apps/app/src/api/thumbnails.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,10 @@ fn create_thumbnail(
return Ok(target);
}
std::fs::create_dir_all(cache)?;
let mut reader = image::ImageReader::open(path)?.with_guessed_format()?;
let mut limits = image::Limits::default();
limits.max_alloc = Some(256 * 1024 * 1024);
limits.max_image_width = Some(16384);
limits.max_image_height = Some(16384);
reader.limits(limits);
let image = reader.decode().map_err(std::io::Error::other)?;
let image = image::ImageReader::open(path)?
.with_guessed_format()?
.decode()
.map_err(std::io::Error::other)?;
let thumbnail = image.thumbnail(size, size);
drop(image);
let temporary = target.with_extension("tmp");
Expand Down
25 changes: 18 additions & 7 deletions packages/app-lib/src/api/instance/icon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ use std::path::{Path, PathBuf};

const INSTANCE_ICON_MAX_BYTES: usize = 4 * 1024 * 1024;
const INSTANCE_ICON_MAX_DIMENSION: u32 = 512;
const INSTANCE_ICON_MAX_SOURCE_DIMENSION: u32 = 8_192;
const INSTANCE_ICON_MAX_DECODE_BYTES: u64 = 64 * 1024 * 1024;
const INSTANCE_ICON_MAX_DECODE_BYTES: u64 = 256 * 1024 * 1024;
static INSTANCE_ICON_DECODES: tokio::sync::Semaphore =
tokio::sync::Semaphore::const_new(2);
const GENERATED_ICON_SIZE: u32 = 256;
const MAX_SYMBOL_BYTES: usize = 4 * 1024 * 1024;
const MAX_SYMBOL_DIMENSION: u32 = 4096;
Expand Down Expand Up @@ -263,7 +264,9 @@ pub(crate) async fn cache_icon(
bytes: Bytes,
state: &State,
) -> crate::Result<PathBuf> {
let permit = INSTANCE_ICON_DECODES.acquire().await?;
let bytes = tokio::task::spawn_blocking(move || {
let _permit = permit;
if looks_like_svg(&bytes) {
return Err(svg_not_supported_error());
}
Expand All @@ -280,7 +283,9 @@ pub(crate) async fn cache_icon_from_path(
state: &State,
) -> crate::Result<PathBuf> {
let icon_path = icon_path.to_path_buf();
let permit = INSTANCE_ICON_DECODES.acquire().await?;
let bytes = tokio::task::spawn_blocking(move || {
let _permit = permit;
let file = StdFile::open(&icon_path).context(ErrorKind::InputError(
format!("could not open instance icon {}", icon_path.display()),
))?;
Expand Down Expand Up @@ -432,8 +437,6 @@ where
"could not identify instance icon format".to_string(),
))?;
let mut limits = image::Limits::default();
limits.max_image_width = Some(INSTANCE_ICON_MAX_SOURCE_DIMENSION);
limits.max_image_height = Some(INSTANCE_ICON_MAX_SOURCE_DIMENSION);
limits.max_alloc = Some(INSTANCE_ICON_MAX_DECODE_BYTES);
reader.limits(limits);

Expand All @@ -446,7 +449,7 @@ where
image.resize(
INSTANCE_ICON_MAX_DIMENSION,
INSTANCE_ICON_MAX_DIMENSION,
image::imageops::FilterType::Lanczos3,
FilterType::Lanczos3,
)
} else {
image
Expand Down Expand Up @@ -479,8 +482,16 @@ fn inspect_legacy_icon(icon_path: &Path) -> crate::Result<LegacyIconAction> {
return Ok(LegacyIconAction::Remove);
}

if metadata.len() < INSTANCE_ICON_MAX_BYTES as u64
&& image::guess_format(bytes).ok() == Some(image::ImageFormat::Png)
let is_png =
image::guess_format(bytes).ok() == Some(image::ImageFormat::Png);
if is_png
&& metadata.len() < INSTANCE_ICON_MAX_BYTES as u64
&& ImageReader::with_format(reader, ImageFormat::Png)
.into_dimensions()
.is_ok_and(|(width, height)| {
width <= INSTANCE_ICON_MAX_DIMENSION
&& height <= INSTANCE_ICON_MAX_DIMENSION
})
{
return Ok(LegacyIconAction::Keep);
}
Expand Down
46 changes: 24 additions & 22 deletions packages/app-lib/src/state/instances/commands/create_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,8 @@ pub(crate) async fn create_instance(
None
};

let icon_path = resolve_icon_path(
input.icon_path.as_deref(),
matches!(&input.link, InstanceLink::SharedInstance { .. }),
state,
)
.await?;
let icon_path =
resolve_icon_path(input.icon_path.as_deref(), true, state).await?;
let now = Utc::now();
let instance_id = format!("local:{}", Uuid::new_v4());
let content_set_id = format!("content-set:{}", Uuid::new_v4());
Expand Down Expand Up @@ -200,32 +196,38 @@ async fn path_available(

pub(crate) async fn resolve_icon_path(
icon_path: Option<&str>,
ignore_missing_remote_icon: bool,
ignore_remote_icon_errors: bool,
state: &State,
) -> crate::Result<Option<String>> {
let Some(icon) = icon_path else {
return Ok(None);
};

let file = if icon.starts_with("https://") || icon.starts_with("http://") {
let bytes = match fetch::fetch(
icon,
None,
None,
None,
&state.fetch_semaphore,
&state.pool,
)
.await
{
Ok(bytes) => bytes,
Err(error) if ignore_missing_remote_icon => {
tracing::warn!("Error while getting instance icon: {error}");
let cached = async {
let bytes = fetch::fetch(
icon,
None,
None,
None,
&state.fetch_semaphore,
&state.pool,
)
.await?;
crate::api::instance::cache_icon(bytes, state).await
}
.await;
match cached {
Ok(file) => file,
Err(error) if ignore_remote_icon_errors => {
tracing::warn!(
icon,
"Error while getting instance icon: {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(
&state.directories.caches_dir().join(icon),
Expand Down
Loading