WIP create a video type inference method and a mapping into a content type.

This commit is contained in:
Florian THIERRY
2026-07-13 23:26:51 +02:00
parent 03dbbb9ac9
commit 850dbca6cf
12 changed files with 354 additions and 28 deletions
+3
View File
@@ -14,6 +14,9 @@ path = "tests/user_service_test.rs"
[dependencies]
lumiere-domain = { path = "../domain" }
axum = "0.8.9"
tokio = "1.52.3"
mime = "0.3.17"
bcrypt = "0.19.0"
chrono = { version = "0.4.44", features = ["serde"] }
log = "0.4.29"
+42 -4
View File
@@ -67,12 +67,50 @@ fn sanitize_and_resolve_path(video_path: &str) -> Result<std::path::PathBuf, Sta
Ok(full_path)
}
/// Determine Content-Type based on file extension
/// Determine Content-Type by reading file headers (magic numbers)
fn get_content_type(path: &Path) -> &'static str {
use std::io::Read;
// Read first few bytes to detect magic numbers
let mut file = match File::open(path) {
Ok(f) => f,
Err(_) => return "application/octet-stream",
};
let mut buffer = [0u8; 16]; // Read enough bytes to detect common formats
let bytes_read = match file.read(&mut buffer) {
Ok(n) if n > 0 => n,
_ => return "application/octet-stream",
};
// Detect AVI format (RIFF header with AVI)
if bytes_read >= 12 && buffer[0..4] == [82, 73, 70, 70] && buffer[8..12] == [65, 86, 73, 32] {
return "video/x-msvideo";
}
// Detect MKV format (EB ML header)
if bytes_read >= 4 && buffer[0..4] == [0x1A, 0x45, 0xDF, 0xA3] {
return "video/x-matroska"; // Proper MIME type for MKV
}
// Detect MP4/MOV format (ftyp box)
if bytes_read >= 8 && buffer[4..8] == [102, 116, 121, 112] {
return "video/mp4";
}
// Detect WebM format (EB ML header like MKV but with different signature)
if bytes_read >= 8 && buffer[0..4] == [0x1A, 0x45, 0xDF, 0xA3] {
// Check for WebM signature at offset 4
if bytes_read >= 12 && buffer[4..8] == [119, 101, 98, 109] {
return "video/webm";
}
}
// Fallback to extension-based detection for unknown formats
match path.extension().and_then(|ext| ext.to_str()) {
Some("avi") => "video/x-msvideo",
Some("mkv") | Some("mp4") | Some("mov") | Some("webm") => "video/mp4", // Generic video type
_ => "application/octet-stream", // Fallback for unknown types
Some("mp4") | Some("mov") => "video/mp4",
Some("webm") => "video/webm",
_ => "application/octet-stream",
}
}
+2 -1
View File
@@ -1 +1,2 @@
pub mod service;
pub mod service;
//pub mod api;