Skip to content

Commit

Permalink
add support for avif
Browse files Browse the repository at this point in the history
  • Loading branch information
Larkooo committed Feb 12, 2025
1 parent 7b8380e commit 4417c10
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 13 deletions.
80 changes: 80 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ hashlink = "0.9.1"
hex = "0.4.3"
hex-literal = "0.4.1"
http = "0.2.9"
image = "0.25.2"
image = { version = "0.25.2", features = [ "avif-native" ] }
indexmap = "2.2.5"
indoc = "1.0.7"
itertools = "0.12.1"
Expand Down
20 changes: 8 additions & 12 deletions crates/torii/server/src/artifacts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@ use camino::Utf8PathBuf;
use data_url::mime::Mime;
use data_url::DataUrl;
use image::{DynamicImage, ImageFormat};
use reqwest::Client;
use serde::{Deserialize, Serialize};
use sqlx::{Pool, Sqlite};
use tokio::fs;
use tokio::fs::File;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::sync::broadcast::Receiver;
use torii_sqlite::constants::TOKENS_TABLE;
use torii_sqlite::utils::fetch_content_from_ipfs;
use torii_sqlite::utils::{fetch_content_from_http, fetch_content_from_ipfs};
use tracing::{debug, error, trace};
use warp::http::Response;
use warp::path::Tail;
Expand Down Expand Up @@ -61,7 +60,6 @@ async fn serve_static_file(
if !token_image_dir.exists() {
match fetch_and_process_image(&artifacts_dir, &token_id, pool)
.await
.context(format!("Failed to fetch and process image for token_id: {}", token_id))
{
Ok(path) => path,
Err(e) => {
Expand Down Expand Up @@ -173,19 +171,15 @@ async fn fetch_and_process_image(
.with_context(|| format!("Image field not a string for token_id: {}", token_id))?
.to_string();

println!("image_uri: {}", image_uri);

let image_type = match image_uri {
uri if uri.starts_with("http") || uri.starts_with("https") => {
debug!(image_uri = %uri, "Fetching image from http/https URL");
// Fetch image from HTTP/HTTPS URL
let client = Client::new();
let response = client
.get(uri)
.send()
.await
.context("Failed to fetch image from URL")?
.bytes()
let response = fetch_content_from_http(&uri)
.await
.context("Failed to read image bytes from response")?;
.context("Failed to fetch image from URL")?;

// svg files typically start with <svg or <?xml
if response.starts_with(b"<svg") || response.starts_with(b"<?xml") {
Expand Down Expand Up @@ -218,7 +212,7 @@ async fn fetch_and_process_image(
)
})?;
ErcImageType::DynamicImage((
image::load_from_memory(&response)
image::load_from_memory_with_format(&response, format)
.context("Failed to load image from bytes")?,
format,
))
Expand Down Expand Up @@ -250,6 +244,8 @@ async fn fetch_and_process_image(
}
};

println!("image_type: {:?}", image_type);

// Extract contract_address and token_id from token_id
let parts: Vec<&str> = token_id.split(':').collect();
if parts.len() != 2 {
Expand Down

0 comments on commit 4417c10

Please sign in to comment.