GridFSUploadStreamCompat.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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.IO;
  16. using System.Threading;
  17. using System.Threading.Tasks;
  18. using MongoDB.Bson;
  19. namespace MongoDB.Driver.GridFS
  20. {
  21. /// <summary>
  22. /// Represents a Stream used by the application to write data to a GridFS file.
  23. /// </summary>
  24. public class GridFSUploadStream : DelegatingStream
  25. {
  26. // fields
  27. private readonly GridFSUploadStream<ObjectId> _wrappedStream;
  28. // constructors
  29. /// <summary>
  30. /// Initializes a new instance of the <see cref="GridFSUploadStream"/> class.
  31. /// </summary>
  32. /// <param name="wrappedStream">The wrapped stream.</param>
  33. internal GridFSUploadStream(GridFSUploadStream<ObjectId> wrappedStream)
  34. : base(wrappedStream)
  35. {
  36. _wrappedStream = wrappedStream;
  37. }
  38. // public properties
  39. /// <summary>
  40. /// Gets the id of the file being added to GridFS.
  41. /// </summary>
  42. /// <value>
  43. /// The id of the file being added to GridFS.
  44. /// </value>
  45. public ObjectId Id
  46. {
  47. get { return _wrappedStream.Id; }
  48. }
  49. // public methods
  50. /// <summary>
  51. /// Aborts an upload operation.
  52. /// </summary>
  53. /// <remarks>
  54. /// Any partial results already written to the server are deleted when Abort is called.
  55. /// </remarks>
  56. /// <param name="cancellationToken">The cancellation token.</param>
  57. public void Abort(CancellationToken cancellationToken = default(CancellationToken))
  58. {
  59. _wrappedStream.Abort(cancellationToken);
  60. }
  61. /// <summary>
  62. /// Aborts an upload operation.
  63. /// </summary>
  64. /// <remarks>
  65. /// Any partial results already written to the server are deleted when AbortAsync is called.
  66. /// </remarks>
  67. /// <param name="cancellationToken">The cancellation token.</param>
  68. /// <returns>A Task.</returns>
  69. public Task AbortAsync(CancellationToken cancellationToken = default(CancellationToken))
  70. {
  71. return _wrappedStream.AbortAsync(cancellationToken);
  72. }
  73. #if NETSTANDARD1_5 || NETSTANDARD1_6
  74. /// <summary>
  75. /// Closes the GridFS stream.
  76. /// </summary>
  77. public virtual void Close()
  78. {
  79. _wrappedStream.Close();
  80. }
  81. #endif
  82. /// <summary>
  83. /// Closes the Stream and completes the upload operation.
  84. /// </summary>
  85. /// <remarks>
  86. /// Any data remaining in the Stream is flushed to the server and the GridFS files collection document is written.
  87. /// </remarks>
  88. /// <param name="cancellationToken">The cancellation token.</param>
  89. public void Close(CancellationToken cancellationToken)
  90. {
  91. _wrappedStream.Close(cancellationToken);
  92. }
  93. /// <summary>
  94. /// Closes the Stream and completes the upload operation.
  95. /// </summary>
  96. /// <remarks>
  97. /// Any data remaining in the Stream is flushed to the server and the GridFS files collection document is written.
  98. /// </remarks>
  99. /// <param name="cancellationToken">The cancellation token.</param>
  100. /// <returns>A Task.</returns>
  101. public Task CloseAsync(CancellationToken cancellationToken = default(CancellationToken))
  102. {
  103. return _wrappedStream.CloseAsync(cancellationToken);
  104. }
  105. }
  106. }