downloader.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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/document/view.hpp>
  20. #include <bsoncxx/stdx/optional.hpp>
  21. #include <bsoncxx/types/bson_value/view.hpp>
  22. #include <mongocxx/cursor.hpp>
  23. #include <mongocxx/stdx.hpp>
  24. #include <mongocxx/config/prelude.hpp>
  25. namespace mongocxx {
  26. MONGOCXX_INLINE_NAMESPACE_BEGIN
  27. namespace gridfs {
  28. ///
  29. /// Class used to download a GridFS file.
  30. ///
  31. class MONGOCXX_API downloader {
  32. public:
  33. ///
  34. /// Default constructs a downloader object. The downloader is equivalent to the state of a moved
  35. /// from downloader. The only valid actions to take with a default constructed downloader are to
  36. /// assign to it, or destroy it.
  37. ///
  38. downloader() noexcept;
  39. ///
  40. /// Move constructs a downloader.
  41. ///
  42. downloader(downloader&&) noexcept;
  43. ///
  44. /// Move assigns a downloader.
  45. ///
  46. downloader& operator=(downloader&&) noexcept;
  47. downloader(const downloader&) = delete;
  48. downloader& operator=(const downloader&) = delete;
  49. ///
  50. /// Destroys a downloader.
  51. ///
  52. ~downloader();
  53. ///
  54. /// Returns true if the downloader is valid, meaning it was not default constructed or moved
  55. /// from.
  56. ///
  57. explicit operator bool() const noexcept;
  58. ///
  59. /// Reads a specified number of bytes from the GridFS file being downloaded.
  60. ///
  61. /// @param buffer
  62. /// A pointer to a buffer to store the bytes read from the file.
  63. ///
  64. /// @param length
  65. /// The number of bytes to read from the file.
  66. ///
  67. /// @return
  68. /// The number of bytes actually read. If zero, the downloader has reached the end of the
  69. /// file.
  70. ///
  71. /// @throws mongocxx::logic_error if the download stream was already closed.
  72. ///
  73. /// @throws mongocxx::gridfs_exception if the requested file has been corrupted.
  74. ///
  75. /// @throws mongocxx::query_exception
  76. /// if an error occurs when reading chunk data from the database for the requested file.
  77. ///
  78. std::size_t read(std::uint8_t* buffer, std::size_t length);
  79. ///
  80. /// Closes the downloader stream.
  81. ///
  82. /// @throws mongocxx::logic_error if the download stream was already closed.
  83. ///
  84. void close();
  85. ///
  86. /// Gets the chunk size of the file being downloaded.
  87. ///
  88. /// @return
  89. /// The chunk size in bytes.
  90. ///
  91. std::int32_t chunk_size() const;
  92. ///
  93. /// Gets the length of the file being downloaded.
  94. ///
  95. /// @return
  96. /// The length in bytes.
  97. ///
  98. std::int64_t file_length() const;
  99. ///
  100. /// Gets the files collection document of the file being downloaded.
  101. ///
  102. /// @return
  103. /// A view to the files collection document of the file being downloaded.
  104. ///
  105. bsoncxx::document::view files_document() const;
  106. private:
  107. friend class bucket;
  108. //
  109. // Constructs a new downloader stream.
  110. //
  111. // @param chunks
  112. // The cursor to read the chunks of the file from. It must have a value if the length of the
  113. // file is non-zero.
  114. //
  115. // @param files_doc
  116. // The files collection document of the file being downloaded.
  117. //
  118. MONGOCXX_PRIVATE downloader(stdx::optional<cursor> chunks, bsoncxx::document::value files_doc);
  119. MONGOCXX_PRIVATE void fetch_chunk();
  120. class MONGOCXX_PRIVATE impl;
  121. MONGOCXX_PRIVATE impl& _get_impl();
  122. MONGOCXX_PRIVATE const impl& _get_impl() const;
  123. std::unique_ptr<impl> _impl;
  124. };
  125. } // namespace gridfs
  126. MONGOCXX_INLINE_NAMESPACE_END
  127. } // namespace mongocxx