Prechádzať zdrojové kódy

2009-12-23 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>

	Added --bt-save-metadata option. When true is given, it saves
	metadata as .torrent file. This option has effect only when
	BitTorrent Magnet URI is used.  The filename is name in metadata
	with suffix .torrent. The directory to be saved is the same
	directory where download file is saved. If the same file already
	exists, metdata is not saved.
	* src/OptionHandlerFactory.cc
	* src/UTMetadataPostDownloadHandler.cc
	* src/UTMetadataPostDownloadHandler.h
	* src/message.h
	* src/prefs.cc
	* src/prefs.h
	* src/usage_text.h
	* test/UTMetadataDataExtensionMessageTest.cc
Tatsuhiro Tsujikawa 16 rokov pred
rodič
commit
2745d85597

+ 17 - 0
ChangeLog

@@ -1,3 +1,20 @@
+2009-12-23  Tatsuhiro Tsujikawa  <t-tujikawa@users.sourceforge.net>
+
+	Added --bt-save-metadata option. When true is given, it saves
+	metadata as .torrent file. This option has effect only when
+	BitTorrent Magnet URI is used.  The filename is name in metadata
+	with suffix .torrent. The directory to be saved is the same
+	directory where download file is saved. If the same file already
+	exists, metdata is not saved.
+	* src/OptionHandlerFactory.cc
+	* src/UTMetadataPostDownloadHandler.cc
+	* src/UTMetadataPostDownloadHandler.h
+	* src/message.h
+	* src/prefs.cc
+	* src/prefs.h
+	* src/usage_text.h
+	* test/UTMetadataDataExtensionMessageTest.cc
+
 2009-12-23  Tatsuhiro Tsujikawa  <t-tujikawa@users.sourceforge.net>
 
 	Added util::saveAs() function. Use it in Signature::save().

+ 9 - 0
src/OptionHandlerFactory.cc

@@ -1021,6 +1021,15 @@ OptionHandlers OptionHandlerFactory::createOptionHandlers()
     op->addTag(TAG_BITTORRENT);
     handlers.push_back(op);
   }
+  {
+    SharedHandle<OptionHandler> op(new BooleanOptionHandler
+				   (PREF_BT_SAVE_METADATA,
+				    TEXT_BT_SAVE_METADATA,
+				    V_FALSE,
+				    OptionHandler::OPT_ARG));
+    op->addTag(TAG_BITTORRENT);
+    handlers.push_back(op);
+  }
   {
     SharedHandle<OptionHandler> op(new NumberOptionHandler
 				   (PREF_BT_STOP_TIMEOUT,

+ 22 - 1
src/UTMetadataPostDownloadHandler.cc

@@ -41,11 +41,15 @@
 #include "A2STR.h"
 #include "DownloadContext.h"
 #include "Logger.h"
+#include "LogFactory.h"
 #include "util.h"
 #include "a2functional.h"
 #include "DiskAdaptor.h"
 #include "PieceStorage.h"
 #include "bencode.h"
+#include "message.h"
+#include "prefs.h"
+#include "Option.h"
 
 namespace aria2 {
 
@@ -63,7 +67,8 @@ bool UTMetadataPostDownloadHandler::Criteria::match
   return false;
 }
 
-UTMetadataPostDownloadHandler::UTMetadataPostDownloadHandler()
+UTMetadataPostDownloadHandler::UTMetadataPostDownloadHandler():
+  _logger(LogFactory::getInstance())
 {
   setCriteria(SharedHandle<Criteria>(new Criteria()));
 }
@@ -82,6 +87,22 @@ void UTMetadataPostDownloadHandler::getNextRequestGroups
 				  std::deque<std::string>(), torrent);
   requestGroup->followedBy(newRgs.begin(), newRgs.end());
   groups.insert(groups.end(), newRgs.begin(), newRgs.end());
+
+  if(!newRgs.empty() &&
+     requestGroup->getOption()->getAsBool(PREF_BT_SAVE_METADATA)) {
+    SharedHandle<DownloadContext> dctx = newRgs.front()->getDownloadContext();
+    assert(dctx->hasAttribute(bittorrent::BITTORRENT));
+    std::string filename = requestGroup->getOption()->get(PREF_DIR);
+    filename += A2STR::SLASH_C;
+    filename +=
+      dctx->getAttribute(bittorrent::BITTORRENT)[bittorrent::NAME].s();
+    filename += ".torrent";
+    if(util::saveAs(filename, torrent)) {
+      _logger->notice(MSG_METADATA_SAVED, filename.c_str());
+    } else {
+      _logger->notice(MSG_METADATA_NOT_SAVED, filename.c_str());
+    }
+  }
 }
 
 } // namespace aria2

