70 lines
2.1 KiB
Rust
70 lines
2.1 KiB
Rust
use actix_web::error::ErrorInternalServerError;
|
||
use once_cell::sync::Lazy;
|
||
use redis::aio::MultiplexedConnection;
|
||
use redis::AsyncCommands;
|
||
use std::collections::HashMap;
|
||
|
||
pub static MIME_TYPES: Lazy<HashMap<&'static str, &'static str>> = Lazy::new(|| {
|
||
let mut m = HashMap::new();
|
||
// Изображения
|
||
m.insert("jpg", "image/jpeg");
|
||
m.insert("jpeg", "image/jpeg");
|
||
m.insert("jfif", "image/jpeg");
|
||
m.insert("png", "image/png");
|
||
m.insert("webp", "image/webp");
|
||
m.insert("gif", "image/gif");
|
||
m.insert("heic", "image/heic");
|
||
m.insert("heif", "image/heic");
|
||
m.insert("tif", "image/tiff");
|
||
m.insert("tiff", "image/tiff");
|
||
// Аудио
|
||
m.insert("3gp", "audio/3gpp");
|
||
m.insert("mp3", "audio/mpeg");
|
||
m.insert("wav", "audio/x-wav");
|
||
m.insert("aif", "audio/x-aiff");
|
||
m.insert("aiff", "audio/x-aiff");
|
||
m.insert("m4a", "audio/m4a");
|
||
m.insert("m4b", "audio/m4b");
|
||
m.insert("m4p", "audio/m4p");
|
||
m.insert("ogg", "audio/ogg");
|
||
m.insert("aac", "audio/aac");
|
||
m.insert("flac", "audio/flac");
|
||
m
|
||
});
|
||
|
||
pub fn get_mime_type(extension: &str) -> Option<&'static str> {
|
||
MIME_TYPES.get(extension).copied()
|
||
}
|
||
|
||
/// Ищет файл в Redis по шаблону имени
|
||
pub async fn find_file_by_pattern(
|
||
redis: &mut MultiplexedConnection,
|
||
pattern: &str,
|
||
) -> Result<Option<String>, actix_web::Error> {
|
||
let pattern_key = format!("files:*{}*", pattern);
|
||
let files: Vec<String> = redis
|
||
.keys(&pattern_key)
|
||
.await
|
||
.map_err(|_| ErrorInternalServerError("Failed to search files in Redis"))?;
|
||
|
||
if files.is_empty() {
|
||
Ok(None)
|
||
} else {
|
||
Ok(Some(files[0].clone()))
|
||
}
|
||
}
|
||
|
||
/// Сохраняет файл в Redis с его MIME-типом
|
||
pub async fn store_file_info(
|
||
redis: &mut MultiplexedConnection,
|
||
filename: &str,
|
||
mime_type: &str,
|
||
) -> Result<(), actix_web::Error> {
|
||
let file_key = format!("files:{}", filename);
|
||
redis
|
||
.set::<_, _, ()>(&file_key, mime_type)
|
||
.await
|
||
.map_err(|_| ErrorInternalServerError("Failed to store file info in Redis"))?;
|
||
Ok(())
|
||
}
|