GridFSBucketCompat.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /* Copyright 2016-present MongoDB Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System;
  16. using System.Collections.Generic;
  17. using System.IO;
  18. using System.Linq;
  19. using System.Threading;
  20. using System.Threading.Tasks;
  21. using MongoDB.Bson;
  22. using MongoDB.Bson.Serialization;
  23. using MongoDB.Driver.Core.Misc;
  24. namespace MongoDB.Driver.GridFS
  25. {
  26. /// <summary>
  27. /// Represents a GridFS bucket.
  28. /// </summary>
  29. public class GridFSBucket : GridFSBucket<ObjectId>, IGridFSBucket
  30. {
  31. // private fields
  32. private readonly GridFSBucket<BsonValue> _bsonValueBucket;
  33. // constructors
  34. /// <summary>
  35. /// Initializes a new instance of the <see cref="GridFSBucket" /> class.
  36. /// </summary>
  37. /// <param name="database">The database.</param>
  38. /// <param name="options">The options.</param>
  39. public GridFSBucket(IMongoDatabase database, GridFSBucketOptions options = null)
  40. : base(database, options)
  41. {
  42. _bsonValueBucket = new GridFSBucket<BsonValue>(database, options);
  43. }
  44. // methods
  45. /// <summary>
  46. /// Deletes a file from GridFS.
  47. /// </summary>
  48. /// <param name="id">The file id.</param>
  49. /// <param name="cancellationToken">The cancellation token.</param>
  50. public void Delete(BsonValue id, CancellationToken cancellationToken = default(CancellationToken))
  51. {
  52. _bsonValueBucket.Delete(id, cancellationToken);
  53. }
  54. /// <summary>
  55. /// Deletes a file from GridFS.
  56. /// </summary>
  57. /// <param name="id">The file id.</param>
  58. /// <param name="cancellationToken">The cancellation token.</param>
  59. /// <returns>A Task.</returns>
  60. public Task DeleteAsync(BsonValue id, CancellationToken cancellationToken = default(CancellationToken))
  61. {
  62. return _bsonValueBucket.DeleteAsync(id, cancellationToken);
  63. }
  64. /// <summary>
  65. /// Downloads a file stored in GridFS and returns it as a byte array.
  66. /// </summary>
  67. /// <param name="id">The file id.</param>
  68. /// <param name="options">The options.</param>
  69. /// <param name="cancellationToken">The cancellation token.</param>
  70. /// <returns>A byte array containing the contents of the file stored in GridFS.</returns>
  71. public byte[] DownloadAsBytes(BsonValue id, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
  72. {
  73. return _bsonValueBucket.DownloadAsBytes(id, options, cancellationToken);
  74. }
  75. /// <summary>
  76. /// Downloads a file stored in GridFS and returns it as a byte array.
  77. /// </summary>
  78. /// <param name="id">The file id.</param>
  79. /// <param name="options">The options.</param>
  80. /// <param name="cancellationToken">The cancellation token.</param>
  81. /// <returns>A Task whose result is a byte array containing the contents of the file stored in GridFS.</returns>
  82. public Task<byte[]> DownloadAsBytesAsync(BsonValue id, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
  83. {
  84. return _bsonValueBucket.DownloadAsBytesAsync(id, options, cancellationToken);
  85. }
  86. /// <summary>
  87. /// Downloads a file stored in GridFS and writes the contents to a stream.
  88. /// </summary>
  89. /// <param name="id">The file id.</param>
  90. /// <param name="destination">The destination.</param>
  91. /// <param name="options">The options.</param>
  92. /// <param name="cancellationToken">The cancellation token.</param>
  93. public void DownloadToStream(BsonValue id, Stream destination, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
  94. {
  95. _bsonValueBucket.DownloadToStream(id, destination, options, cancellationToken);
  96. }
  97. /// <summary>
  98. /// Downloads a file stored in GridFS and writes the contents to a stream.
  99. /// </summary>
  100. /// <param name="id">The file id.</param>
  101. /// <param name="destination">The destination.</param>
  102. /// <param name="options">The options.</param>
  103. /// <param name="cancellationToken">The cancellation token.</param>
  104. /// <returns>A Task.</returns>
  105. public Task DownloadToStreamAsync(BsonValue id, Stream destination, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
  106. {
  107. return _bsonValueBucket.DownloadToStreamAsync(id, destination, options, cancellationToken);
  108. }
  109. /// <inheritdoc />
  110. public IAsyncCursor<GridFSFileInfo> Find(FilterDefinition<GridFSFileInfo> filter, GridFSFindOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
  111. {
  112. Ensure.IsNotNull(filter, nameof(filter));
  113. var wrappedFilter = WrapFilter(filter);
  114. var wrappedOptions = WrapFindOptions(options);
  115. var cursor = base.Find(wrappedFilter, wrappedOptions, cancellationToken);
  116. return new BatchTransformingAsyncCursor<GridFSFileInfo<ObjectId>, GridFSFileInfo>(cursor, TransformFileInfos);
  117. }
  118. /// <inheritdoc />
  119. public async Task<IAsyncCursor<GridFSFileInfo>> FindAsync(FilterDefinition<GridFSFileInfo> filter, GridFSFindOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
  120. {
  121. Ensure.IsNotNull(filter, nameof(filter));
  122. var wrappedFilter = WrapFilter(filter);
  123. var wrappedOptions = WrapFindOptions(options);
  124. var cursor = await base.FindAsync(wrappedFilter, wrappedOptions, cancellationToken).ConfigureAwait(false);
  125. return new BatchTransformingAsyncCursor<GridFSFileInfo<ObjectId>, GridFSFileInfo>(cursor, TransformFileInfos);
  126. }
  127. /// <summary>
  128. /// Opens a Stream that can be used by the application to read data from a GridFS file.
  129. /// </summary>
  130. /// <param name="id">The file id.</param>
  131. /// <param name="options">The options.</param>
  132. /// <param name="cancellationToken">The cancellation token.</param>
  133. /// <returns>A Stream.</returns>
  134. public GridFSDownloadStream OpenDownloadStream(BsonValue id, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
  135. {
  136. var wrappedStream = _bsonValueBucket.OpenDownloadStream(id, options, cancellationToken);
  137. return new GridFSDownloadStream(wrappedStream);
  138. }
  139. /// <summary>
  140. /// Opens a Stream that can be used by the application to read data from a GridFS file.
  141. /// </summary>
  142. /// <param name="id">The file id.</param>
  143. /// <param name="options">The options.</param>
  144. /// <param name="cancellationToken">The cancellation token.</param>
  145. /// <returns>A Task whose result is a Stream.</returns>
  146. public async Task<GridFSDownloadStream> OpenDownloadStreamAsync(BsonValue id, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
  147. {
  148. var wrappedStream = await _bsonValueBucket.OpenDownloadStreamAsync(id, options, cancellationToken).ConfigureAwait(false);
  149. return new GridFSDownloadStream(wrappedStream);
  150. }
  151. /// <inheritdoc />
  152. public GridFSUploadStream OpenUploadStream(string filename, GridFSUploadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
  153. {
  154. var id = ObjectId.GenerateNewId();
  155. var wrappedStream = base.OpenUploadStream(id, filename, options, cancellationToken);
  156. return new GridFSUploadStream(wrappedStream);
  157. }
  158. /// <inheritdoc />
  159. public async Task<GridFSUploadStream> OpenUploadStreamAsync(string filename, GridFSUploadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
  160. {
  161. var id = ObjectId.GenerateNewId();
  162. var wrappedStream = await base.OpenUploadStreamAsync(id, filename, options, cancellationToken).ConfigureAwait(false);
  163. return new GridFSUploadStream(wrappedStream);
  164. }
  165. /// <summary>
  166. /// Renames a GridFS file.
  167. /// </summary>
  168. /// <param name="id">The file id.</param>
  169. /// <param name="newFilename">The new filename.</param>
  170. /// <param name="cancellationToken">The cancellation token.</param>
  171. public void Rename(BsonValue id, string newFilename, CancellationToken cancellationToken = default(CancellationToken))
  172. {
  173. _bsonValueBucket.Rename(id, newFilename, cancellationToken);
  174. }
  175. /// <summary>
  176. /// Renames a GridFS file.
  177. /// </summary>
  178. /// <param name="id">The file id.</param>
  179. /// <param name="newFilename">The new filename.</param>
  180. /// <param name="cancellationToken">The cancellation token.</param>
  181. /// <returns>A Task.</returns>
  182. public Task RenameAsync(BsonValue id, string newFilename, CancellationToken cancellationToken = default(CancellationToken))
  183. {
  184. return _bsonValueBucket.RenameAsync(id, newFilename, cancellationToken);
  185. }
  186. /// <inheritdoc />
  187. public ObjectId UploadFromBytes(string filename, byte[] source, GridFSUploadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
  188. {
  189. var id = ObjectId.GenerateNewId();
  190. UploadFromBytes(id, filename, source, options, cancellationToken);
  191. return id;
  192. }
  193. /// <inheritdoc />
  194. public async Task<ObjectId> UploadFromBytesAsync(string filename, byte[] source, GridFSUploadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
  195. {
  196. var id = ObjectId.GenerateNewId();
  197. await UploadFromBytesAsync(id, filename, source, options, cancellationToken).ConfigureAwait(false);
  198. return id;
  199. }
  200. /// <inheritdoc />
  201. public ObjectId UploadFromStream(string filename, Stream source, GridFSUploadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
  202. {
  203. var id = ObjectId.GenerateNewId();
  204. UploadFromStream(id, filename, source, options, cancellationToken);
  205. return id;
  206. }
  207. /// <inheritdoc />
  208. public async Task<ObjectId> UploadFromStreamAsync(string filename, Stream source, GridFSUploadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
  209. {
  210. var id = ObjectId.GenerateNewId();
  211. await UploadFromStreamAsync(id, filename, source, options, cancellationToken).ConfigureAwait(false);
  212. return id;
  213. }
  214. // private methods
  215. private IEnumerable<GridFSFileInfo> TransformFileInfos(IEnumerable<GridFSFileInfo<ObjectId>> fileInfos)
  216. {
  217. return fileInfos.Select(fi => new GridFSFileInfo(fi.BackingDocument));
  218. }
  219. private FilterDefinition<GridFSFileInfo<ObjectId>> WrapFilter(FilterDefinition<GridFSFileInfo> filter)
  220. {
  221. var renderedFilter = filter.Render(GridFSFileInfoSerializer.Instance, BsonSerializer.SerializerRegistry);
  222. return new BsonDocumentFilterDefinition<GridFSFileInfo<ObjectId>>(renderedFilter);
  223. }
  224. private GridFSFindOptions<ObjectId> WrapFindOptions(GridFSFindOptions options)
  225. {
  226. if (options != null)
  227. {
  228. var renderedSort = options.Sort == null ? null : options.Sort.Render(GridFSFileInfoSerializer.Instance, BsonSerializer.SerializerRegistry);
  229. var wrappedSort = renderedSort == null ? null : new BsonDocumentSortDefinition<GridFSFileInfo<ObjectId>>(renderedSort);
  230. return new GridFSFindOptions<ObjectId>
  231. {
  232. BatchSize = options.BatchSize,
  233. Limit = options.Limit,
  234. MaxTime = options.MaxTime,
  235. NoCursorTimeout = options.NoCursorTimeout,
  236. Skip = options.Skip,
  237. Sort = wrappedSort
  238. };
  239. }
  240. else
  241. {
  242. return null;
  243. }
  244. }
  245. }
  246. }