Bladeren bron

Add addTorrent API

Tatsuhiro Tsujikawa 12 jaren geleden
bovenliggende
commit
8e8fb9ee36
3 gewijzigde bestanden met toevoegingen van 99 en 0 verwijderingen
  1. 44 0
      src/aria2api.cc
  2. 41 0
      src/includes/aria2/aria2.h
  3. 14 0
      test/Aria2ApiTest.cc

+ 44 - 0
src/aria2api.cc

@@ -306,6 +306,50 @@ int addMetalink(Session* session,
 #endif // !ENABLE_METALINK
 }
 
+int addTorrent(Session* session,
+               A2Gid* gid,
+               const std::string& torrentFile,
+               const std::vector<std::string>& webSeedUris,
+               const KeyVals& options,
+               int position)
+{
+#ifdef ENABLE_BITTORRENT
+  const SharedHandle<DownloadEngine>& e =
+    session->context->reqinfo->getDownloadEngine();
+  SharedHandle<Option> requestOption(new Option(*e->getOption()));
+  std::vector<SharedHandle<RequestGroup> > result;
+  try {
+    apiGatherRequestOption(requestOption.get(), options,
+                           OptionParser::getInstance());
+    requestOption->put(PREF_TORRENT_FILE, torrentFile);
+    createRequestGroupForBitTorrent(result, requestOption, webSeedUris,
+                                    torrentFile);
+  } catch(RecoverableException& e) {
+    A2_LOG_INFO_EX(EX_EXCEPTION_CAUGHT, e);
+    return -1;
+  }
+  if(!result.empty()) {
+    addRequestGroup(result.front(), e, position);
+    if(gid) {
+      *gid = result.front()->getGID();
+    }
+  }
+  return 0;
+#else // !ENABLE_BITTORRENT
+  return -1;
+#endif // !ENABLE_BITTORRENT
+}
+
+int addTorrent(Session* session,
+               A2Gid* gid,
+               const std::string& torrentFile,
+               const KeyVals& options,
+               int position)
+{
+  return addTorrent(session, gid, torrentFile, std::vector<std::string>(),
+                    options, position);
+}
+
 int removeDownload(Session* session, const A2Gid& gid, bool force)
 {
   const SharedHandle<DownloadEngine>& e =

+ 41 - 0
src/includes/aria2/aria2.h

@@ -314,6 +314,47 @@ int addMetalink(Session* session,
                 const KeyVals& options,
                 int position = -1);
 
+/**
+ * @function
+ *
+ * Adds BitTorrent download. On successful return, if the |gid| is not
+ * ``NULL``, the GID of added download will be assigned to the
+ * |*gid|. The path to ".torrent" file is specified by the
+ * |torrentFile|. BitTorrent Magnet URI cannot be used with this
+ * function. Use :func:`addUri()` instead. The |webSeedUris| contains
+ * URIs used for Web-seeding. For single file torrents, URI can be a
+ * complete URI pointing to the resource or if URI ends with /, name
+ * in torrent file is added. For multi-file torrents, name and path in
+ * torrent are added to form a URI for each file. The |options| is an
+ * array of a pair of option name and value.  If unknown options are
+ * included in |options|, they are simply ignored. If the |position|
+ * is not negative integer, the new download is inserted at position
+ * in the waiting queue. If the |position| is negative or the
+ * |position| is larger than the size of the queue, it is appended at
+ * the end of the queue.
+ *
+ * This function returns 0 if it succeeds, or negative error code.
+ *
+ */
+int addTorrent(Session* session,
+               A2Gid* gid,
+               const std::string& torrentFile,
+               const std::vector<std::string>& webSeedUris,
+               const KeyVals& options,
+               int position = -1);
+
+/**
+ * @function
+ *
+ * Same as :func:`addTorrent()` with an empty vector as the
+ * |webSeedUris|.
+ */
+int addTorrent(Session* session,
+               A2Gid* gid,
+               const std::string& torrentFile,
+               const KeyVals& options,
+               int position = -1);
+
 /**
  * @function
  *

+ 14 - 0
test/Aria2ApiTest.cc

@@ -9,6 +9,7 @@ class Aria2ApiTest:public CppUnit::TestFixture {
   CPPUNIT_TEST_SUITE(Aria2ApiTest);
   CPPUNIT_TEST(testAddUri);
   CPPUNIT_TEST(testAddMetalink);
+  CPPUNIT_TEST(testAddTorrent);
   CPPUNIT_TEST(testRemovePause);
   CPPUNIT_TEST(testChangePosition);
   CPPUNIT_TEST_SUITE_END();
@@ -29,6 +30,7 @@ public:
 
   void testAddUri();
   void testAddMetalink();
+  void testAddTorrent();
   void testRemovePause();
   void testChangePosition();
 };
@@ -70,6 +72,18 @@ void Aria2ApiTest::testAddMetalink()
                                        options));
 }
 
+void Aria2ApiTest::testAddTorrent()
+{
+  std::string torrentPath = A2_TEST_DIR"/test.torrent";
+  A2Gid gid;
+  KeyVals options;
+  CPPUNIT_ASSERT_EQUAL(0, addTorrent(session_, &gid, torrentPath, options));
+  CPPUNIT_ASSERT(!isNull(gid));
+
+  options.push_back(KeyVals::value_type("file-allocation", "foo"));
+  CPPUNIT_ASSERT_EQUAL(-1, addTorrent(session_, &gid, torrentPath, options));
+}
+
 void Aria2ApiTest::testRemovePause()
 {
   A2Gid gid;