1+ use async_tiff:: TIFF ;
2+ use std:: sync:: Arc ;
3+
14#[ cfg( feature = "object_store" ) ]
25use async_tiff:: reader:: ObjectReader ;
3- use async_tiff:: TIFF ;
46#[ cfg( feature = "object_store" ) ]
57use object_store:: local:: LocalFileSystem ;
68#[ cfg( feature = "object_store" ) ]
79use std:: env:: current_dir;
8- use std:: sync:: Arc ;
10+
11+ #[ cfg( not( any( feature = "tokio" , feature = "object_store" ) ) ) ]
12+ use async_tiff:: {
13+ error:: { AsyncTiffError , AsyncTiffResult } ,
14+ reader:: AsyncFileReader ,
15+ } ;
16+ #[ cfg( not( any( feature = "tokio" , feature = "object_store" ) ) ) ]
17+ use bytes:: Bytes ;
18+ #[ cfg( not( any( feature = "tokio" , feature = "object_store" ) ) ) ]
19+ use futures:: { future:: BoxFuture , FutureExt } ;
20+ #[ cfg( not( any( feature = "tokio" , feature = "object_store" ) ) ) ]
21+ use std:: ops:: Range ;
922
1023const TEST_IMAGE_DIR : & str = "tests/image_tiff/images/" ;
1124
@@ -17,10 +30,49 @@ pub(crate) async fn open_tiff(filename: &str) -> TIFF {
1730 TIFF :: try_open ( reader) . await . unwrap ( )
1831}
1932
20- #[ cfg( not( feature = "object_store" ) ) ]
33+ #[ cfg( all ( feature = "tokio" , not( feature = "object_store" ) ) ) ]
2134pub ( crate ) async fn open_tiff ( filename : & str ) -> TIFF {
2235 // let store = Arc::new(LocalFileSystem::new_with_prefix(current_dir().unwrap()).unwrap());
2336 let path = format ! ( "{TEST_IMAGE_DIR}/{filename}" ) ;
24- let reader = Arc :: new ( std:: fs:: File :: open ( path) . expect ( "could not open file" ) ) ;
37+ let reader = Arc :: new (
38+ tokio:: fs:: File :: open ( path)
39+ . await
40+ . expect ( "could not open file" ) ,
41+ ) ;
42+ TIFF :: try_open ( reader) . await . unwrap ( )
43+ }
44+
45+ #[ cfg( not( any( feature = "tokio" , feature = "object_store" ) ) ) ]
46+ #[ derive( Debug ) ]
47+ struct TokioFile ( tokio:: fs:: File ) ;
48+ #[ cfg( not( any( feature = "tokio" , feature = "object_store" ) ) ) ]
49+ impl AsyncFileReader for TokioFile {
50+ fn get_bytes ( & self , range : Range < u64 > ) -> BoxFuture < ' _ , AsyncTiffResult < Bytes > > {
51+ use tokio:: io:: { AsyncReadExt , AsyncSeekExt } ;
52+
53+ async move {
54+ let mut file = self . 0 . try_clone ( ) . await ?;
55+ file. seek ( std:: io:: SeekFrom :: Start ( range. start ) ) . await ?;
56+
57+ let to_read = ( range. end - range. start ) . try_into ( ) . unwrap ( ) ;
58+ let mut buffer = Vec :: with_capacity ( to_read) ;
59+ let read = file. take ( to_read as u64 ) . read_to_end ( & mut buffer) . await ?;
60+ if read != to_read {
61+ return Err ( AsyncTiffError :: EndOfFile ( to_read, read) ) ;
62+ }
63+
64+ Ok ( buffer. into ( ) )
65+ }
66+ . boxed ( )
67+ }
68+ }
69+ #[ cfg( not( any( feature = "tokio" , feature = "object_store" ) ) ) ]
70+ pub ( crate ) async fn open_tiff ( filename : & str ) -> TIFF {
71+ let path = format ! ( "{TEST_IMAGE_DIR}/{filename}" ) ;
72+ let reader = Arc :: new ( TokioFile (
73+ tokio:: fs:: File :: open ( path)
74+ . await
75+ . expect ( "could not open file" ) ,
76+ ) ) ;
2577 TIFF :: try_open ( reader) . await . unwrap ( )
2678}
0 commit comments