From 8cc4f0bcbc3d410a32c1c08129cf2ce0efc6d53d Mon Sep 17 00:00:00 2001 From: Vomitblood Date: Thu, 8 Aug 2024 00:35:49 +0800 Subject: [PATCH] updated github actions workflow --- .github/workflows/build-validation.yml | 3 ++ src-tauri/src/bin/testing.rs | 66 +++++++++++++------------- 2 files changed, 35 insertions(+), 34 deletions(-) diff --git a/.github/workflows/build-validation.yml b/.github/workflows/build-validation.yml index 9ebc3e2..b8f7515 100644 --- a/.github/workflows/build-validation.yml +++ b/.github/workflows/build-validation.yml @@ -32,6 +32,9 @@ jobs: - name: Install dependencies for Tauri run: apt install -y cargo libwebkit2gtk-4.0-dev build-essential curl wget file libssl-dev libgtk-3-dev libayatana-appindicator3-dev librsvg2-dev + - name: Update Rust + run: rustup update + - name: Install mise run: | install -dm 755 /etc/apt/keyrings diff --git a/src-tauri/src/bin/testing.rs b/src-tauri/src/bin/testing.rs index f6aea5e..4bc045d 100644 --- a/src-tauri/src/bin/testing.rs +++ b/src-tauri/src/bin/testing.rs @@ -23,36 +23,36 @@ enum ImageType { // // determine the file format // } -// fn determine_image_type(file_path: &std::path::Path) -> Result { -// // open the file -// let mut file = -// std::fs::File::open(file_path).map_err(|e| format!("Failed to open file: {e}"))?; +/// Determines the image type of the file at the given path. +/// Returns `Ok(ImageType)` on success, or an `Err(String)` if there is an error. +fn determine_image_type(file_path: &std::path::Path) -> Result { + // Open the file + let mut file = + std::fs::File::open(file_path).map_err(|e| format!("Failed to open file: {e}"))?; -// // read the first few bytes to determine the format -// let mut buffer = [0; 12]; -// file.read_exact(&mut buffer) -// .map_err(|e| format!("Failed to read file: {e}"))?; + // Read the first few bytes to determine the format + let mut buffer = [0; 12]; + std::io::Read::read_exact(&mut file, &mut buffer) + .map_err(|e| format!("Failed to read file: {e}"))?; -// // check statis formats -// if let Some(format) = image::guess_format(&buffer).ok() { -// match format { -// image::ImageFormat::Jpeg => return Ok(ImageType::Jpeg), -// image::ImageFormat::Png => return Ok(ImageType::Png), -// image::ImageFormat::Gif => return Ok(ImageType::Gif), -// image::ImageFormat::WebP => { -// // additional check if is animated -// if is_animated_webp(file_path) { -// return Ok(ImageType::AnimatedWebP); -// } else { -// return Ok(ImageType::WebP); -// } -// } -// _ => return Ok(ImageType::Unsupported), -// } -// } else { -// return Ok(ImageType::Unsupported); -// } -// } + // Check static formats using the image crate + match image::guess_format(&buffer) { + Ok(image::ImageFormat::Jpeg) => Ok(ImageType::Jpeg), + Ok(image::ImageFormat::Png) => Ok(ImageType::Png), + Ok(image::ImageFormat::Gif) => Ok(ImageType::Gif), + Ok(image::ImageFormat::WebP) => { + // Additional check for animation in WebP + is_animated_webp(file_path).map(|animated| { + if animated { + ImageType::AnimatedWebP + } else { + ImageType::WebP + } + }) + } + _ => Ok(ImageType::Unsupported), + } +} /// check if the file is a valid webp image and if it is animated /// returns `Ok(true)` if it is animated, `Ok(false)` if it is not @@ -70,13 +70,11 @@ fn is_animated_webp(file_path: &std::path::Path) -> Result { .map(|anim| anim.has_animation()) .map_err(|_| "File is not a valid WebP image".to_string()) } - fn main() { // Example usage - let path = std::path::Path::new("/home/vomitblood/Downloads/Win10_22H2_English_x64v1.iso"); - match is_animated_webp(path) { - Ok(true) => println!("The WebP is animated."), - Ok(false) => println!("The WebP is not animated."), - Err(e) => println!("Error: {}", e), + let path = std::path::Path::new("path/to/your/image.webp"); + match determine_image_type(&path) { + Ok(image_type) => println!("Image type: {:?}", image_type), + Err(e) => println!("Error determining image type: {}", e), } }