WIP create a video type inference method and a mapping into a content type.
This commit is contained in:
@@ -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",
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
pub mod service;
|
||||
pub mod service;
|
||||
//pub mod api;
|
||||
Reference in New Issue
Block a user