Better auto-renaming

pull/709/head
Nils Maier 2016-07-18 23:11:55 +02:00
parent 1ab2b99692
commit e6e2de96ca
3 changed files with 40 additions and 3 deletions

View File

@ -778,8 +778,16 @@ void RequestGroup::tryAutoFileRenaming()
fmt("File renaming failed: %s", getFirstFilePath().c_str()),
error_code::FILE_RENAMING_FAILED);
}
auto fn = filepath;
std::string ext;
auto idx = fn.find_last_of(".");
auto slash = fn.find_last_of("\\/");
if (idx != std::string::npos && (slash == std::string::npos || slash < idx)) {
ext = fn.substr(idx);
fn = fn.substr(0, idx);
}
for (int i = 1; i < 10000; ++i) {
auto newfilename = fmt("%s.%d", filepath.c_str(), i);
auto newfilename = fmt("%s.%d%s", fn.c_str(), i, ext.c_str());
File newfile(newfilename);
File ctrlfile(newfile.getPath() + DefaultBtProgressInfoFile::getSuffix());
if (!newfile.exists() || (newfile.exists() && ctrlfile.exists())) {

View File

@ -199,8 +199,6 @@ private:
void initializePostDownloadHandler();
void tryAutoFileRenaming();
// Returns the result code of this RequestGroup. If the download
// finished, then returns error_code::FINISHED. If the
// download didn't finish and error result is available in
@ -219,6 +217,8 @@ public:
bool isCheckIntegrityReady();
void tryAutoFileRenaming();
const std::shared_ptr<SegmentMan>& getSegmentMan() const
{
return segmentMan_;

View File

@ -39,6 +39,35 @@ void RequestGroupTest::testGetFirstFilePath()
CPPUNIT_ASSERT_EQUAL(std::string("/tmp/myfile"), group.getFirstFilePath());
// test file renaming
option_->put(PREF_AUTO_FILE_RENAMING, "false");
try {
group.tryAutoFileRenaming();
}
catch (const Exception& ex) {
CPPUNIT_ASSERT_EQUAL(error_code::FILE_ALREADY_EXISTS, ex.getErrorCode());
}
option_->put(PREF_AUTO_FILE_RENAMING, "true");
group.tryAutoFileRenaming();
CPPUNIT_ASSERT_EQUAL(std::string("/tmp/myfile.1"), group.getFirstFilePath());
ctx->getFirstFileEntry()->setPath("/tmp/myfile.txt");
group.tryAutoFileRenaming();
CPPUNIT_ASSERT_EQUAL(std::string("/tmp/myfile.1.txt"), group.getFirstFilePath());
ctx->getFirstFileEntry()->setPath("/tmp.txt/myfile");
group.tryAutoFileRenaming();
CPPUNIT_ASSERT_EQUAL(std::string("/tmp.txt/myfile.1"), group.getFirstFilePath());
ctx->getFirstFileEntry()->setPath("/tmp.txt/myfile.txt");
group.tryAutoFileRenaming();
CPPUNIT_ASSERT_EQUAL(std::string("/tmp.txt/myfile.1.txt"), group.getFirstFilePath());
// test in-memory
ctx->getFirstFileEntry()->setPath("/tmp/myfile");
group.markInMemoryDownload();
CPPUNIT_ASSERT_EQUAL(std::string("[MEMORY]myfile"), group.getFirstFilePath());