Project Colors (#512)

* Inital tests

* Finish project colors

* Run fmt + clippy + prepare

* Fix dp+rp fmting
This commit is contained in:
Geometrically
2022-12-29 17:20:50 -07:00
committed by GitHub
parent 60bb6f105d
commit 5bb188a822
16 changed files with 1422 additions and 1062 deletions

View File

@@ -2,12 +2,9 @@ pub fn get_image_content_type(extension: &str) -> Option<&'static str> {
match extension {
"bmp" => Some("image/bmp"),
"gif" => Some("image/gif"),
"jpeg" | "jpg" | "jpe" => Some("image/jpeg"),
"jpeg" | "jpg" => Some("image/jpeg"),
"png" => Some("image/png"),
"svg" | "svgz" => Some("image/svg+xml"),
"webp" => Some("image/webp"),
"rgb" => Some("image/x-rgb"),
"mp4" => Some("video/mp4"),
_ => None,
}
}

19
src/util/img.rs Normal file
View File

@@ -0,0 +1,19 @@
use color_thief::ColorFormat;
use image::imageops::FilterType;
use image::{EncodableLayout, ImageError};
pub fn get_color_from_img(data: &[u8]) -> Result<Option<u32>, ImageError> {
let image =
image::load_from_memory(data)?.resize(256, 256, FilterType::Nearest);
let color = color_thief::get_palette(
image.to_rgb8().as_bytes(),
ColorFormat::Rgb,
10,
2,
)
.ok()
.and_then(|x| x.get(0).copied())
.map(|x| (x.r as u32) << 16 | (x.g as u32) << 8 | (x.b as u32));
Ok(color)
}

View File

@@ -2,6 +2,7 @@ pub mod auth;
pub mod env;
pub mod ext;
pub mod guards;
pub mod img;
pub mod routes;
pub mod validate;
pub mod webhook;

View File

@@ -157,7 +157,13 @@ pub async fn send_discord_webhook(
_ => 1049805243866681424,
};
let mut x = loader.clone();
let mut x = if loader == "datapack" {
"Data Pack"
} else {
loader
}
.to_string();
formatted_loaders.push_str(&format!(
"<:{loader}:{emoji_id}> {}{x}\n",
x.remove(0).to_uppercase()
@@ -190,6 +196,13 @@ pub async fn send_discord_webhook(
project_type = "datapack".to_string();
}
let mut display_project_type = match &*project_type {
"datapack" => "data pack",
"resourcepack" => "resource pack",
_ => &*project_type,
}
.to_string();
let embed = DiscordEmbed {
author: Some(DiscordEmbedAuthor {
name: project.username.clone(),
@@ -224,8 +237,8 @@ pub async fn send_discord_webhook(
.map(|x| DiscordEmbedImage { url: Some(x) }),
footer: Some(DiscordEmbedFooter {
text: format!(
"{}{project_type} on Modrinth",
project_type.remove(0).to_uppercase()
"{}{display_project_type} on Modrinth",
display_project_type.remove(0).to_uppercase()
),
icon_url: Some(
"https://cdn-raw.modrinth.com/modrinth-new.png".to_string(),