Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,26 @@ flate2 = "1.0.20"
futures = "0.3.31"
jpeg = { package = "jpeg-decoder", version = "0.3.0", default-features = false }
num_enum = "0.7.3"
object_store = "0.12"
object_store = { version = "0.12", optional = true }
# In the future we could make this feature-flagged, but for now we depend on
# object_store which uses reqwest.
reqwest = { version = "0.12", default-features = false }
reqwest = { version = "0.12", default-features = false, optional = true }
thiserror = "1"
tokio = { version = "1.43.0", optional = true }
weezl = "0.1.0"

[dev-dependencies]
tiff = "0.9.1"
tokio = { version = "1.9", features = ["macros", "fs", "rt-multi-thread"] }

[features]
default = ["object_store", "reqwest"]
reqwest = ["dep:reqwest"]
object_store = ["dep:object_store"]

[package.metadata.cargo-all-features]
# If your crate has a large number of optional dependencies, skip them for speed
# skip_optional_dependencies = true

# Exclude certain features from the build matrix
denylist = ["default"]
1 change: 1 addition & 0 deletions src/cog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ impl TIFF {
}
}

#[cfg(feature = "object_store")]
#[cfg(test)]
mod test {
use std::io::BufReader;
Expand Down
2 changes: 2 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub enum AsyncTiffError {
JPEGDecodingError(#[from] jpeg::Error),

/// Error while fetching data using object store.
#[cfg(feature = "object_store")]
#[error(transparent)]
ObjectStore(#[from] object_store::Error),

Expand All @@ -32,6 +33,7 @@ pub enum AsyncTiffError {
InternalTIFFError(#[from] crate::tiff::TiffError),

/// Reqwest error
#[cfg(feature = "reqwest")]
#[error(transparent)]
ReqwestError(#[from] reqwest::Error),

Expand Down
32 changes: 26 additions & 6 deletions src/reader.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
//! Abstractions for network reading.

use std::fmt::Debug;
use std::io::Read;
use std::io::{Read, Seek};
use std::ops::Range;
use std::sync::Arc;

use byteorder::{BigEndian, LittleEndian, ReadBytesExt};
use bytes::buf::Reader;
use bytes::{Buf, Bytes};
use futures::future::{BoxFuture, FutureExt, TryFutureExt};
use futures::future::{BoxFuture, FutureExt};
#[cfg(feature = "object_store")]
use futures::TryFutureExt;
#[cfg(feature = "object_store")]
use object_store::ObjectStore;

use crate::error::{AsyncTiffError, AsyncTiffResult};
Expand Down Expand Up @@ -67,6 +70,21 @@ impl AsyncFileReader for Box<dyn AsyncFileReader + '_> {
}
}

impl AsyncFileReader for std::fs::File {
fn get_bytes(&self, range: Range<u64>) -> BoxFuture<'_, AsyncTiffResult<Bytes>> {
async move {
let mut file = self.try_clone()?;
file.seek(std::io::SeekFrom::Start(range.start))?;
let len = (range.end - range.start) as usize;
let mut buf = vec![0u8; len];
file.read_exact(&mut buf)?;
let res = Bytes::copy_from_slice(&buf);
Ok(res)
}
.boxed()
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not add this because this will perform blocking reads on the main thread.

Instead we should probably restore the tokio integration

async-tiff/src/reader.rs

Lines 70 to 91 in 6a7a2dc

// #[cfg(feature = "tokio")]
// impl<T: tokio::io::AsyncRead + tokio::io::AsyncSeek + Unpin + Debug + Send + Sync> AsyncFileReader
// for T
// {
// fn get_bytes(&self, range: Range<u64>) -> BoxFuture<'_, AsyncTiffResult<Bytes>> {
// use tokio::io::{AsyncReadExt, AsyncSeekExt};
// async move {
// self.seek(std::io::SeekFrom::Start(range.start)).await?;
// let to_read = (range.end - range.start).try_into().unwrap();
// let mut buffer = Vec::with_capacity(to_read);
// let read = self.take(to_read as u64).read_to_end(&mut buffer).await?;
// if read != to_read {
// return Err(AsyncTiffError::EndOfFile(to_read, read));
// }
// Ok(buffer.into())
// }
// .boxed()
// }
// }
which will allow reading from a tokio async file, which I think performs file IO from a thread pool

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem I had was that the feature testing also wants to test with all features disabled, and I didn't exactly know how to deal with that... added a fallback in util.rs

Copy link
Contributor Author

@feefladder feefladder Mar 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also tokio's asyncread require &mut self, so I changed the impl to tokio::fs::File, where we can try_clone() on &self. There doesn't seem to be a TryClone trait


// #[cfg(feature = "tokio")]
// impl<T: tokio::io::AsyncRead + tokio::io::AsyncSeek + Unpin + Debug + Send + Sync> AsyncFileReader
// for T
Expand All @@ -91,12 +109,13 @@ impl AsyncFileReader for Box<dyn AsyncFileReader + '_> {
// }

/// An AsyncFileReader that reads from an [`ObjectStore`] instance.
#[cfg(feature = "object_store")]
#[derive(Clone, Debug)]
pub struct ObjectReader {
store: Arc<dyn ObjectStore>,
path: object_store::path::Path,
}

#[cfg(feature = "object_store")]
impl ObjectReader {
/// Creates a new [`ObjectReader`] for the provided [`ObjectStore`] and path
///
Expand All @@ -105,7 +124,7 @@ impl ObjectReader {
Self { store, path }
}
}

#[cfg(feature = "object_store")]
impl AsyncFileReader for ObjectReader {
fn get_bytes(&self, range: Range<u64>) -> BoxFuture<'_, AsyncTiffResult<Bytes>> {
let range = range.start as _..range.end as _;
Expand Down Expand Up @@ -134,19 +153,20 @@ impl AsyncFileReader for ObjectReader {
}

/// An AsyncFileReader that reads from a URL using reqwest.
#[cfg(feature = "reqwest")]
#[derive(Debug, Clone)]
pub struct ReqwestReader {
client: reqwest::Client,
url: reqwest::Url,
}

#[cfg(feature = "reqwest")]
impl ReqwestReader {
/// Construct a new ReqwestReader from a reqwest client and URL.
pub fn new(client: reqwest::Client, url: reqwest::Url) -> Self {
Self { client, url }
}
}

#[cfg(feature = "reqwest")]
impl AsyncFileReader for ReqwestReader {
fn get_bytes(&self, range: Range<u64>) -> BoxFuture<'_, AsyncTiffResult<Bytes>> {
let url = self.url.clone();
Expand Down
17 changes: 14 additions & 3 deletions tests/image_tiff/util.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
use std::env::current_dir;
use std::sync::Arc;

#[cfg(feature = "object_store")]
use async_tiff::reader::ObjectReader;
use async_tiff::TIFF;
#[cfg(feature = "object_store")]
use object_store::local::LocalFileSystem;
#[cfg(feature = "object_store")]
use std::env::current_dir;
use std::sync::Arc;

const TEST_IMAGE_DIR: &str = "tests/image_tiff/images/";

#[cfg(feature = "object_store")]
pub(crate) async fn open_tiff(filename: &str) -> TIFF {
let store = Arc::new(LocalFileSystem::new_with_prefix(current_dir().unwrap()).unwrap());
let path = format!("{TEST_IMAGE_DIR}/{filename}");
let reader = Arc::new(ObjectReader::new(store.clone(), path.as_str().into()));
TIFF::try_open(reader).await.unwrap()
}

#[cfg(not(feature = "object_store"))]
pub(crate) async fn open_tiff(filename: &str) -> TIFF {
// let store = Arc::new(LocalFileSystem::new_with_prefix(current_dir().unwrap()).unwrap());
let path = format!("{TEST_IMAGE_DIR}/{filename}");
let reader = Arc::new(std::fs::File::open(path).expect("could not open file"));
TIFF::try_open(reader).await.unwrap()
}
Loading