@@ -4,14 +4,14 @@ use tokio_util::compat::FuturesAsyncWriteCompatExt;
4
4
use super :: { options:: GridFsDownloadByNameOptions , FilesCollectionDocument , GridFsBucket } ;
5
5
use crate :: {
6
6
bson:: { doc, Bson } ,
7
- error:: { ErrorKind , Result } ,
7
+ error:: { ErrorKind , GridFsErrorKind , Result } ,
8
8
options:: { FindOneOptions , FindOptions } ,
9
9
} ;
10
10
11
11
impl GridFsBucket {
12
12
/// Downloads the contents of the stored file specified by `id` and writes
13
13
/// the contents to the `destination`.
14
- pub async fn download_to_tokio_writer < T > ( & self , id : Bson , destination : & mut T ) -> Result < ( ) >
14
+ pub async fn download_to_tokio_writer < T > ( & self , id : Bson , destination : T ) -> Result < ( ) >
15
15
where
16
16
T : tokio:: io:: AsyncWrite + std:: marker:: Unpin ,
17
17
{
@@ -23,14 +23,14 @@ impl GridFsBucket {
23
23
let file = match self
24
24
. inner
25
25
. files
26
- . find_one ( doc ! { "_id" : & id } , options)
26
+ . find_one ( doc ! { "_id" : id . clone ( ) } , options)
27
27
. await ?
28
28
{
29
29
Some ( fcd) => fcd,
30
30
None => {
31
- return Err ( ErrorKind :: InvalidArgument {
32
- message : format ! ( "couldn't find file with id {}" , & id ) ,
33
- }
31
+ return Err ( ErrorKind :: GridFs ( GridFsErrorKind :: FileNotFound {
32
+ identifier : id . into ( ) ,
33
+ } )
34
34
. into ( ) ) ;
35
35
}
36
36
} ;
@@ -40,11 +40,7 @@ impl GridFsBucket {
40
40
41
41
/// Downloads the contents of the stored file specified by `id` and writes
42
42
/// the contents to the `destination`.
43
- pub async fn download_to_futures_0_3_writer < T > (
44
- & self ,
45
- id : Bson ,
46
- destination : & mut T ,
47
- ) -> Result < ( ) >
43
+ pub async fn download_to_futures_0_3_writer < T > ( & self , id : Bson , destination : T ) -> Result < ( ) >
48
44
where
49
45
T : futures_util:: io:: AsyncWrite + std:: marker:: Unpin ,
50
46
{
@@ -59,7 +55,7 @@ impl GridFsBucket {
59
55
pub async fn download_to_tokio_writer_by_name < T > (
60
56
& self ,
61
57
filename : impl AsRef < str > ,
62
- destination : & mut T ,
58
+ destination : T ,
63
59
options : impl Into < Option < GridFsDownloadByNameOptions > > ,
64
60
) -> Result < ( ) >
65
61
where
@@ -79,17 +75,27 @@ impl GridFsBucket {
79
75
. build ( ) ;
80
76
81
77
let file = match self
82
- . inner
83
- . files
78
+ . files ( )
84
79
. find_one ( doc ! { "filename" : filename. as_ref( ) } , options)
85
80
. await ?
86
81
{
87
82
Some ( fcd) => fcd,
88
83
None => {
89
- return Err ( ErrorKind :: InvalidArgument {
90
- message : format ! ( "couldn't find file with name {}" , & filename. as_ref( ) ) ,
84
+ if self
85
+ . files ( )
86
+ . find_one ( doc ! { "filename" : filename. as_ref( ) } , None )
87
+ . await ?
88
+ . is_some ( )
89
+ {
90
+ return Err (
91
+ ErrorKind :: GridFs ( GridFsErrorKind :: RevisionNotFound { revision } ) . into ( ) ,
92
+ ) ;
93
+ } else {
94
+ return Err ( ErrorKind :: GridFs ( GridFsErrorKind :: FileNotFound {
95
+ identifier : filename. as_ref ( ) . into ( ) ,
96
+ } )
97
+ . into ( ) ) ;
91
98
}
92
- . into ( ) ) ;
93
99
}
94
100
} ;
95
101
@@ -104,7 +110,7 @@ impl GridFsBucket {
104
110
pub async fn download_to_futures_0_3_writer_by_name < T > (
105
111
& self ,
106
112
filename : impl AsRef < str > ,
107
- destination : & mut T ,
113
+ destination : T ,
108
114
options : impl Into < Option < GridFsDownloadByNameOptions > > ,
109
115
) -> Result < ( ) >
110
116
where
@@ -117,7 +123,7 @@ impl GridFsBucket {
117
123
async fn download_to_tokio_writer_common < T > (
118
124
& self ,
119
125
file : FilesCollectionDocument ,
120
- destination : & mut T ,
126
+ mut destination : T ,
121
127
) -> Result < ( ) >
122
128
where
123
129
T : tokio:: io:: AsyncWrite + std:: marker:: Unpin ,
@@ -144,22 +150,17 @@ impl GridFsBucket {
144
150
while cursor. advance ( ) . await ? {
145
151
let chunk = cursor. deserialize_current ( ) ?;
146
152
if chunk. n != n {
147
- return Err ( ErrorKind :: GridFS {
148
- message : format ! ( "missing chunk {} in file" , n) ,
149
- }
150
- . into ( ) ) ;
153
+ return Err ( ErrorKind :: GridFs ( GridFsErrorKind :: MissingChunk { n } ) . into ( ) ) ;
151
154
}
152
155
153
156
let chunk_length = chunk. data . bytes . len ( ) ;
154
157
let expected_length =
155
158
std:: cmp:: min ( total_bytes_expected - chunk_size * n as u64 , chunk_size) ;
156
159
if chunk_length as u64 != expected_length {
157
- return Err ( ErrorKind :: GridFS {
158
- message : format ! (
159
- "chunk {} was expected to be {} bytes but is {} bytes" ,
160
- n, chunk_length, expected_length
161
- ) ,
162
- }
160
+ return Err ( ErrorKind :: GridFs ( GridFsErrorKind :: WrongSizeChunk {
161
+ actual_size : chunk_length as u32 ,
162
+ expected_size : expected_length as u32 ,
163
+ } )
163
164
. into ( ) ) ;
164
165
}
165
166
@@ -169,10 +170,11 @@ impl GridFsBucket {
169
170
170
171
let expected_n =
171
172
total_bytes_expected / chunk_size + u64:: from ( total_bytes_expected % chunk_size != 0 ) ;
172
- if ( n as u64 ) < expected_n {
173
- return Err ( ErrorKind :: GridFS {
174
- message : "missing last chunk in file" . into ( ) ,
175
- }
173
+ if ( n as u64 ) != expected_n {
174
+ return Err ( ErrorKind :: GridFs ( GridFsErrorKind :: WrongNumberOfChunks {
175
+ actual_number : n,
176
+ expected_number : expected_n as u32 ,
177
+ } )
176
178
. into ( ) ) ;
177
179
}
178
180
0 commit comments