update code level
This commit is contained in:
@@ -3,6 +3,30 @@ use std::fs;
|
||||
use image::{GenericImageView, ImageFormat};
|
||||
use uuid::Uuid;
|
||||
use crate::config::StorageConfig;
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum ImageProcessorError {
|
||||
#[error("IO error: {0}")]
|
||||
Io(#[from] std::io::Error),
|
||||
#[error("Image processing error: {0}")]
|
||||
Image(#[from] image::ImageError),
|
||||
#[error("Invalid image data")]
|
||||
InvalidData,
|
||||
}
|
||||
|
||||
// 手动实现 From<reqwest::Error> 以支持 ? 操作符
|
||||
impl From<reqwest::Error> for ImageProcessorError {
|
||||
fn from(err: reqwest::Error) -> Self {
|
||||
ImageProcessorError::Io(std::io::Error::new(std::io::ErrorKind::Other, err.to_string()))
|
||||
}
|
||||
}
|
||||
|
||||
impl ImageProcessorError {
|
||||
pub fn is_send(&self) -> bool {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ImageProcessor {
|
||||
pub config: StorageConfig,
|
||||
@@ -17,7 +41,7 @@ impl ImageProcessor {
|
||||
Self { config }
|
||||
}
|
||||
|
||||
pub async fn save_image(&self, image_data: &[u8], filename: &str) -> Result<ImageInfo, Box<dyn std::error::Error>> {
|
||||
pub async fn save_image(&self, image_data: &[u8], filename: &str) -> Result<ImageInfo, ImageProcessorError> {
|
||||
let image_id = Uuid::new_v4();
|
||||
let file_extension = Path::new(filename)
|
||||
.extension()
|
||||
@@ -56,7 +80,7 @@ impl ImageProcessor {
|
||||
output_path: &Path,
|
||||
_max_width: u32,
|
||||
_max_height: u32,
|
||||
) -> Result<(), Box<dyn std::error::Error>> {
|
||||
) -> Result<(), ImageProcessorError> {
|
||||
// 使用统一的缩略图尺寸(400x300,保持宽高比)
|
||||
let thumbnail = img.thumbnail(400, 300);
|
||||
|
||||
@@ -81,7 +105,7 @@ impl ImageProcessor {
|
||||
}
|
||||
|
||||
/// 快速生成缩略图(低质量但快速)
|
||||
pub async fn generate_fast_thumbnail(&self, image_data: &[u8], _filename: &str) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
|
||||
pub async fn generate_fast_thumbnail(&self, image_data: &[u8], _filename: &str) -> Result<Vec<u8>, ImageProcessorError> {
|
||||
// 使用快速解码和缩放
|
||||
let img = image::load_from_memory(image_data)?;
|
||||
|
||||
@@ -130,7 +154,7 @@ impl ImageProcessor {
|
||||
&self,
|
||||
url: &str,
|
||||
filename: &str,
|
||||
) -> Result<ImageInfo, Box<dyn std::error::Error>> {
|
||||
) -> Result<ImageInfo, ImageProcessorError> {
|
||||
let response = reqwest::get(url).await?;
|
||||
let image_data = response.bytes().await?;
|
||||
|
||||
@@ -142,7 +166,7 @@ impl ImageProcessor {
|
||||
&self,
|
||||
image_data: &[u8],
|
||||
thumbnail_filename: &str,
|
||||
) -> Result<(), Box<dyn std::error::Error>> {
|
||||
) -> Result<(), ImageProcessorError> {
|
||||
// 使用统一的缩略图生成方法
|
||||
let thumbnail_data = self.generate_thumbnail_from_memory(image_data, thumbnail_filename).await?;
|
||||
|
||||
@@ -161,7 +185,7 @@ impl ImageProcessor {
|
||||
&self,
|
||||
image_data: &[u8],
|
||||
_thumbnail_filename: &str,
|
||||
) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
|
||||
) -> Result<Vec<u8>, ImageProcessorError> {
|
||||
// 加载图片
|
||||
let img = image::load_from_memory(image_data)?;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user