Added --rpc-save-upload-metadata option

If true is given, which is default, save the uploaded torrent or
metalink metadata in the directory specified by --dir option. The
filename consists of SHA1-hash hex string of metadata plus
extension. For torrent, the extension is '.torrent'. For metalink, it
is '.meta4'.  If false is given to this option, the downloads added by
aria2.addTorrent or aria2.addMetalink will not be saved by
--save-session option.
pull/28/head
Tatsuhiro Tsujikawa 2012-10-16 00:18:34 +09:00
parent 4a4fec2c8c
commit a9c76ed35e
6 changed files with 88 additions and 30 deletions

View File

@ -792,6 +792,17 @@ std::vector<OptionHandler*> OptionHandlerFactory::createOptionHandlers()
op->addTag(TAG_RPC);
handlers.push_back(op);
}
{
OptionHandler* op(new BooleanOptionHandler
(PREF_RPC_SAVE_UPLOAD_METADATA,
TEXT_RPC_SAVE_UPLOAD_METADATA,
A2_V_TRUE,
OptionHandler::OPT_ARG));
op->addTag(TAG_RPC);
op->setInitialOption(true);
op->setChangeGlobalOption(true);
handlers.push_back(op);
}
{
OptionHandler* op(new BooleanOptionHandler
(PREF_RPC_SECURE,

View File

@ -278,19 +278,23 @@ SharedHandle<ValueBase> AddTorrentRpcMethod::process
bool posGiven = checkPosParam(posParam);
size_t pos = posGiven ? posParam->i() : 0;
std::string filename = util::applyDir
(requestOption->get(PREF_DIR), getHexSha1(torrentParam->s())+".torrent");
std::vector<SharedHandle<RequestGroup> > result;
// Save uploaded data in order to save this download in
// --save-session file.
if(util::saveAs(filename, torrentParam->s(), true)) {
A2_LOG_INFO(fmt("Uploaded torrent data was saved as %s", filename.c_str()));
requestOption->put(PREF_TORRENT_FILE, filename);
} else {
A2_LOG_INFO(fmt("Uploaded torrent data was not saved."
" Failed to write file %s", filename.c_str()));
filename.clear();
std::string filename;
if(requestOption->getAsBool(PREF_RPC_SAVE_UPLOAD_METADATA)) {
filename = util::applyDir
(requestOption->get(PREF_DIR), getHexSha1(torrentParam->s())+".torrent");
// Save uploaded data in order to save this download in
// --save-session file.
if(util::saveAs(filename, torrentParam->s(), true)) {
A2_LOG_INFO(fmt("Uploaded torrent data was saved as %s",
filename.c_str()));
requestOption->put(PREF_TORRENT_FILE, filename);
} else {
A2_LOG_INFO(fmt("Uploaded torrent data was not saved."
" Failed to write file %s", filename.c_str()));
filename.clear();
}
}
std::vector<SharedHandle<RequestGroup> > result;
createRequestGroupForBitTorrent(result, requestOption, uris, filename,
torrentParam->s());
@ -325,22 +329,27 @@ SharedHandle<ValueBase> AddMetalinkRpcMethod::process
std::vector<SharedHandle<RequestGroup> > result;
#ifdef ENABLE_MESSAGE_DIGEST
// TODO RFC5854 Metalink has the extension .meta4 and Metalink
// Version 3 uses .metalink extension. We use .meta4 for both
// RFC5854 Metalink and Version 3. aria2 can detect which of which
// by reading content rather than extension.
std::string filename = util::applyDir
(requestOption->get(PREF_DIR), getHexSha1(metalinkParam->s())+".meta4");
// Save uploaded data in order to save this download in
// --save-session file.
if(util::saveAs(filename, metalinkParam->s(), true)) {
A2_LOG_INFO(fmt("Uploaded metalink data was saved as %s",
filename.c_str()));
requestOption->put(PREF_METALINK_FILE, filename);
createRequestGroupForMetalink(result, requestOption);
std::string filename;
if(requestOption->getAsBool(PREF_RPC_SAVE_UPLOAD_METADATA)) {
// TODO RFC5854 Metalink has the extension .meta4 and Metalink
// Version 3 uses .metalink extension. We use .meta4 for both
// RFC5854 Metalink and Version 3. aria2 can detect which of which
// by reading content rather than extension.
filename = util::applyDir
(requestOption->get(PREF_DIR), getHexSha1(metalinkParam->s())+".meta4");
// Save uploaded data in order to save this download in
// --save-session file.
if(util::saveAs(filename, metalinkParam->s(), true)) {
A2_LOG_INFO(fmt("Uploaded metalink data was saved as %s",
filename.c_str()));
requestOption->put(PREF_METALINK_FILE, filename);
createRequestGroupForMetalink(result, requestOption);
} else {
A2_LOG_INFO(fmt("Uploaded metalink data was not saved."
" Failed to write file %s", filename.c_str()));
createRequestGroupForMetalink(result, requestOption, metalinkParam->s());
}
} else {
A2_LOG_INFO(fmt("Uploaded metalink data was not saved."
" Failed to write file %s", filename.c_str()));
createRequestGroupForMetalink(result, requestOption, metalinkParam->s());
}
#else // !ENABLE_MESSAGE_DIGEST

View File

@ -277,6 +277,8 @@ const Pref* PREF_RPC_PRIVATE_KEY = makePref("rpc-private-key");
// value: true | false
const Pref* PREF_RPC_SECURE = makePref("rpc-secure");
// value: true | false
const Pref* PREF_RPC_SAVE_UPLOAD_METADATA = makePref("rpc-save-upload-metadata");
// value: true | false
const Pref* PREF_DRY_RUN = makePref("dry-run");
// value: true | false
const Pref* PREF_REUSE_URI = makePref("reuse-uri");

View File

@ -220,6 +220,8 @@ extern const Pref* PREF_RPC_PRIVATE_KEY;
// value: true | false
extern const Pref* PREF_RPC_SECURE;
// value: true | false
extern const Pref* PREF_RPC_SAVE_UPLOAD_METADATA;
// value: true | false
extern const Pref* PREF_DRY_RUN;
// value: true | false
extern const Pref* PREF_REUSE_URI;

View File

@ -898,3 +898,14 @@
" scheme. Use --rpc-certificate and\n" \
" --rpc-private-key options to specify the\n" \
" server certificate and private key.")
#define TEXT_RPC_SAVE_UPLOAD_METADATA \
_(" --rpc-save-upload-metadata[=true|false] Save the uploaded torrent or\n" \
" metalink metadata in the directory specified\n" \
" by --dir option. The filename consists of\n" \
" SHA1-hash hex string of metadata plus\n" \
" extension. For torrent, the extension is\n" \
" '.torrent'. For metalink, it is '.meta4'.\n" \
" If false is given to this option, the\n" \
" downloads added by aria2.addTorrent or\n" \
" aria2.addMetalink will not be saved by\n" \
" --save-session option.")

View File

@ -264,13 +264,23 @@ void RpcMethodTest::testAddTorrent()
SharedHandle<List> uris = List::g();
uris->append("http://localhost/aria2-0.8.2.tar.bz2");
req.params->append(uris);
{
// Saving upload metadata is disabled by option.
RpcResponse res = m.execute(req, e_.get());
CPPUNIT_ASSERT
(!File(e_->getOption()->get(PREF_DIR)+
"/0a3893293e27ac0490424c06de4d09242215f0a6.torrent").exists());
CPPUNIT_ASSERT_EQUAL(0, res.code);
CPPUNIT_ASSERT_EQUAL(std::string("1"), downcast<String>(res.param)->s());
}
e_->getOption()->put(PREF_RPC_SAVE_UPLOAD_METADATA, A2_V_TRUE);
{
RpcResponse res = m.execute(req, e_.get());
CPPUNIT_ASSERT
(File(e_->getOption()->get(PREF_DIR)+
"/0a3893293e27ac0490424c06de4d09242215f0a6.torrent").exists());
CPPUNIT_ASSERT_EQUAL(0, res.code);
CPPUNIT_ASSERT_EQUAL(std::string("1"), downcast<String>(res.param)->s());
CPPUNIT_ASSERT_EQUAL(std::string("2"), downcast<String>(res.param)->s());
SharedHandle<RequestGroup> group =
e_->getRequestGroupMan()->findReservedGroup(1);
@ -296,7 +306,7 @@ void RpcMethodTest::testAddTorrent()
CPPUNIT_ASSERT_EQUAL(0, res.code);
CPPUNIT_ASSERT_EQUAL
(dir+"/aria2-0.8.2.tar.bz2",
e_->getRequestGroupMan()->findReservedGroup(2)->getFirstFilePath());
e_->getRequestGroupMan()->findReservedGroup(3)->getFirstFilePath());
CPPUNIT_ASSERT
(File(dir+"/0a3893293e27ac0490424c06de4d09242215f0a6.torrent").exists());
}
@ -352,12 +362,25 @@ void RpcMethodTest::testAddMetalink()
RpcRequest req(AddMetalinkRpcMethod::getMethodName(), List::g());
req.params->append(readFile(A2_TEST_DIR"/2files.metalink"));
{
// Saving upload metadata is disabled by option.
RpcResponse res = m.execute(req, e_.get());
CPPUNIT_ASSERT_EQUAL(0, res.code);
const List* resParams = downcast<List>(res.param);
CPPUNIT_ASSERT_EQUAL((size_t)2, resParams->size());
CPPUNIT_ASSERT_EQUAL(std::string("1"), downcast<String>(resParams->get(0))->s());
CPPUNIT_ASSERT_EQUAL(std::string("2"), downcast<String>(resParams->get(1))->s());
CPPUNIT_ASSERT
(!File(e_->getOption()->get(PREF_DIR)+
"/c908634fbc257fd56f0114912c2772aeeb4064f4.meta4").exists());
}
e_->getOption()->put(PREF_RPC_SAVE_UPLOAD_METADATA, A2_V_TRUE);
{
RpcResponse res = m.execute(req, e_.get());
CPPUNIT_ASSERT_EQUAL(0, res.code);
const List* resParams = downcast<List>(res.param);
CPPUNIT_ASSERT_EQUAL((size_t)2, resParams->size());
CPPUNIT_ASSERT_EQUAL(std::string("3"), downcast<String>(resParams->get(0))->s());
CPPUNIT_ASSERT_EQUAL(std::string("4"), downcast<String>(resParams->get(1))->s());
#ifdef ENABLE_MESSAGE_DIGEST
CPPUNIT_ASSERT
(File(e_->getOption()->get(PREF_DIR)+
@ -386,7 +409,7 @@ void RpcMethodTest::testAddMetalink()
RpcResponse res = m.execute(req, e_.get());
CPPUNIT_ASSERT_EQUAL(0, res.code);
CPPUNIT_ASSERT_EQUAL(dir+"/aria2-5.0.0.tar.bz2",
e_->getRequestGroupMan()->findReservedGroup(3)->
e_->getRequestGroupMan()->findReservedGroup(5)->
getFirstFilePath());
#ifdef ENABLE_MESSAGE_DIGEST
CPPUNIT_ASSERT