Skip to main content

aster/media/
mod.rs

1//! 媒体处理模块
2//!
3//! 提供图片、PDF、SVG 等媒体文件的处理功能
4
5mod image;
6mod mime;
7mod pdf;
8mod svg;
9
10pub use image::*;
11pub use mime::*;
12pub use pdf::*;
13pub use svg::*;
14
15// 重新导出增强函数
16pub use image::estimate_image_dimensions;
17pub use image::read_image_file_enhanced;
18
19use std::path::Path;
20
21/// 媒体文件类型
22#[derive(Debug, Clone, Copy, PartialEq, Eq)]
23pub enum MediaType {
24    Image,
25    Pdf,
26    Svg,
27    Unknown,
28}
29
30/// 媒体读取结果
31#[derive(Debug, Clone)]
32pub enum MediaResult {
33    Image(ImageResult),
34    Pdf(PdfReadResult),
35}
36
37/// 检测文件的媒体类型
38pub fn detect_media_type(file_path: &Path) -> MediaType {
39    let ext = file_path
40        .extension()
41        .and_then(|e| e.to_str())
42        .unwrap_or("")
43        .to_lowercase();
44
45    if is_supported_image_format(&ext) {
46        return MediaType::Image;
47    }
48
49    if is_pdf_extension(&ext) {
50        return MediaType::Pdf;
51    }
52
53    if ext == "svg" {
54        return MediaType::Svg;
55    }
56
57    MediaType::Unknown
58}
59
60/// 检查文件是否为支持的媒体文件
61pub fn is_supported_media_file(file_path: &Path) -> bool {
62    detect_media_type(file_path) != MediaType::Unknown
63}
64
65/// 二进制文件黑名单
66/// 这些文件类型不应该被读取
67pub static BINARY_FILE_BLACKLIST: &[&str] = &[
68    // 音频格式
69    "mp3", "wav", "flac", "ogg", "aac", "m4a", "wma", "aiff", "opus", // 视频格式
70    "mp4", "avi", "mov", "wmv", "flv", "mkv", "webm", "m4v", "mpeg", "mpg",
71    // 压缩文件
72    "zip", "rar", "tar", "gz", "bz2", "7z", "xz", "z", "tgz", "iso", // 可执行文件
73    "exe", "dll", "so", "dylib", "app", "msi", "deb", "rpm", "bin", // 数据库文件
74    "dat", "db", "sqlite", "sqlite3", "mdb", "idx", // Office 文档(旧格式)
75    "doc", "docx", "xls", "xlsx", "ppt", "pptx", "odt", "ods", "odp", // 字体文件
76    "ttf", "otf", "woff", "woff2", "eot", // 设计文件
77    "psd", "ai", "eps", "sketch", "fig", "xd", "blend", "obj", "3ds", "max",
78    // 编译文件
79    "class", "jar", "war", "pyc", "pyo", "rlib", "swf", "fla",
80];
81
82/// 检查文件是否在黑名单中
83pub fn is_blacklisted_file(file_path: &Path) -> bool {
84    let ext = file_path
85        .extension()
86        .and_then(|e| e.to_str())
87        .unwrap_or("")
88        .to_lowercase();
89
90    BINARY_FILE_BLACKLIST.contains(&ext.as_str())
91}
92
93#[cfg(test)]
94mod tests;