Skip to content

Commit 7a4d428

Browse files
implement from_file abstraction
1 parent b288365 commit 7a4d428

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

apps/desktop/src-tauri/src/upload.rs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use tauri::{AppHandle, ipc::Channel};
3030
use tauri_plugin_clipboard_manager::ClipboardExt;
3131
use tauri_specta::Event;
3232
use tokio::fs::File;
33-
use tokio::io::{AsyncReadExt, AsyncSeekExt};
33+
use tokio::io::{AsyncReadExt, AsyncSeekExt, BufReader};
3434
use tokio::sync::watch;
3535
use tokio::task::{self, JoinHandle};
3636
use tokio::time::sleep;
@@ -626,7 +626,7 @@ impl InstantMultipartUpload {
626626
}
627627
}
628628

629-
struct Chunk {
629+
pub struct Chunk {
630630
/// The total size of the file to be uploaded.
631631
/// This can change as the recording grows.
632632
total_size: u64,
@@ -636,9 +636,31 @@ struct Chunk {
636636
chunk: Bytes,
637637
}
638638

639-
/// Creates a stream that reads chunks from a potentially growing file,
640-
/// yielding (part_number, chunk_data) pairs. The first chunk is yielded last
641-
/// to allow for header rewriting after recording completion.
639+
/// Creates a stream that reads chunks from a file, yielding [Chunk]'s.
640+
pub fn from_file(path: PathBuf) -> impl Stream<Item = io::Result<Chunk>> {
641+
try_stream! {
642+
let file = File::open(path).await?;
643+
let total_size = file.metadata().await?.len();
644+
let mut file = BufReader::new(file);
645+
646+
let mut buf = vec![0u8; CHUNK_SIZE as usize];
647+
let mut part_number = 0;
648+
loop {
649+
part_number += 1;
650+
let n = file.read(&mut buf).await?;
651+
if n == 0 { break; }
652+
yield Chunk {
653+
total_size,
654+
part_number,
655+
chunk: Bytes::copy_from_slice(&buf[..n]),
656+
};
657+
}
658+
}
659+
}
660+
661+
/// Creates a stream that reads chunks from a potentially growing file, yielding [Chunk]'s.
662+
/// The first chunk of the file is yielded last to allow for header rewriting after recording completion.
663+
/// This uploader will continually poll the filesystem and wait for the file to stop uploading before flushing the rest.
642664
pub fn from_pending_file(
643665
path: PathBuf,
644666
realtime_upload_done: Option<Receiver<()>>,

0 commit comments

Comments
 (0)