Skip to content

Commit

Permalink
changes after initial rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanfranke committed Oct 30, 2024
1 parent 7cb0b60 commit bed9f9c
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 568 deletions.
2 changes: 1 addition & 1 deletion src/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ mod drop;
mod drop_index;
mod find;
mod find_and_modify;
pub mod gridfs;
pub(crate) mod gridfs;
mod insert_many;
mod insert_one;
mod list_collections;
Expand Down
2 changes: 1 addition & 1 deletion src/action/gridfs.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Action builders for gridfs.
mod delete;
mod download;
pub mod download;
mod drop;
mod find;
mod rename;
Expand Down
50 changes: 47 additions & 3 deletions src/action/gridfs/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,43 @@ use crate::{
gridfs::{
FilesCollectionDocument,
GridFsBucket,
GridFsDownloadByIdOptions,
GridFsDownloadByNameOptions,
GridFsDownloadStream,
},
};

// TODO: Determine proper visibility, including parent modules.
pub struct DownloadRange(pub Option<u64>, pub Option<u64>);

fn create_download_range(start: Option<u64>, end: Option<u64>) -> Result<DownloadRange> {
match (start, end) {
(Some(start), Some(end)) => {
if start <= end {
Ok(DownloadRange(Some(start), Some(end)))
} else {
Err(
ErrorKind::GridFs(GridFsErrorKind::InvalidPartialDownloadRange { start, end })
.into(),
)
}
}
_ => Ok(DownloadRange(start, end)),
}
}

impl GridFsBucket {
/// Opens and returns a [`GridFsDownloadStream`] from which the application can read
/// the contents of the stored file specified by `id`.
///
/// `await` will return d[`Result<GridFsDownloadStream>`].
#[deeplink]
pub fn open_download_stream(&self, id: Bson) -> OpenDownloadStream {
OpenDownloadStream { bucket: self, id }
OpenDownloadStream {
bucket: self,
id,
options: None,
}
}

/// Opens and returns a [`GridFsDownloadStream`] from which the application can read
Expand Down Expand Up @@ -129,15 +153,28 @@ impl crate::sync::gridfs::GridFsBucket {
pub struct OpenDownloadStream<'a> {
bucket: &'a GridFsBucket,
id: Bson,
options: Option<GridFsDownloadByIdOptions>,
}

impl<'a> OpenDownloadStream<'a> {
option_setters! { options: GridFsDownloadByIdOptions;
start: u64,
end: u64,
}
}

#[action_impl(sync = crate::sync::gridfs::GridFsDownloadStream)]
impl<'a> Action for OpenDownloadStream<'a> {
type Future = OpenDownloadStreamFuture;

async fn execute(self) -> Result<GridFsDownloadStream> {
let range = create_download_range(
self.options.as_ref().and_then(|options| options.start),
self.options.as_ref().and_then(|options| options.end),
)?;

let file = self.bucket.find_file_by_id(&self.id).await?;
GridFsDownloadStream::new(file, self.bucket.chunks()).await
GridFsDownloadStream::new(file, self.bucket.chunks(), range).await
}
}

Expand All @@ -154,6 +191,8 @@ pub struct OpenDownloadStreamByName<'a> {
impl<'a> OpenDownloadStreamByName<'a> {
option_setters! { options: GridFsDownloadByNameOptions;
revision: i32,
start: u64,
end: u64,
}
}

Expand All @@ -162,10 +201,15 @@ impl<'a> Action for OpenDownloadStreamByName<'a> {
type Future = OpenDownloadStreamByNameFuture;

async fn execute(self) -> Result<GridFsDownloadStream> {
let range = create_download_range(
self.options.as_ref().and_then(|options| options.start),
self.options.as_ref().and_then(|options| options.end),
)?;

let file = self
.bucket
.find_file_by_name(&self.filename, self.options)
.await?;
GridFsDownloadStream::new(file, self.bucket.chunks()).await
GridFsDownloadStream::new(file, self.bucket.chunks(), range).await
}
}
2 changes: 1 addition & 1 deletion src/gridfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;

use crate::{
bson::{doc, oid::ObjectId, Bson, DateTime, Document, RawBinaryRef},
bson::{oid::ObjectId, Bson, DateTime, Document, RawBinaryRef},
checked::Checked,
error::Error,
options::{CollectionOptions, ReadConcern, SelectionCriteria, WriteConcern},
Expand Down
Loading

0 comments on commit bed9f9c

Please sign in to comment.