/* */ #include "BtPostDownloadHandler.h" #include "prefs.h" #include "RequestGroup.h" #include "Option.h" #include "Logger.h" #include "LogFactory.h" #include "DownloadHandlerConstants.h" #include "File.h" #include "PieceStorage.h" #include "DiskAdaptor.h" #include "util.h" #include "ContentTypeRequestGroupCriteria.h" #include "Exception.h" #include "DownloadContext.h" #include "download_helper.h" #include "fmt.h" #include "ValueBaseBencodeParser.h" #include "DiskWriter.h" #include "AbstractSingleDiskAdaptor.h" #include "BencodeDiskWriter.h" namespace aria2 { BtPostDownloadHandler::BtPostDownloadHandler() { SharedHandle cri (new ContentTypeRequestGroupCriteria (getBtContentTypes(), getBtExtensions())); setCriteria(cri); } BtPostDownloadHandler::~BtPostDownloadHandler() {} void BtPostDownloadHandler::getNextRequestGroups (std::vector >& groups, RequestGroup* requestGroup) { A2_LOG_INFO(fmt("Generating RequestGroups for Torrent file %s", requestGroup->getFirstFilePath().c_str())); SharedHandle torrent; if(requestGroup->inMemoryDownload()) { const SharedHandle& dw = static_pointer_cast (requestGroup->getPieceStorage()->getDiskAdaptor())->getDiskWriter(); const SharedHandle& bdw = static_pointer_cast(dw); int error = bdw->finalize(); if(error == 0) { torrent = bdw->getResult(); } } else { std::string content; try { requestGroup->getPieceStorage()->getDiskAdaptor()->openExistingFile(); content = util::toString(requestGroup->getPieceStorage() ->getDiskAdaptor()); requestGroup->getPieceStorage()->getDiskAdaptor()->closeFile(); } catch(Exception& e) { requestGroup->getPieceStorage()->getDiskAdaptor()->closeFile(); throw; } ssize_t error; torrent = bittorrent::ValueBaseBencodeParser().parseFinal (content.c_str(), content.size(), error); } if(!torrent) { throw DL_ABORT_EX2("Could not parse BitTorrent metainfo", error_code::BENCODE_PARSE_ERROR); } std::vector > newRgs; createRequestGroupForBitTorrent(newRgs, requestGroup->getOption(), std::vector(), "", torrent); requestGroup->followedBy(newRgs.begin(), newRgs.end()); SharedHandle mi = createMetadataInfoFromFirstFileEntry(requestGroup->getGroupId(), requestGroup->getDownloadContext()); if(mi) { setMetadataInfo(newRgs.begin(), newRgs.end(), mi); } groups.insert(groups.end(), newRgs.begin(), newRgs.end()); } } // namespace aria2