IGridFSBucket.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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.IO;
  17. using System.Threading;
  18. using System.Threading.Tasks;
  19. using MongoDB.Bson;
  20. namespace MongoDB.Driver.GridFS
  21. {
  22. /// <summary>
  23. /// Represents a GridFS system bucket.
  24. /// </summary>
  25. /// <typeparam name="TFileId">The type of the file identifier.</typeparam>
  26. public interface IGridFSBucket<TFileId>
  27. {
  28. // properties
  29. /// <summary>
  30. /// Gets the database where the GridFS files are stored.
  31. /// </summary>
  32. /// <value>
  33. /// The database.
  34. /// </value>
  35. IMongoDatabase Database { get; }
  36. /// <summary>
  37. /// Gets the options.
  38. /// </summary>
  39. /// <value>
  40. /// The options.
  41. /// </value>
  42. ImmutableGridFSBucketOptions Options { get; }
  43. // methods
  44. /// <summary>
  45. /// Deletes a file from GridFS.
  46. /// </summary>
  47. /// <param name="id">The file id.</param>
  48. /// <param name="cancellationToken">The cancellation token.</param>
  49. void Delete(TFileId id, CancellationToken cancellationToken = default(CancellationToken));
  50. /// <summary>
  51. /// Deletes a file from GridFS.
  52. /// </summary>
  53. /// <param name="id">The file id.</param>
  54. /// <param name="cancellationToken">The cancellation token.</param>
  55. /// <returns>A Task.</returns>
  56. Task DeleteAsync(TFileId id, CancellationToken cancellationToken = default(CancellationToken));
  57. /// <summary>
  58. /// Downloads a file stored in GridFS and returns it as a byte array.
  59. /// </summary>
  60. /// <param name="id">The file id.</param>
  61. /// <param name="options">The options.</param>
  62. /// <param name="cancellationToken">The cancellation token.</param>
  63. /// <returns>The contents of the file stored in GridFS.</returns>
  64. byte[] DownloadAsBytes(TFileId id, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken));
  65. /// <summary>
  66. /// Downloads a file stored in GridFS and returns it as a byte array.
  67. /// </summary>
  68. /// <param name="id">The file id.</param>
  69. /// <param name="options">The options.</param>
  70. /// <param name="cancellationToken">The cancellation token.</param>
  71. /// <returns>A Task whose result is a byte array containing the contents of the file stored in GridFS.</returns>
  72. Task<byte[]> DownloadAsBytesAsync(TFileId id, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken));
  73. /// <summary>
  74. /// Downloads a file stored in GridFS and returns it as a byte array.
  75. /// </summary>
  76. /// <param name="filename">The filename.</param>
  77. /// <param name="options">The options.</param>
  78. /// <param name="cancellationToken">The cancellation token.</param>
  79. /// <returns>A byte array containing the contents of the file stored in GridFS.</returns>
  80. byte[] DownloadAsBytesByName(string filename, GridFSDownloadByNameOptions options = null, CancellationToken cancellationToken = default(CancellationToken));
  81. /// <summary>
  82. /// Downloads a file stored in GridFS and returns it as a byte array.
  83. /// </summary>
  84. /// <param name="filename">The filename.</param>
  85. /// <param name="options">The options.</param>
  86. /// <param name="cancellationToken">The cancellation token.</param>
  87. /// <returns>A Task whose result is a byte array containing the contents of the file stored in GridFS.</returns>
  88. Task<byte[]> DownloadAsBytesByNameAsync(string filename, GridFSDownloadByNameOptions options = null, CancellationToken cancellationToken = default(CancellationToken));
  89. /// <summary>
  90. /// Downloads a file stored in GridFS and writes the contents to a stream.
  91. /// </summary>
  92. /// <param name="id">The file id.</param>
  93. /// <param name="destination">The destination.</param>
  94. /// <param name="options">The options.</param>
  95. /// <param name="cancellationToken">The cancellation token.</param>
  96. void DownloadToStream(TFileId id, Stream destination, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken));
  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. Task DownloadToStreamAsync(TFileId id, Stream destination, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken));
  106. /// <summary>
  107. /// Downloads a file stored in GridFS and writes the contents to a stream.
  108. /// </summary>
  109. /// <param name="filename">The filename.</param>
  110. /// <param name="destination">The destination.</param>
  111. /// <param name="options">The options.</param>
  112. /// <param name="cancellationToken">The cancellation token.</param>
  113. void DownloadToStreamByName(string filename, Stream destination, GridFSDownloadByNameOptions options = null, CancellationToken cancellationToken = default(CancellationToken));
  114. /// <summary>
  115. /// Downloads a file stored in GridFS and writes the contents to a stream.
  116. /// </summary>
  117. /// <param name="filename">The filename.</param>
  118. /// <param name="destination">The destination.</param>
  119. /// <param name="options">The options.</param>
  120. /// <param name="cancellationToken">The cancellation token.</param>
  121. /// <returns>A Task.</returns>
  122. Task DownloadToStreamByNameAsync(string filename, Stream destination, GridFSDownloadByNameOptions options = null, CancellationToken cancellationToken = default(CancellationToken));
  123. /// <summary>
  124. /// Drops the files and chunks collections associated with this GridFS bucket.
  125. /// </summary>
  126. /// <param name="cancellationToken">The cancellation token.</param>
  127. void Drop(CancellationToken cancellationToken = default(CancellationToken));
  128. /// <summary>
  129. /// Drops the files and chunks collections associated with this GridFS bucket.
  130. /// </summary>
  131. /// <param name="cancellationToken">The cancellation token.</param>
  132. /// <returns>A Task.</returns>
  133. Task DropAsync(CancellationToken cancellationToken = default(CancellationToken));
  134. /// <summary>
  135. /// Finds matching entries from the files collection.
  136. /// </summary>
  137. /// <param name="filter">The filter.</param>
  138. /// <param name="options">The options.</param>
  139. /// <param name="cancellationToken">The cancellation token.</param>
  140. /// <returns>A cursor of files collection documents.</returns>
  141. IAsyncCursor<GridFSFileInfo<TFileId>> Find(FilterDefinition<GridFSFileInfo<TFileId>> filter, GridFSFindOptions<TFileId> options = null, CancellationToken cancellationToken = default(CancellationToken));
  142. /// <summary>
  143. /// Finds matching entries from the files collection.
  144. /// </summary>
  145. /// <param name="filter">The filter.</param>
  146. /// <param name="options">The options.</param>
  147. /// <param name="cancellationToken">The cancellation token.</param>
  148. /// <returns>A Task whose result is a cursor of files collection documents.</returns>
  149. Task<IAsyncCursor<GridFSFileInfo<TFileId>>> FindAsync(FilterDefinition<GridFSFileInfo<TFileId>> filter, GridFSFindOptions<TFileId> options = null, CancellationToken cancellationToken = default(CancellationToken));
  150. /// <summary>
  151. /// Opens a Stream that can be used by the application to read data from a GridFS file.
  152. /// </summary>
  153. /// <param name="id">The file id.</param>
  154. /// <param name="options">The options.</param>
  155. /// <param name="cancellationToken">The cancellation token.</param>
  156. /// <returns>A Stream.</returns>
  157. GridFSDownloadStream<TFileId> OpenDownloadStream(TFileId id, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken));
  158. /// <summary>
  159. /// Opens a Stream that can be used by the application to read data from a GridFS file.
  160. /// </summary>
  161. /// <param name="id">The file id.</param>
  162. /// <param name="options">The options.</param>
  163. /// <param name="cancellationToken">The cancellation token.</param>
  164. /// <returns>A Task whose result is a Stream.</returns>
  165. Task<GridFSDownloadStream<TFileId>> OpenDownloadStreamAsync(TFileId id, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken));
  166. /// <summary>
  167. /// Opens a Stream that can be used by the application to read data from a GridFS file.
  168. /// </summary>
  169. /// <param name="filename">The filename.</param>
  170. /// <param name="options">The options.</param>
  171. /// <param name="cancellationToken">The cancellation token.</param>
  172. /// <returns>A Stream.</returns>
  173. GridFSDownloadStream<TFileId> OpenDownloadStreamByName(string filename, GridFSDownloadByNameOptions options = null, CancellationToken cancellationToken = default(CancellationToken));
  174. /// <summary>
  175. /// Opens a Stream that can be used by the application to read data from a GridFS file.
  176. /// </summary>
  177. /// <param name="filename">The filename.</param>
  178. /// <param name="options">The options.</param>
  179. /// <param name="cancellationToken">The cancellation token.</param>
  180. /// <returns>A Task whose result is a Stream.</returns>
  181. Task<GridFSDownloadStream<TFileId>> OpenDownloadStreamByNameAsync(string filename, GridFSDownloadByNameOptions options = null, CancellationToken cancellationToken = default(CancellationToken));
  182. /// <summary>
  183. /// Opens a Stream that can be used by the application to write data to a GridFS file.
  184. /// </summary>
  185. /// <param name="id">The file id.</param>
  186. /// <param name="filename">The filename.</param>
  187. /// <param name="options">The options.</param>
  188. /// <param name="cancellationToken">The cancellation token.</param>
  189. /// <returns>A Stream.</returns>
  190. GridFSUploadStream<TFileId> OpenUploadStream(TFileId id, string filename, GridFSUploadOptions options = null, CancellationToken cancellationToken = default(CancellationToken));
  191. /// <summary>
  192. /// Opens a Stream that can be used by the application to write data to a GridFS file.
  193. /// </summary>
  194. /// <param name="id">The file id.</param>
  195. /// <param name="filename">The filename.</param>
  196. /// <param name="options">The options.</param>
  197. /// <param name="cancellationToken">The cancellation token.</param>
  198. /// <returns>A Task whose result is a Stream.</returns>
  199. Task<GridFSUploadStream<TFileId>> OpenUploadStreamAsync(TFileId id, string filename, GridFSUploadOptions options = null, CancellationToken cancellationToken = default(CancellationToken));
  200. /// <summary>
  201. /// Renames a GridFS file.
  202. /// </summary>
  203. /// <param name="id">The file id.</param>
  204. /// <param name="newFilename">The new filename.</param>
  205. /// <param name="cancellationToken">The cancellation token.</param>
  206. void Rename(TFileId id, string newFilename, CancellationToken cancellationToken = default(CancellationToken));
  207. /// <summary>
  208. /// Renames a GridFS file.
  209. /// </summary>
  210. /// <param name="id">The file id.</param>
  211. /// <param name="newFilename">The new filename.</param>
  212. /// <param name="cancellationToken">The cancellation token.</param>
  213. /// <returns>A Task.</returns>
  214. Task RenameAsync(TFileId id, string newFilename, CancellationToken cancellationToken = default(CancellationToken));
  215. /// <summary>
  216. /// Uploads a file (or a new revision of a file) to GridFS.
  217. /// </summary>
  218. /// <param name="id">The file id.</param>
  219. /// <param name="filename">The filename.</param>
  220. /// <param name="source">The source.</param>
  221. /// <param name="options">The options.</param>
  222. /// <param name="cancellationToken">The cancellation token.</param>
  223. void UploadFromBytes(TFileId id, string filename, byte[] source, GridFSUploadOptions options = null, CancellationToken cancellationToken = default(CancellationToken));
  224. /// <summary>
  225. /// Uploads a file (or a new revision of a file) to GridFS.
  226. /// </summary>
  227. /// <param name="id">The file id.</param>
  228. /// <param name="filename">The filename.</param>
  229. /// <param name="source">The source.</param>
  230. /// <param name="options">The options.</param>
  231. /// <param name="cancellationToken">The cancellation token.</param>
  232. /// <returns>A Task.</returns>
  233. Task UploadFromBytesAsync(TFileId id, string filename, byte[] source, GridFSUploadOptions options = null, CancellationToken cancellationToken = default(CancellationToken));
  234. /// <summary>
  235. /// Uploads a file (or a new revision of a file) to GridFS.
  236. /// </summary>
  237. /// <param name="id">The file id.</param>
  238. /// <param name="filename">The filename.</param>
  239. /// <param name="source">The source.</param>
  240. /// <param name="options">The options.</param>
  241. /// <param name="cancellationToken">The cancellation token.</param>
  242. /// <returns>The id of the new file.</returns>
  243. void UploadFromStream(TFileId id, string filename, Stream source, GridFSUploadOptions options = null, CancellationToken cancellationToken = default(CancellationToken));
  244. /// <summary>
  245. /// Uploads a file (or a new revision of a file) to GridFS.
  246. /// </summary>
  247. /// <param name="id">The file id.</param>
  248. /// <param name="filename">The filename.</param>
  249. /// <param name="source">The source.</param>
  250. /// <param name="options">The options.</param>
  251. /// <param name="cancellationToken">The cancellation token.</param>
  252. /// <returns>A Task.</returns>
  253. Task UploadFromStreamAsync(TFileId id, string filename, Stream source, GridFSUploadOptions options = null, CancellationToken cancellationToken = default(CancellationToken));
  254. }
  255. }