-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
GridFSBucketCompat.cs
273 lines (249 loc) · 12.9 KB
/
GridFSBucketCompat.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/* 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
{
/// <summary>
/// Represents a GridFS bucket.
/// </summary>
public class GridFSBucket : GridFSBucket<ObjectId>, IGridFSBucket
{
// private fields
private readonly GridFSBucket<BsonValue> _bsonValueBucket;
// constructors
/// <summary>
/// Initializes a new instance of the <see cref="GridFSBucket" /> class.
/// </summary>
/// <param name="database">The database.</param>
/// <param name="options">The options.</param>
public GridFSBucket(IMongoDatabase database, GridFSBucketOptions options = null)
: base(database, options)
{
_bsonValueBucket = new GridFSBucket<BsonValue>(database, options);
}
// methods
/// <summary>
/// Deletes a file from GridFS.
/// </summary>
/// <param name="id">The file id.</param>
/// <param name="cancellationToken">The cancellation token.</param>
public void Delete(BsonValue id, CancellationToken cancellationToken = default(CancellationToken))
{
_bsonValueBucket.Delete(id, cancellationToken);
}
/// <summary>
/// Deletes a file from GridFS.
/// </summary>
/// <param name="id">The file id.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>A Task.</returns>
public Task DeleteAsync(BsonValue id, CancellationToken cancellationToken = default(CancellationToken))
{
return _bsonValueBucket.DeleteAsync(id, cancellationToken);
}
/// <summary>
/// Downloads a file stored in GridFS and returns it as a byte array.
/// </summary>
/// <param name="id">The file id.</param>
/// <param name="options">The options.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>A byte array containing the contents of the file stored in GridFS.</returns>
public byte[] DownloadAsBytes(BsonValue id, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
{
return _bsonValueBucket.DownloadAsBytes(id, options, cancellationToken);
}
/// <summary>
/// Downloads a file stored in GridFS and returns it as a byte array.
/// </summary>
/// <param name="id">The file id.</param>
/// <param name="options">The options.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>A Task whose result is a byte array containing the contents of the file stored in GridFS.</returns>
public Task<byte[]> DownloadAsBytesAsync(BsonValue id, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
{
return _bsonValueBucket.DownloadAsBytesAsync(id, options, cancellationToken);
}
/// <summary>
/// Downloads a file stored in GridFS and writes the contents to a stream.
/// </summary>
/// <param name="id">The file id.</param>
/// <param name="destination">The destination.</param>
/// <param name="options">The options.</param>
/// <param name="cancellationToken">The cancellation token.</param>
public void DownloadToStream(BsonValue id, Stream destination, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
{
_bsonValueBucket.DownloadToStream(id, destination, options, cancellationToken);
}
/// <summary>
/// Downloads a file stored in GridFS and writes the contents to a stream.
/// </summary>
/// <param name="id">The file id.</param>
/// <param name="destination">The destination.</param>
/// <param name="options">The options.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>A Task.</returns>
public Task DownloadToStreamAsync(BsonValue id, Stream destination, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
{
return _bsonValueBucket.DownloadToStreamAsync(id, destination, options, cancellationToken);
}
/// <inheritdoc />
public IAsyncCursor<GridFSFileInfo> Find(FilterDefinition<GridFSFileInfo> 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<ObjectId>, GridFSFileInfo>(cursor, TransformFileInfos);
}
/// <inheritdoc />
public async Task<IAsyncCursor<GridFSFileInfo>> FindAsync(FilterDefinition<GridFSFileInfo> 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<ObjectId>, GridFSFileInfo>(cursor, TransformFileInfos);
}
/// <summary>
/// Opens a Stream that can be used by the application to read data from a GridFS file.
/// </summary>
/// <param name="id">The file id.</param>
/// <param name="options">The options.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>A Stream.</returns>
public GridFSDownloadStream OpenDownloadStream(BsonValue id, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
{
var wrappedStream = _bsonValueBucket.OpenDownloadStream(id, options, cancellationToken);
return new GridFSDownloadStream(wrappedStream);
}
/// <summary>
/// Opens a Stream that can be used by the application to read data from a GridFS file.
/// </summary>
/// <param name="id">The file id.</param>
/// <param name="options">The options.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>A Task whose result is a Stream.</returns>
public async Task<GridFSDownloadStream> OpenDownloadStreamAsync(BsonValue id, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
{
var wrappedStream = await _bsonValueBucket.OpenDownloadStreamAsync(id, options, cancellationToken).ConfigureAwait(false);
return new GridFSDownloadStream(wrappedStream);
}
/// <inheritdoc />
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);
}
/// <inheritdoc />
public async Task<GridFSUploadStream> 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);
}
/// <summary>
/// Renames a GridFS file.
/// </summary>
/// <param name="id">The file id.</param>
/// <param name="newFilename">The new filename.</param>
/// <param name="cancellationToken">The cancellation token.</param>
public void Rename(BsonValue id, string newFilename, CancellationToken cancellationToken = default(CancellationToken))
{
_bsonValueBucket.Rename(id, newFilename, cancellationToken);
}
/// <summary>
/// Renames a GridFS file.
/// </summary>
/// <param name="id">The file id.</param>
/// <param name="newFilename">The new filename.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>A Task.</returns>
public Task RenameAsync(BsonValue id, string newFilename, CancellationToken cancellationToken = default(CancellationToken))
{
return _bsonValueBucket.RenameAsync(id, newFilename, cancellationToken);
}
/// <inheritdoc />
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;
}
/// <inheritdoc />
public async Task<ObjectId> 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;
}
/// <inheritdoc />
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;
}
/// <inheritdoc />
public async Task<ObjectId> 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<GridFSFileInfo> TransformFileInfos(IEnumerable<GridFSFileInfo<ObjectId>> fileInfos)
{
return fileInfos.Select(fi => new GridFSFileInfo(fi.BackingDocument));
}
private FilterDefinition<GridFSFileInfo<ObjectId>> WrapFilter(FilterDefinition<GridFSFileInfo> filter)
{
var linqProvider = Database.Client.Settings.LinqProvider;
var renderedFilter = filter.Render(GridFSFileInfoSerializer.Instance, BsonSerializer.SerializerRegistry, linqProvider);
return new BsonDocumentFilterDefinition<GridFSFileInfo<ObjectId>>(renderedFilter);
}
private GridFSFindOptions<ObjectId> WrapFindOptions(GridFSFindOptions options)
{
if (options != null)
{
var linqProvider = Database.Client.Settings.LinqProvider;
var renderedSort = options.Sort == null ? null : options.Sort.Render(GridFSFileInfoSerializer.Instance, BsonSerializer.SerializerRegistry, linqProvider);
var wrappedSort = renderedSort == null ? null : new BsonDocumentSortDefinition<GridFSFileInfo<ObjectId>>(renderedSort);
return new GridFSFindOptions<ObjectId>
{
AllowDiskUse = options.AllowDiskUse,
BatchSize = options.BatchSize,
Limit = options.Limit,
MaxTime = options.MaxTime,
NoCursorTimeout = options.NoCursorTimeout,
Skip = options.Skip,
Sort = wrappedSort
};
}
else
{
return null;
}
}
}
}