65
src/lookup.rs
Normal file
65
src/lookup.rs
Normal file
@@ -0,0 +1,65 @@
|
||||
use std::collections::HashMap;
|
||||
use actix_web::error::ErrorInternalServerError;
|
||||
use once_cell::sync::Lazy;
|
||||
use redis::aio::MultiplexedConnection;
|
||||
use redis::AsyncCommands;
|
||||
|
||||
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("mp3", "audio/mpeg");
|
||||
m.insert("wav", "audio/x-wav");
|
||||
m.insert("ogg", "audio/ogg");
|
||||
m.insert("aac", "audio/aac");
|
||||
m.insert("m4a", "audio/m4a");
|
||||
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(())
|
||||
}
|
||||
Reference in New Issue
Block a user