@@ -29,6 +29,7 @@ use std::sync::Arc;
2929
3030use git_version:: git_version;
3131use lz4_flex:: frame:: { FrameDecoder , FrameEncoder } ;
32+ use tempfile:: NamedTempFile ;
3233
3334use super :: gridpdf:: GridArray ;
3435use super :: metadata:: MetaData ;
@@ -131,7 +132,12 @@ impl GridArrayCollection {
131132 encoder. write_all ( serialized) ?;
132133 }
133134
134- encoder. finish ( ) ?;
135+ let mut writer = encoder. finish ( ) ?;
136+ writer. flush ( ) ?;
137+
138+ // Sync to disk to ensure data is written before returning
139+ writer. get_mut ( ) . sync_all ( ) ?;
140+
135141 Ok ( ( ) )
136142 }
137143
@@ -324,13 +330,12 @@ impl GridArrayReader {
324330 new_grids. push ( converted_grid) ;
325331 }
326332
327- let temp_path =
328- std:: env:: temp_dir ( ) . join ( format ! ( "neopdf_conversion_{}.tmp" , std:: process:: id( ) ) ) ;
329- let grid_refs: Vec < & GridArray > = new_grids. iter ( ) . collect ( ) ;
330- GridArrayCollection :: compress ( & grid_refs, & metadata, & temp_path) ?;
333+ let temp_file = NamedTempFile :: new ( ) ?;
334+ let temp_path = temp_file. path ( ) ;
331335
332- let result = Self :: from_file_v2 ( & temp_path) ?;
333- std:: fs:: remove_file ( & temp_path) . ok ( ) ;
336+ let grid_refs: Vec < & GridArray > = new_grids. iter ( ) . collect ( ) ;
337+ GridArrayCollection :: compress ( & grid_refs, & metadata, temp_path) ?;
338+ let result = Self :: from_file_v2 ( temp_path) ?;
334339
335340 Ok ( result)
336341 }
@@ -543,13 +548,12 @@ impl LazyGridArrayIterator {
543548 new_grids. push ( converted_grid) ;
544549 }
545550
546- let temp_path =
547- std:: env:: temp_dir ( ) . join ( format ! ( "neopdf_lazy_conversion_{}.tmp" , std:: process:: id( ) ) ) ;
548- let grid_refs: Vec < & GridArray > = new_grids. iter ( ) . collect ( ) ;
549- GridArrayCollection :: compress ( & grid_refs, & metadata, & temp_path) ?;
551+ let temp_file = NamedTempFile :: new ( ) ?;
552+ let temp_path = temp_file. path ( ) ;
550553
551- let result = Self :: from_file_v2 ( & temp_path) ?;
552- std:: fs:: remove_file ( & temp_path) . ok ( ) ;
554+ let grid_refs: Vec < & GridArray > = new_grids. iter ( ) . collect ( ) ;
555+ GridArrayCollection :: compress ( & grid_refs, & metadata, temp_path) ?;
556+ let result = Self :: from_file_v2 ( temp_path) ?;
553557
554558 Ok ( result)
555559 }
0 commit comments