/* Copyright 2016-present MongoDB Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Driver.Core.Misc;
namespace MongoDB.Driver.GridFS
{
///
/// Represents a GridFS bucket.
///
public class GridFSBucket : GridFSBucket, IGridFSBucket
{
// private fields
private readonly GridFSBucket _bsonValueBucket;
// constructors
///
/// Initializes a new instance of the class.
///
/// The database.
/// The options.
public GridFSBucket(IMongoDatabase database, GridFSBucketOptions options = null)
: base(database, options)
{
_bsonValueBucket = new GridFSBucket(database, options);
}
// methods
///
/// Deletes a file from GridFS.
///
/// The file id.
/// The cancellation token.
public void Delete(BsonValue id, CancellationToken cancellationToken = default(CancellationToken))
{
_bsonValueBucket.Delete(id, cancellationToken);
}
///
/// Deletes a file from GridFS.
///
/// The file id.
/// The cancellation token.
/// A Task.
public Task DeleteAsync(BsonValue id, CancellationToken cancellationToken = default(CancellationToken))
{
return _bsonValueBucket.DeleteAsync(id, cancellationToken);
}
///
/// Downloads a file stored in GridFS and returns it as a byte array.
///
/// The file id.
/// The options.
/// The cancellation token.
/// A byte array containing the contents of the file stored in GridFS.
public byte[] DownloadAsBytes(BsonValue id, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
{
return _bsonValueBucket.DownloadAsBytes(id, options, cancellationToken);
}
///
/// Downloads a file stored in GridFS and returns it as a byte array.
///
/// The file id.
/// The options.
/// The cancellation token.
/// A Task whose result is a byte array containing the contents of the file stored in GridFS.
public Task DownloadAsBytesAsync(BsonValue id, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
{
return _bsonValueBucket.DownloadAsBytesAsync(id, options, cancellationToken);
}
///
/// Downloads a file stored in GridFS and writes the contents to a stream.
///
/// The file id.
/// The destination.
/// The options.
/// The cancellation token.
public void DownloadToStream(BsonValue id, Stream destination, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
{
_bsonValueBucket.DownloadToStream(id, destination, options, cancellationToken);
}
///
/// Downloads a file stored in GridFS and writes the contents to a stream.
///
/// The file id.
/// The destination.
/// The options.
/// The cancellation token.
/// A Task.
public Task DownloadToStreamAsync(BsonValue id, Stream destination, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
{
return _bsonValueBucket.DownloadToStreamAsync(id, destination, options, cancellationToken);
}
///
public IAsyncCursor Find(FilterDefinition filter, GridFSFindOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(filter, nameof(filter));
var wrappedFilter = WrapFilter(filter);
var wrappedOptions = WrapFindOptions(options);
var cursor = base.Find(wrappedFilter, wrappedOptions, cancellationToken);
return new BatchTransformingAsyncCursor, GridFSFileInfo>(cursor, TransformFileInfos);
}
///
public async Task> FindAsync(FilterDefinition filter, GridFSFindOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
{
Ensure.IsNotNull(filter, nameof(filter));
var wrappedFilter = WrapFilter(filter);
var wrappedOptions = WrapFindOptions(options);
var cursor = await base.FindAsync(wrappedFilter, wrappedOptions, cancellationToken).ConfigureAwait(false);
return new BatchTransformingAsyncCursor, GridFSFileInfo>(cursor, TransformFileInfos);
}
///
/// Opens a Stream that can be used by the application to read data from a GridFS file.
///
/// The file id.
/// The options.
/// The cancellation token.
/// A Stream.
public GridFSDownloadStream OpenDownloadStream(BsonValue id, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
{
var wrappedStream = _bsonValueBucket.OpenDownloadStream(id, options, cancellationToken);
return new GridFSDownloadStream(wrappedStream);
}
///
/// Opens a Stream that can be used by the application to read data from a GridFS file.
///
/// The file id.
/// The options.
/// The cancellation token.
/// A Task whose result is a Stream.
public async Task OpenDownloadStreamAsync(BsonValue id, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
{
var wrappedStream = await _bsonValueBucket.OpenDownloadStreamAsync(id, options, cancellationToken).ConfigureAwait(false);
return new GridFSDownloadStream(wrappedStream);
}
///
public GridFSUploadStream OpenUploadStream(string filename, GridFSUploadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
{
var id = ObjectId.GenerateNewId();
var wrappedStream = base.OpenUploadStream(id, filename, options, cancellationToken);
return new GridFSUploadStream(wrappedStream);
}
///
public async Task OpenUploadStreamAsync(string filename, GridFSUploadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
{
var id = ObjectId.GenerateNewId();
var wrappedStream = await base.OpenUploadStreamAsync(id, filename, options, cancellationToken).ConfigureAwait(false);
return new GridFSUploadStream(wrappedStream);
}
///
/// Renames a GridFS file.
///
/// The file id.
/// The new filename.
/// The cancellation token.
public void Rename(BsonValue id, string newFilename, CancellationToken cancellationToken = default(CancellationToken))
{
_bsonValueBucket.Rename(id, newFilename, cancellationToken);
}
///
/// Renames a GridFS file.
///
/// The file id.
/// The new filename.
/// The cancellation token.
/// A Task.
public Task RenameAsync(BsonValue id, string newFilename, CancellationToken cancellationToken = default(CancellationToken))
{
return _bsonValueBucket.RenameAsync(id, newFilename, cancellationToken);
}
///
public ObjectId UploadFromBytes(string filename, byte[] source, GridFSUploadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
{
var id = ObjectId.GenerateNewId();
UploadFromBytes(id, filename, source, options, cancellationToken);
return id;
}
///
public async Task UploadFromBytesAsync(string filename, byte[] source, GridFSUploadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
{
var id = ObjectId.GenerateNewId();
await UploadFromBytesAsync(id, filename, source, options, cancellationToken).ConfigureAwait(false);
return id;
}
///
public ObjectId UploadFromStream(string filename, Stream source, GridFSUploadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
{
var id = ObjectId.GenerateNewId();
UploadFromStream(id, filename, source, options, cancellationToken);
return id;
}
///
public async Task UploadFromStreamAsync(string filename, Stream source, GridFSUploadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
{
var id = ObjectId.GenerateNewId();
await UploadFromStreamAsync(id, filename, source, options, cancellationToken).ConfigureAwait(false);
return id;
}
// private methods
private IEnumerable TransformFileInfos(IEnumerable> fileInfos)
{
return fileInfos.Select(fi => new GridFSFileInfo(fi.BackingDocument));
}
private FilterDefinition> WrapFilter(FilterDefinition filter)
{
var renderedFilter = filter.Render(GridFSFileInfoSerializer.Instance, BsonSerializer.SerializerRegistry);
return new BsonDocumentFilterDefinition>(renderedFilter);
}
private GridFSFindOptions WrapFindOptions(GridFSFindOptions options)
{
if (options != null)
{
var renderedSort = options.Sort == null ? null : options.Sort.Render(GridFSFileInfoSerializer.Instance, BsonSerializer.SerializerRegistry);
var wrappedSort = renderedSort == null ? null : new BsonDocumentSortDefinition>(renderedSort);
return new GridFSFindOptions
{
BatchSize = options.BatchSize,
Limit = options.Limit,
MaxTime = options.MaxTime,
NoCursorTimeout = options.NoCursorTimeout,
Skip = options.Skip,
Sort = wrappedSort
};
}
else
{
return null;
}
}
}
}