Files
lumi-re/backend/application/src/video/service.rs
T

101 lines
2.6 KiB
Rust

// Video service implementations for streaming files chunk by chunk
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::Path;
use std::io::Read;
use lumiere_domain::video::error::VideoError;
use lumiere_domain::video::model::library_item::{LibraryItem, LibraryRawItem};
use lumiere_domain::video::port::VideoPort;
#[derive(Clone)]
pub struct VideoService {
video_port: Box<dyn VideoPort>
}
impl VideoService {
pub fn new(video_port: Box<dyn VideoPort>) -> Self {
Self { video_port }
}
pub fn list_root_folder(self) -> Result<Vec<LibraryItem>, VideoError> {
let read_items_with_possibly_no_id = self.video_port.list_root_folder()?;
let items_with_id = read_items_with_possibly_no_id.iter()
.map(LibraryRawItem::map_to_library_item)
.collect();
self.video_port.save_all(items_with_id);
Ok(items_with_id)
}
}
/// Basic example: Read file line by line using BufReader
pub fn read_file_line_by_line(path: &str) -> std::io::Result<()> {
let path = Path::new(path);
let file = File::open(path)?;
let reader = BufReader::new(file);
// Read line by line
for line in reader.lines() {
let line = line?;
println!("Line: {}", line);
}
Ok(())
}
/// Read file in fixed-size chunks
pub fn read_file_in_chunks(path: &str, chunk_size: usize) -> std::io::Result<()> {
let mut file = File::open(path)?;
let mut buffer = vec![0; chunk_size];
loop {
let n = file.read(&mut buffer)?;
if n == 0 {
break; // EOF
}
println!("Read {} bytes: {:?}", n, &buffer[..n]);
}
Ok(())
}
/// Using std::io::copy for streaming between files
pub fn copy_file_streaming(input_path: &str, output_path: &str) -> std::io::Result<()> {
use std::io;
let mut input = File::open(input_path)?;
let mut output = File::create(output_path)?;
// Copy in chunks without loading entire file into memory
io::copy(&mut input, &mut output)?;
Ok(())
}
/// Process large files efficiently with configurable buffer size
pub fn process_large_file(path: &str, buffer_size: usize) -> std::io::Result<()> {
let file = File::open(path)?;
let mut reader = BufReader::new(file);
let mut buffer = vec![0; buffer_size];
loop {
let bytes_read = reader.read(&mut buffer)?;
if bytes_read == 0 {
break;
}
process_chunk(&buffer[..bytes_read]);
}
Ok(())
}
/// Example chunk processing function
fn process_chunk(chunk: &[u8]) {
// Process your chunk here
println!("Processing {} bytes", chunk.len());
}