Skip to content

Commit

Permalink
Fix all buffer sent bug
Browse files Browse the repository at this point in the history
  • Loading branch information
fangpenlin committed Jan 6, 2025
1 parent 99daf7a commit eede4b4
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/api/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ impl Processor {
fn list_files(&self, path: &str) -> anyhow::Result<Response> {
let dir_path = Path::new(&self.root_dir).join(path);
log::info!(
"Listing files at {}",
"Listing files at {:?}",
dir_path.to_str().unwrap_or("<Unknown>")
);
// Ideally we should find a way to learn the size of all files, but we need to
Expand Down Expand Up @@ -141,18 +141,19 @@ impl Processor {
where
F: FnMut(CommandResponse),
{
log::info!("Fetch file at {:?}, chunk_size={}", path, chunk_size);
let mut file = std::fs::File::open(path)?;
let file_size = file.metadata()?.len();
let mut buf = vec![0; chunk_size as usize];
for offset in (0..file_size).step_by(chunk_size as usize) {
file.read(&mut buf)?;
let read_size = file.read(&mut buf)?;
// TODO: somehow stream_position doesn't work correctly?
// assert_eq!(file.stream_position().unwrap(), offset);
send(CommandResponse {
id: req_id.to_string(),
response: FetchFileChunk {
offset,
data: &buf,
data: &buf[..read_size],
is_final: offset + chunk_size >= file_size,
},
});
Expand Down Expand Up @@ -216,6 +217,7 @@ pub async fn process_events(
}
SessionEvent::ReceiveText { text } => {
let request: serde_json::Result<CommandRequest> = serde_json::from_str(&text);
log::info!("Processing request {:?}", request);
match request {
Ok(request) => processor.as_mut().unwrap().process(
&request,
Expand Down

0 comments on commit eede4b4

Please sign in to comment.