|
| 1 | +use bson::{doc, Bson}; |
| 2 | + |
| 3 | +use crate::{ |
| 4 | + action::{action_impl, deeplink, option_setters}, |
| 5 | + error::{ErrorKind, GridFsErrorKind, GridFsFileIdentifier, Result}, |
| 6 | + gridfs::{FilesCollectionDocument, GridFsDownloadByNameOptions}, |
| 7 | + GridFsBucket, |
| 8 | + GridFsDownloadStream, |
| 9 | +}; |
| 10 | + |
| 11 | +impl GridFsBucket { |
| 12 | + /// Opens and returns a [`GridFsDownloadStream`] from which the application can read |
| 13 | + /// the contents of the stored file specified by `id`. |
| 14 | + /// |
| 15 | + /// `await` will return d[`Result<GridFsDownloadStream>`]. |
| 16 | + #[deeplink] |
| 17 | + pub fn open_download_stream(&self, id: Bson) -> OpenDownloadStream { |
| 18 | + OpenDownloadStream { bucket: self, id } |
| 19 | + } |
| 20 | + |
| 21 | + /// Opens and returns a [`GridFsDownloadStream`] from which the application can read |
| 22 | + /// the contents of the stored file specified by `filename`. |
| 23 | + /// |
| 24 | + /// If there are multiple files in the bucket with the given filename, the `revision` in the |
| 25 | + /// options provided is used to determine which one to download. See the documentation for |
| 26 | + /// [`GridFsDownloadByNameOptions`] for details on how to specify a revision. If no revision is |
| 27 | + /// provided, the file with `filename` most recently uploaded will be downloaded. |
| 28 | + /// |
| 29 | + /// `await` will return d[`Result<GridFsDownloadStream>`]. |
| 30 | + #[deeplink] |
| 31 | + pub fn open_download_stream_by_name( |
| 32 | + &self, |
| 33 | + filename: impl AsRef<str>, |
| 34 | + ) -> OpenDownloadStreamByName { |
| 35 | + OpenDownloadStreamByName { |
| 36 | + bucket: self, |
| 37 | + filename: filename.as_ref().to_owned(), |
| 38 | + options: None, |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + // Utility functions for finding files within the bucket. |
| 43 | + |
| 44 | + async fn find_file_by_id(&self, id: &Bson) -> Result<FilesCollectionDocument> { |
| 45 | + match self.find_one(doc! { "_id": id }).await? { |
| 46 | + Some(file) => Ok(file), |
| 47 | + None => Err(ErrorKind::GridFs(GridFsErrorKind::FileNotFound { |
| 48 | + identifier: GridFsFileIdentifier::Id(id.clone()), |
| 49 | + }) |
| 50 | + .into()), |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + async fn find_file_by_name( |
| 55 | + &self, |
| 56 | + filename: &str, |
| 57 | + options: Option<GridFsDownloadByNameOptions>, |
| 58 | + ) -> Result<FilesCollectionDocument> { |
| 59 | + let revision = options.and_then(|opts| opts.revision).unwrap_or(-1); |
| 60 | + let (sort, skip) = if revision >= 0 { |
| 61 | + (1, revision) |
| 62 | + } else { |
| 63 | + (-1, -revision - 1) |
| 64 | + }; |
| 65 | + |
| 66 | + match self |
| 67 | + .files() |
| 68 | + .find_one(doc! { "filename": filename }) |
| 69 | + .sort(doc! { "uploadDate": sort }) |
| 70 | + .skip(skip as u64) |
| 71 | + .await? |
| 72 | + { |
| 73 | + Some(fcd) => Ok(fcd), |
| 74 | + None => { |
| 75 | + if self |
| 76 | + .files() |
| 77 | + .find_one(doc! { "filename": filename }) |
| 78 | + .await? |
| 79 | + .is_some() |
| 80 | + { |
| 81 | + Err(ErrorKind::GridFs(GridFsErrorKind::RevisionNotFound { revision }).into()) |
| 82 | + } else { |
| 83 | + Err(ErrorKind::GridFs(GridFsErrorKind::FileNotFound { |
| 84 | + identifier: GridFsFileIdentifier::Filename(filename.into()), |
| 85 | + }) |
| 86 | + .into()) |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +#[cfg(feature = "sync")] |
| 94 | +impl crate::sync::gridfs::GridFsBucket { |
| 95 | + /// Opens and returns a [`GridFsDownloadStream`] from which the application can read |
| 96 | + /// the contents of the stored file specified by `id`. |
| 97 | + /// |
| 98 | + /// [`run`](OpenDownloadStream::run) will return d[`Result<GridFsDownloadStream>`]. |
| 99 | + #[deeplink] |
| 100 | + pub fn open_download_stream(&self, id: Bson) -> OpenDownloadStream { |
| 101 | + self.async_bucket.open_download_stream(id) |
| 102 | + } |
| 103 | + |
| 104 | + /// Opens and returns a [`GridFsDownloadStream`] from which the application can read |
| 105 | + /// the contents of the stored file specified by `filename`. |
| 106 | + /// |
| 107 | + /// If there are multiple files in the bucket with the given filename, the `revision` in the |
| 108 | + /// options provided is used to determine which one to download. See the documentation for |
| 109 | + /// [`GridFsDownloadByNameOptions`] for details on how to specify a revision. If no revision is |
| 110 | + /// provided, the file with `filename` most recently uploaded will be downloaded. |
| 111 | + /// |
| 112 | + /// [`run`](OpenDownloadStreamByName::run) will return d[`Result<GridFsDownloadStream>`]. |
| 113 | + #[deeplink] |
| 114 | + pub fn open_download_stream_by_name( |
| 115 | + &self, |
| 116 | + filename: impl AsRef<str>, |
| 117 | + ) -> OpenDownloadStreamByName { |
| 118 | + self.async_bucket.open_download_stream_by_name(filename) |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +/// Opens and returns a [`GridFsDownloadStream`] from which the application can read |
| 123 | +/// the contents of the stored file specified by an id. Construct with |
| 124 | +/// [`GridFsBucket::open_download_stream`]. |
| 125 | +#[must_use] |
| 126 | +pub struct OpenDownloadStream<'a> { |
| 127 | + bucket: &'a GridFsBucket, |
| 128 | + id: Bson, |
| 129 | +} |
| 130 | + |
| 131 | +action_impl! { |
| 132 | + impl<'a> Action for OpenDownloadStream<'a> { |
| 133 | + type Future = OpenDownloadStreamFuture; |
| 134 | + |
| 135 | + async fn execute(self) -> Result<GridFsDownloadStream> { |
| 136 | + let file = self.bucket.find_file_by_id(&self.id).await?; |
| 137 | + GridFsDownloadStream::new(file, self.bucket.chunks()).await |
| 138 | + } |
| 139 | + |
| 140 | + fn sync_wrap(out) -> Result<crate::sync::gridfs::GridFsDownloadStream> { |
| 141 | + out.map(crate::sync::gridfs::GridFsDownloadStream::new) |
| 142 | + } |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +/// Opens and returns a [`GridFsDownloadStream`] from which the application can read |
| 147 | +/// the contents of the stored file specified by a filename. Construct with |
| 148 | +/// [`GridFsBucket::open_download_stream_by_name`]. |
| 149 | +#[must_use] |
| 150 | +pub struct OpenDownloadStreamByName<'a> { |
| 151 | + bucket: &'a GridFsBucket, |
| 152 | + filename: String, |
| 153 | + options: Option<GridFsDownloadByNameOptions>, |
| 154 | +} |
| 155 | + |
| 156 | +impl<'a> OpenDownloadStreamByName<'a> { |
| 157 | + option_setters! { options: GridFsDownloadByNameOptions; |
| 158 | + revision: i32, |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +action_impl! { |
| 163 | + impl<'a> Action for OpenDownloadStreamByName<'a> { |
| 164 | + type Future = OpenDownloadStreamByNameFuture; |
| 165 | + |
| 166 | + async fn execute(self) -> Result<GridFsDownloadStream> { |
| 167 | + let file = self |
| 168 | + .bucket |
| 169 | + .find_file_by_name(&self.filename, self.options) |
| 170 | + .await?; |
| 171 | + GridFsDownloadStream::new(file, self.bucket.chunks()).await |
| 172 | + } |
| 173 | + |
| 174 | + fn sync_wrap(out) -> Result<crate::sync::gridfs::GridFsDownloadStream> { |
| 175 | + out.map(crate::sync::gridfs::GridFsDownloadStream::new) |
| 176 | + } |
| 177 | + } |
| 178 | +} |
0 commit comments