1
+ use async_tiff:: TIFF ;
2
+ use std:: sync:: Arc ;
3
+
1
4
#[ cfg( feature = "object_store" ) ]
2
5
use async_tiff:: reader:: ObjectReader ;
3
- use async_tiff:: TIFF ;
4
6
#[ cfg( feature = "object_store" ) ]
5
7
use object_store:: local:: LocalFileSystem ;
6
8
#[ cfg( feature = "object_store" ) ]
7
9
use 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 ;
9
22
10
23
const TEST_IMAGE_DIR : & str = "tests/image_tiff/images/" ;
11
24
@@ -17,10 +30,49 @@ pub(crate) async fn open_tiff(filename: &str) -> TIFF {
17
30
TIFF :: try_open ( reader) . await . unwrap ( )
18
31
}
19
32
20
- #[ cfg( not( feature = "object_store" ) ) ]
33
+ #[ cfg( all ( feature = "tokio" , not( feature = "object_store" ) ) ) ]
21
34
pub ( crate ) async fn open_tiff ( filename : & str ) -> TIFF {
22
35
// let store = Arc::new(LocalFileSystem::new_with_prefix(current_dir().unwrap()).unwrap());
23
36
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
+ ) ) ;
25
77
TIFF :: try_open ( reader) . await . unwrap ( )
26
78
}
0 commit comments