+ 4 - 0
src/UTMetadataPostDownloadHandler.h

@@ -40,9 +40,13 @@
 
 namespace aria2 {
 
+class Logger;
+
 class UTMetadataPostDownloadHandler:public PostDownloadHandler
 {
 private:
+  Logger* _logger;
+
   class Criteria:public RequestGroupCriteria
   {
   public:

+ 3 - 0
src/message.h

@@ -177,6 +177,9 @@
 #define MSG_NOT_FILE _("Is '%s' a file?")
 #define MSG_INTERFACE_NOT_FOUND _("Failed to find given interface %s,"\
 				  " cause: %s")
+#define MSG_METADATA_SAVED _("Saved metadata as %s.")
+#define MSG_METADATA_NOT_SAVED _("Saving metadata as %s failed. Maybe file"\
+				 " already exists.")
 
 #define EX_TIME_OUT _("Timeout.")
 #define EX_INVALID_CHUNK_SIZE _("Invalid chunk size.")

+ 2 - 0
src/prefs.cc

@@ -308,6 +308,8 @@ const std::string PREF_BT_TRACKER_INTERVAL("bt-tracker-interval");
 const std::string PREF_BT_STOP_TIMEOUT("bt-stop-timeout");
 // values: head[=SIZE]|tail[=SIZE], ...
 const std::string PREF_BT_PRIORITIZE_PIECE("bt-prioritize-piece");
+// values: true | false
+const std::string PREF_BT_SAVE_METADATA("bt-save-metadata");
 
 /**
  * Metalink related preferences

+ 2 - 0
src/prefs.h

@@ -312,6 +312,8 @@ extern const std::string PREF_BT_TRACKER_INTERVAL;
 extern const std::string PREF_BT_STOP_TIMEOUT;
 // values: head[=SIZE]|tail[=SIZE], ...
 extern const std::string PREF_BT_PRIORITIZE_PIECE;
+// values: true | false
+extern const std::string PREF_BT_SAVE_METADATA;
 
 /**
  * Metalink related preferences

+ 7 - 0
src/usage_text.h

@@ -582,3 +582,10 @@ _(" --interface=INTERFACE        Bind sockets to given interface. You can specif
   "                              interface name, IP address and hostname.")
 #define TEXT_DISABLE_IPV6 \
 _(" --disable-ipv6[=true|false]  Disable IPv6.")
+#define TEXT_BT_SAVE_METADATA \
+_(" --bt-save-metadata[=true|false] Save metadata as .torrent file. This option has\n" \
+  "                              effect only when BitTorrent Magnet URI is used.\n" \
+  "                              The filename is name in metadata with suffix\n" \
+  "                              .torrent. The directory to be saved is the same\n" \
+  "                              directory where download file is saved. If the\n" \
+  "                              same file already exists, metdata is not saved.")

+ 12 - 0
test/UTMetadataPostDownloadHandlerTest.cc

@@ -13,6 +13,7 @@
 #include "DiskAdaptor.h"
 #include "util.h"
 #include "MessageDigestHelper.h"
+#include "prefs.h"
 
 namespace aria2 {
 
@@ -62,6 +63,10 @@ void UTMetadataPostDownloadHandlerTest::testCanHandle()
 
 void UTMetadataPostDownloadHandlerTest::testGetNextRequestGroups()
 {
+  File trfile("aria2-0.8.2.tar.bz2.torrent");
+  if(trfile.exists()) {
+    trfile.remove();
+  }
   std::string metadata =
     "d6:lengthi384e4:name19:aria2-0.8.2.tar.bz212:piece lengthi128e"
     "6:pieces60:AAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCe";
@@ -104,6 +109,13 @@ void UTMetadataPostDownloadHandlerTest::testGetNextRequestGroups()
     (std::find(_requestGroup->followedBy().begin(),
 	       _requestGroup->followedBy().end(),
 	       newRg->getGID()) != _requestGroup->followedBy().end());
+  CPPUNIT_ASSERT(!trfile.exists());
+
+  results.clear();
+
+  _requestGroup->getOption()->put(PREF_BT_SAVE_METADATA, V_TRUE);
+  handler.getNextRequestGroups(results, _requestGroup.get());
+  CPPUNIT_ASSERT(trfile.exists());
 
   results.clear();