@@ -17,8 +17,8 @@ use crate::{
17
17
bson:: { doc, oid:: ObjectId , Bson , DateTime , Document , RawBinaryRef } ,
18
18
concern:: { ReadConcern , WriteConcern } ,
19
19
cursor:: Cursor ,
20
- error:: Result ,
21
- options:: SelectionCriteria ,
20
+ error:: { ErrorKind , GridFsErrorKind , GridFsFileIdentifier , Result } ,
21
+ options:: { CollectionOptions , FindOptions , SelectionCriteria } ,
22
22
Collection ,
23
23
Database ,
24
24
} ;
@@ -171,8 +171,19 @@ impl GridFsBucket {
171
171
. as_deref ( )
172
172
. unwrap_or ( DEFAULT_BUCKET_NAME ) ;
173
173
174
- let files = db. collection :: < FilesCollectionDocument > ( & format ! ( "{}.files" , bucket_name) ) ;
175
- let chunks = db. collection :: < Chunk > ( & format ! ( "{}.chunks" , bucket_name) ) ;
174
+ let collection_options = CollectionOptions :: builder ( )
175
+ . read_concern ( options. read_concern . clone ( ) )
176
+ . write_concern ( options. write_concern . clone ( ) )
177
+ . selection_criteria ( options. selection_criteria . clone ( ) )
178
+ . build ( ) ;
179
+ let files = db. collection_with_options :: < FilesCollectionDocument > (
180
+ & format ! ( "{}.files" , bucket_name) ,
181
+ collection_options. clone ( ) ,
182
+ ) ;
183
+ let chunks = db. collection_with_options :: < Chunk > (
184
+ & format ! ( "{}.chunks" , bucket_name) ,
185
+ collection_options,
186
+ ) ;
176
187
177
188
GridFsBucket {
178
189
inner : Arc :: new ( GridFsBucketInner {
@@ -318,28 +329,58 @@ impl GridFsBucket {
318
329
todo ! ( )
319
330
}
320
331
321
- /// Given an `id`, deletes the stored file's files collection document and
322
- /// associated chunks from a [`GridFsBucket`].
323
- pub async fn delete ( & self , id : Bson ) {
324
- todo ! ( )
332
+ /// Deletes the [`FilesCollectionDocument`] with the given `id `and its associated chunks from
333
+ /// this bucket.
334
+ pub async fn delete ( & self , id : Bson ) -> Result < ( ) > {
335
+ let delete_result = self
336
+ . files ( )
337
+ . delete_one ( doc ! { "_id" : id. clone( ) } , None )
338
+ . await ?;
339
+ // Delete chunks regardless of whether a file was found. This will remove any possibly
340
+ // orphaned chunks.
341
+ self . chunks ( )
342
+ . delete_many ( doc ! { "files_id" : id. clone( ) } , None )
343
+ . await ?;
344
+
345
+ if delete_result. deleted_count == 0 {
346
+ return Err ( ErrorKind :: GridFs ( GridFsErrorKind :: FileNotFound {
347
+ identifier : GridFsFileIdentifier :: Id ( id) ,
348
+ } )
349
+ . into ( ) ) ;
350
+ }
351
+
352
+ Ok ( ( ) )
325
353
}
326
354
327
- /// Finds and returns the files collection documents that match the filter.
355
+ /// Finds and returns the [`FilesCollectionDocument`]s within this bucket that match the given
356
+ /// filter.
328
357
pub async fn find (
329
358
& self ,
330
359
filter : Document ,
331
- options : impl Into < Option < GridFsBucketOptions > > ,
360
+ options : impl Into < Option < GridFsFindOptions > > ,
332
361
) -> Result < Cursor < FilesCollectionDocument > > {
333
- todo ! ( )
362
+ let find_options = options. into ( ) . map ( FindOptions :: from) ;
363
+ self . files ( ) . find ( filter, find_options) . await
334
364
}
335
365
336
- /// Renames the stored file with the specified `id`.
337
- pub async fn rename ( & self , id : Bson , new_filename : String ) {
338
- todo ! ( )
366
+ /// Renames the file with the given 'id' to the provided `new_filename`.
367
+ pub async fn rename ( & self , id : Bson , new_filename : impl AsRef < str > ) -> Result < ( ) > {
368
+ self . files ( )
369
+ . update_one (
370
+ doc ! { "_id" : id } ,
371
+ doc ! { "$set" : { "filename" : new_filename. as_ref( ) } } ,
372
+ None ,
373
+ )
374
+ . await ?;
375
+
376
+ Ok ( ( ) )
339
377
}
340
378
341
- /// Drops the files associated with this bucket.
342
- pub async fn drop ( & self ) {
343
- todo ! ( )
379
+ /// Drops all of the files and their associated chunks in this bucket.
380
+ pub async fn drop ( & self ) -> Result < ( ) > {
381
+ self . files ( ) . drop ( None ) . await ?;
382
+ self . chunks ( ) . drop ( None ) . await ?;
383
+
384
+ Ok ( ( ) )
344
385
}
345
386
}
0 commit comments