PartList.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /***************************************************************************
  2. *
  3. * Project _____ __ ____ _ _
  4. * ( _ ) /__\ (_ _)_| |_ _| |_
  5. * )(_)( /(__)\ )( (_ _)(_ _)
  6. * (_____)(__)(__)(__) |_| |_|
  7. *
  8. *
  9. * Copyright 2018-present, Leonid Stryzhevskyi <lganzzzo@gmail.com>,
  10. * Matthias Haselmaier <mhaselmaier@gmail.com>
  11. *
  12. * Licensed under the Apache License, Version 2.0 (the "License");
  13. * you may not use this file except in compliance with the License.
  14. * You may obtain a copy of the License at
  15. *
  16. * http://www.apache.org/licenses/LICENSE-2.0
  17. *
  18. * Unless required by applicable law or agreed to in writing, software
  19. * distributed under the License is distributed on an "AS IS" BASIS,
  20. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21. * See the License for the specific language governing permissions and
  22. * limitations under the License.
  23. *
  24. ***************************************************************************/
  25. #include "PartList.hpp"
  26. namespace oatpp { namespace web { namespace mime { namespace multipart {
  27. PartList::PartList(const oatpp::String& boundary)
  28. : Multipart(boundary)
  29. , m_readIteratorInitialized(false)
  30. {}
  31. PartList::PartList(const Headers& requestHeaders)
  32. : Multipart(parseBoundaryFromHeaders(requestHeaders))
  33. , m_readIteratorInitialized(false)
  34. {
  35. if(!getBoundary()) {
  36. throw std::runtime_error("[oatpp::web::mime::multipart::PartList::PartList]: Error. No 'boundary' value found in headers.");
  37. }
  38. }
  39. std::shared_ptr<PartList> PartList::createSharedWithRandomBoundary(v_int32 boundarySize) {
  40. auto boundary = generateRandomBoundary(boundarySize);
  41. return std::make_shared<PartList>(boundary);
  42. }
  43. std::shared_ptr<Part> PartList::readNextPart(async::Action& action) {
  44. if(!m_readIteratorInitialized) {
  45. m_readIteratorInitialized = true;
  46. m_iterator = m_parts.begin();
  47. }
  48. if(m_iterator == m_parts.end()) {
  49. return nullptr;
  50. }
  51. return *m_iterator ++;
  52. }
  53. void PartList::writeNextPart(const std::shared_ptr<Part>& part, async::Action& action) {
  54. if(part->getName()) {
  55. m_namedParts[part->getName()].push_back(part);
  56. }
  57. m_parts.push_back(part);
  58. }
  59. std::shared_ptr<Part> PartList::getNamedPart(const oatpp::String& name) {
  60. auto it = m_namedParts.find(name);
  61. if(it != m_namedParts.end()) {
  62. return it->second.front();
  63. }
  64. return nullptr;
  65. }
  66. std::list<std::shared_ptr<Part>> PartList::getNamedParts(const oatpp::String& name) {
  67. auto it = m_namedParts.find(name);
  68. if(it != m_namedParts.end()) {
  69. return it->second;
  70. }
  71. return std::list<std::shared_ptr<Part>>{};
  72. }
  73. const std::list<std::shared_ptr<Part>>& PartList::getAllParts() {
  74. return m_parts;
  75. }
  76. v_int64 PartList::count() {
  77. return m_parts.size();
  78. }
  79. }}}}