uploader.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // Copyright 2017 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. #pragma once
  15. #include <cstddef>
  16. #include <cstdint>
  17. #include <memory>
  18. #include <bsoncxx/document/value.hpp>
  19. #include <bsoncxx/stdx/optional.hpp>
  20. #include <bsoncxx/stdx/string_view.hpp>
  21. #include <bsoncxx/types/bson_value/view.hpp>
  22. #include <bsoncxx/view_or_value.hpp>
  23. #include <mongocxx/client_session.hpp>
  24. #include <mongocxx/collection.hpp>
  25. #include <mongocxx/result/gridfs/upload.hpp>
  26. #include <mongocxx/stdx.hpp>
  27. #include <mongocxx/config/prelude.hpp>
  28. namespace mongocxx {
  29. MONGOCXX_INLINE_NAMESPACE_BEGIN
  30. namespace gridfs {
  31. ///
  32. /// Class used to upload a GridFS file.
  33. ///
  34. class MONGOCXX_API uploader {
  35. public:
  36. ///
  37. /// Default constructs an uploader object. The uploader is equivalent to the state of a moved
  38. /// from uploader. The only valid actions to take with a default constructed uploader are to
  39. /// assign to it, or destroy it.
  40. ///
  41. uploader() noexcept;
  42. ///
  43. /// Move constructs an uploader.
  44. ///
  45. uploader(uploader&&) noexcept;
  46. ///
  47. /// Move assigns an uploader.
  48. ///
  49. uploader& operator=(uploader&&) noexcept;
  50. uploader(const uploader&) = delete;
  51. uploader& operator=(const uploader&) = delete;
  52. ///
  53. /// Destroys an uploader.
  54. ///
  55. ~uploader();
  56. ///
  57. /// Returns true if the uploader is valid, meaning it was not default constructed or moved from.
  58. ///
  59. explicit operator bool() const noexcept;
  60. ///
  61. /// Writes a specified number of bytes to a GridFS file.
  62. ///
  63. /// @param bytes
  64. /// A pointer to the bytes to write.
  65. ///
  66. /// @param length
  67. /// The number of bytes to write.
  68. ///
  69. /// @throws mongocxx::logic_error if the upload stream was already closed.
  70. ///
  71. /// @throws mongocxx::bulk_write_exception
  72. /// if an error occurs when writing chunk data to the database.
  73. ///
  74. /// @throws mongocxx::gridfs_exception
  75. /// if the uploader requires more than 2^31-1 chunks to store the file at the requested chunk
  76. /// size.
  77. ///
  78. void write(const std::uint8_t* bytes, std::size_t length);
  79. ///
  80. /// Closes the uploader stream.
  81. ///
  82. /// @throws mongocxx::logic_error if the upload stream was already closed.
  83. ///
  84. /// @throws mongocxx::bulk_write_exception
  85. /// if an error occurs when writing chunk data or file metadata to the database.
  86. ///
  87. /// @throws mongocxx::gridfs_exception
  88. /// if the uploader requires more than 2^31-1 chunks to store the file at the requested chunk
  89. /// size.
  90. ///
  91. result::gridfs::upload close();
  92. ///
  93. /// Aborts uploading the file.
  94. ///
  95. /// @throws mongocxx::logic_error if the upload stream was already closed.
  96. ///
  97. /// @throws mongocxx::bulk_write_exception
  98. /// if an error occurs when removing chunk data from the database.
  99. ///
  100. void abort();
  101. ///
  102. /// Gets the chunk size of the file being uploaded.
  103. ///
  104. /// @return
  105. /// The chunk size in bytes.
  106. ///
  107. std::int32_t chunk_size() const;
  108. private:
  109. friend class bucket;
  110. //
  111. // Constructs a new uploader stream.
  112. //
  113. // @param session
  114. // The client session to use for upload operations.
  115. //
  116. // @param id
  117. // The id of the GridFS file being uploaded.
  118. //
  119. // @param files
  120. // The files collection of the bucket receiving the file.
  121. //
  122. // @param chunks
  123. // The chunks collection of the bucket receiving the file.
  124. //
  125. // @param chunk_size
  126. // The size in bytes of the chunks being uploaded.
  127. //
  128. // @param metadata
  129. // Optional metadata field of the files collection document.
  130. //
  131. MONGOCXX_PRIVATE uploader(const client_session* session,
  132. bsoncxx::types::bson_value::view id,
  133. stdx::string_view filename,
  134. collection files,
  135. collection chunks,
  136. std::int32_t chunk_size,
  137. stdx::optional<bsoncxx::document::view_or_value> metadata = {});
  138. MONGOCXX_PRIVATE void finish_chunk();
  139. MONGOCXX_PRIVATE void flush_chunks();
  140. class MONGOCXX_PRIVATE impl;
  141. MONGOCXX_PRIVATE impl& _get_impl();
  142. MONGOCXX_PRIVATE const impl& _get_impl() const;
  143. std::unique_ptr<impl> _impl;
  144. };
  145. } // namespace gridfs
  146. MONGOCXX_INLINE_NAMESPACE_END
  147. } // namespace mongocxx