MetalinkPostDownloadHandler.cc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "MetalinkPostDownloadHandler.h"
  36. #include <deque>
  37. #include "RequestGroup.h"
  38. #include "Metalink2RequestGroup.h"
  39. #include "Logger.h"
  40. #include "LogFactory.h"
  41. #include "DiskAdaptor.h"
  42. #include "PieceStorage.h"
  43. #include "DownloadHandlerConstants.h"
  44. #include "ContentTypeRequestGroupCriteria.h"
  45. #include "Exception.h"
  46. #include "prefs.h"
  47. #include "Option.h"
  48. #include "DownloadContext.h"
  49. #include "download_helper.h"
  50. #include "fmt.h"
  51. #include "FileEntry.h"
  52. #include "RequestGroupMan.h"
  53. namespace aria2 {
  54. MetalinkPostDownloadHandler::MetalinkPostDownloadHandler()
  55. {
  56. setCriteria(make_unique<ContentTypeRequestGroupCriteria>(
  57. getMetalinkContentTypes(), getMetalinkExtensions()));
  58. }
  59. namespace {
  60. const std::string& getBaseUri(RequestGroup* requestGroup)
  61. {
  62. auto& dctx = requestGroup->getDownloadContext();
  63. if (dctx->getFileEntries().empty()) {
  64. return A2STR::NIL;
  65. }
  66. else {
  67. // TODO Check download result for each URI
  68. auto& entry = dctx->getFirstFileEntry();
  69. auto& spentUris = entry->getSpentUris();
  70. if (spentUris.empty()) {
  71. auto& remainingUris = entry->getRemainingUris();
  72. if (remainingUris.empty()) {
  73. return A2STR::NIL;
  74. }
  75. else {
  76. return remainingUris.front();
  77. }
  78. }
  79. else {
  80. return spentUris.back();
  81. }
  82. }
  83. }
  84. } // namespace
  85. void MetalinkPostDownloadHandler::getNextRequestGroups(
  86. std::vector<std::shared_ptr<RequestGroup>>& groups,
  87. RequestGroup* requestGroup) const
  88. {
  89. A2_LOG_DEBUG(fmt("Generating RequestGroups for Metalink file %s",
  90. requestGroup->getFirstFilePath().c_str()));
  91. auto diskAdaptor = requestGroup->getPieceStorage()->getDiskAdaptor();
  92. try {
  93. diskAdaptor->openExistingFile();
  94. // requestOption.put(PREF_DIR,
  95. // requestGroup->getDownloadContext()->getDir());
  96. const std::string& baseUri = getBaseUri(requestGroup);
  97. std::vector<std::shared_ptr<RequestGroup>> newRgs;
  98. Metalink2RequestGroup().generate(newRgs, diskAdaptor,
  99. requestGroup->getOption(), baseUri);
  100. requestGroup->followedBy(newRgs.begin(), newRgs.end());
  101. for (auto& rg : newRgs) {
  102. rg->following(requestGroup->getGID());
  103. }
  104. auto mi = createMetadataInfoFromFirstFileEntry(
  105. requestGroup->getGroupId(), requestGroup->getDownloadContext());
  106. if (mi) {
  107. setMetadataInfo(newRgs.begin(), newRgs.end(), mi);
  108. }
  109. auto rgman = requestGroup->getRequestGroupMan();
  110. if (rgman && rgman->getKeepRunning() &&
  111. requestGroup->getOption()->getAsBool(PREF_PAUSE_METADATA)) {
  112. for (auto& rg : newRgs) {
  113. rg->setPauseRequested(true);
  114. }
  115. }
  116. groups.insert(groups.end(), newRgs.begin(), newRgs.end());
  117. diskAdaptor->closeFile();
  118. }
  119. catch (Exception& e) {
  120. diskAdaptor->closeFile();
  121. throw;
  122. }
  123. }
  124. } // namespace aria2