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