From 22926ce29e3f8e040ec349401aeb6a77f32eae72 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Wed, 4 Mar 2026 21:26:04 +0800 Subject: Initialize Butchunker project structure and policy system --- policy/_policies/src/stream_read.rs | 46 +++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 policy/_policies/src/stream_read.rs (limited to 'policy/_policies/src/stream_read.rs') diff --git a/policy/_policies/src/stream_read.rs b/policy/_policies/src/stream_read.rs new file mode 100644 index 0000000..5cf7791 --- /dev/null +++ b/policy/_policies/src/stream_read.rs @@ -0,0 +1,46 @@ +use crate::error::ChunkFailed; +use std::{collections::HashMap, path::Path}; + +pub async fn chunk_stream_process( + path: &Path, + stream_data: &mut T, + size: u32, + params: &HashMap<&str, &str>, + chunk_func: F, +) -> Result, ChunkFailed> +where + T: Default, + F: AsyncFn(&[u8], u32, &mut T, &HashMap<&str, &str>) -> Option, +{ + let mut file = tokio::fs::File::open(path) + .await + .map_err(|_| ChunkFailed::FileOpenFailed(path.to_path_buf()))?; + let mut buffer = vec![0u8; size as usize]; + let mut splits = Vec::new(); + let mut total_read = 0; + + loop { + let bytes_read = tokio::io::AsyncReadExt::read(&mut file, &mut buffer) + .await + .map_err(|_| ChunkFailed::FileReadFailed(path.to_path_buf()))?; + + if bytes_read == 0 { + break Ok(splits); + } + + // Process chunking on the buffer slice + let chunk_result = chunk_func( + &buffer[..bytes_read], + bytes_read as u32, + stream_data, + params, + ) + .await; + + if let Some(offset) = chunk_result { + splits.push(total_read + offset); + } + + total_read += bytes_read as u32; + } +} -- cgit