WIP create a video type inference method and a mapping into a content type.
This commit is contained in:
@@ -2,3 +2,4 @@ pub mod common;
|
||||
pub mod configuration;
|
||||
pub mod schema;
|
||||
pub mod user;
|
||||
pub mod video;
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
pub mod video_format_detector;
|
||||
@@ -0,0 +1,51 @@
|
||||
use std::fs::File;
|
||||
use std::path::Path;
|
||||
use surveillant_systeme_domain::video::model::VideoFormat;
|
||||
use surveillant_systeme_domain::video::model::VideoFormat::{AVI, MKV, MP4, UNKNOWN, WEBM};
|
||||
|
||||
/// Determine Content-Type by reading file headers (magic numbers)
|
||||
pub fn get_content_type(path: &Path) -> VideoFormat {
|
||||
use std::io::Read;
|
||||
|
||||
// Read first few bytes to detect magic numbers
|
||||
let mut file = match File::open(path) {
|
||||
Ok(f) => f,
|
||||
Err(_) => return UNKNOWN,
|
||||
};
|
||||
|
||||
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 UNKNOWN,
|
||||
};
|
||||
|
||||
// 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 AVI;
|
||||
}
|
||||
|
||||
// Detect MKV format (EB ML header)
|
||||
if bytes_read >= 4 && buffer[0..4] == [0x1A, 0x45, 0xDF, 0xA3] {
|
||||
return MKV; // Proper MIME type for MKV
|
||||
}
|
||||
|
||||
// Detect MP4/MOV format (ftyp box)
|
||||
if bytes_read >= 8 && buffer[4..8] == [102, 116, 121, 112] {
|
||||
return 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 WEBM;
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback to extension-based detection for unknown formats
|
||||
match path.extension().and_then(|ext| ext.to_str()) {
|
||||
Some("mp4") | Some("mov") => MP4,
|
||||
Some("webm") => WEBM,
|
||||
_ => UNKNOWN,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user