@@ -30,7 +30,7 @@ use tauri::{AppHandle, ipc::Channel};
30
30
use tauri_plugin_clipboard_manager:: ClipboardExt ;
31
31
use tauri_specta:: Event ;
32
32
use tokio:: fs:: File ;
33
- use tokio:: io:: { AsyncReadExt , AsyncSeekExt } ;
33
+ use tokio:: io:: { AsyncReadExt , AsyncSeekExt , BufReader } ;
34
34
use tokio:: sync:: watch;
35
35
use tokio:: task:: { self , JoinHandle } ;
36
36
use tokio:: time:: sleep;
@@ -626,7 +626,7 @@ impl InstantMultipartUpload {
626
626
}
627
627
}
628
628
629
- struct Chunk {
629
+ pub struct Chunk {
630
630
/// The total size of the file to be uploaded.
631
631
/// This can change as the recording grows.
632
632
total_size : u64 ,
@@ -636,9 +636,31 @@ struct Chunk {
636
636
chunk : Bytes ,
637
637
}
638
638
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.
642
664
pub fn from_pending_file (
643
665
path : PathBuf ,
644
666
realtime_upload_done : Option < Receiver < ( ) > > ,
0 commit comments