Skip to content

Commit bed9f9c

Browse files
committed
changes after initial rebase
1 parent 7cb0b60 commit bed9f9c

File tree

7 files changed

+124
-568
lines changed

7 files changed

+124
-568
lines changed

src/action.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ mod drop;
1414
mod drop_index;
1515
mod find;
1616
mod find_and_modify;
17-
pub mod gridfs;
17+
pub(crate) mod gridfs;
1818
mod insert_many;
1919
mod insert_one;
2020
mod list_collections;

src/action/gridfs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Action builders for gridfs.
22
33
mod delete;
4-
mod download;
4+
pub mod download;
55
mod drop;
66
mod find;
77
mod rename;

src/action/gridfs/download.rs

+47-3
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,43 @@ use crate::{
66
gridfs::{
77
FilesCollectionDocument,
88
GridFsBucket,
9+
GridFsDownloadByIdOptions,
910
GridFsDownloadByNameOptions,
1011
GridFsDownloadStream,
1112
},
1213
};
1314

15+
// TODO: Determine proper visibility, including parent modules.
16+
pub struct DownloadRange(pub Option<u64>, pub Option<u64>);
17+
18+
fn create_download_range(start: Option<u64>, end: Option<u64>) -> Result<DownloadRange> {
19+
match (start, end) {
20+
(Some(start), Some(end)) => {
21+
if start <= end {
22+
Ok(DownloadRange(Some(start), Some(end)))
23+
} else {
24+
Err(
25+
ErrorKind::GridFs(GridFsErrorKind::InvalidPartialDownloadRange { start, end })
26+
.into(),
27+
)
28+
}
29+
}
30+
_ => Ok(DownloadRange(start, end)),
31+
}
32+
}
33+
1434
impl GridFsBucket {
1535
/// Opens and returns a [`GridFsDownloadStream`] from which the application can read
1636
/// the contents of the stored file specified by `id`.
1737
///
1838
/// `await` will return d[`Result<GridFsDownloadStream>`].
1939
#[deeplink]
2040
pub fn open_download_stream(&self, id: Bson) -> OpenDownloadStream {
21-
OpenDownloadStream { bucket: self, id }
41+
OpenDownloadStream {
42+
bucket: self,
43+
id,
44+
options: None,
45+
}
2246
}
2347

2448
/// Opens and returns a [`GridFsDownloadStream`] from which the application can read
@@ -129,15 +153,28 @@ impl crate::sync::gridfs::GridFsBucket {
129153
pub struct OpenDownloadStream<'a> {
130154
bucket: &'a GridFsBucket,
131155
id: Bson,
156+
options: Option<GridFsDownloadByIdOptions>,
157+
}
158+
159+
impl<'a> OpenDownloadStream<'a> {
160+
option_setters! { options: GridFsDownloadByIdOptions;
161+
start: u64,
162+
end: u64,
163+
}
132164
}
133165

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

138170
async fn execute(self) -> Result<GridFsDownloadStream> {
171+
let range = create_download_range(
172+
self.options.as_ref().and_then(|options| options.start),
173+
self.options.as_ref().and_then(|options| options.end),
174+
)?;
175+
139176
let file = self.bucket.find_file_by_id(&self.id).await?;
140-
GridFsDownloadStream::new(file, self.bucket.chunks()).await
177+
GridFsDownloadStream::new(file, self.bucket.chunks(), range).await
141178
}
142179
}
143180

@@ -154,6 +191,8 @@ pub struct OpenDownloadStreamByName<'a> {
154191
impl<'a> OpenDownloadStreamByName<'a> {
155192
option_setters! { options: GridFsDownloadByNameOptions;
156193
revision: i32,
194+
start: u64,
195+
end: u64,
157196
}
158197
}
159198

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

164203
async fn execute(self) -> Result<GridFsDownloadStream> {
204+
let range = create_download_range(
205+
self.options.as_ref().and_then(|options| options.start),
206+
self.options.as_ref().and_then(|options| options.end),
207+
)?;
208+
165209
let file = self
166210
.bucket
167211
.find_file_by_name(&self.filename, self.options)
168212
.await?;
169-
GridFsDownloadStream::new(file, self.bucket.chunks()).await
213+
GridFsDownloadStream::new(file, self.bucket.chunks(), range).await
170214
}
171215
}

src/gridfs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use serde::{Deserialize, Serialize};
1010
use serde_with::skip_serializing_none;
1111

1212
use crate::{
13-
bson::{doc, oid::ObjectId, Bson, DateTime, Document, RawBinaryRef},
13+
bson::{oid::ObjectId, Bson, DateTime, Document, RawBinaryRef},
1414
checked::Checked,
1515
error::Error,
1616
options::{CollectionOptions, ReadConcern, SelectionCriteria, WriteConcern},

0 commit comments

Comments
 (0)