Provide torrent creation feature via WebAPI

PR #20366.
Closes #5614.

Co-authored-by: Radu Carpa <radu.carpa@cern.ch>
This commit is contained in:
Vladimir Golovnev
2024-02-27 15:57:16 +03:00
committed by GitHub
parent 15697f904d
commit 0114610a40
17 changed files with 887 additions and 83 deletions

View File

@@ -0,0 +1,134 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2024 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2024 Radu Carpa <radu.carpa@cern.ch>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*/
#include "torrentcreationmanager.h"
#include <utility>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/composite_key.hpp>
#include <boost/multi_index/indexed_by.hpp>
#include <boost/multi_index/mem_fun.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <QUuid>
#define SETTINGS_KEY(name) u"TorrentCreator/Manager/" name
namespace BitTorrent
{
using namespace boost::multi_index;
class TorrentCreationManager::TaskSet final : public boost::multi_index_container<
std::shared_ptr<TorrentCreationTask>,
indexed_by<
ordered_unique<tag<struct ByID>, const_mem_fun<TorrentCreationTask, QString, &TorrentCreationTask::id>>,
ordered_non_unique<tag<struct ByCompletion>, composite_key<
TorrentCreationTask,
const_mem_fun<TorrentCreationTask, bool, &TorrentCreationTask::isFinished>,
const_mem_fun<TorrentCreationTask, QDateTime, &TorrentCreationTask::timeAdded>>>>>
{
};
}
BitTorrent::TorrentCreationManager::TorrentCreationManager(IApplication *app, QObject *parent)
: ApplicationComponent(app, parent)
, m_maxTasks {SETTINGS_KEY(u"MaxTasks"_s), 256}
, m_numThreads {SETTINGS_KEY(u"NumThreads"_s), 1}
, m_tasks {std::make_unique<TaskSet>()}
{
if (m_numThreads > 0)
m_threadPool.setMaxThreadCount(m_numThreads);
}
BitTorrent::TorrentCreationManager::~TorrentCreationManager() = default;
std::shared_ptr<BitTorrent::TorrentCreationTask> BitTorrent::TorrentCreationManager::createTask(const TorrentCreatorParams &params, bool startSeeding)
{
if (std::cmp_greater_equal(m_tasks->size(), m_maxTasks.get()))
{
// Try to delete old finished tasks to stay under target
auto &tasksByCompletion = m_tasks->get<ByCompletion>();
auto [iter, endIter] = tasksByCompletion.equal_range(std::make_tuple(true));
while ((iter != endIter) && std::cmp_greater_equal(m_tasks->size(), m_maxTasks.get()))
{
iter = tasksByCompletion.erase(iter);
}
}
if (std::cmp_greater_equal(m_tasks->size(), m_maxTasks.get()))
return {};
const QString taskID = generateTaskID();
auto *torrentCreator = new TorrentCreator(params, this);
auto creationTask = std::make_shared<TorrentCreationTask>(app(), taskID, torrentCreator, startSeeding);
connect(creationTask.get(), &QObject::destroyed, torrentCreator, &BitTorrent::TorrentCreator::requestInterruption);
m_tasks->get<ByID>().insert(creationTask);
m_threadPool.start(torrentCreator);
return creationTask;
}
QString BitTorrent::TorrentCreationManager::generateTaskID() const
{
const auto &tasksByID = m_tasks->get<ByID>();
QString taskID = QUuid::createUuid().toString(QUuid::WithoutBraces);
while (tasksByID.find(taskID) != tasksByID.end())
taskID = QUuid::createUuid().toString(QUuid::WithoutBraces);
return taskID;
}
std::shared_ptr<BitTorrent::TorrentCreationTask> BitTorrent::TorrentCreationManager::getTask(const QString &id) const
{
const auto &tasksByID = m_tasks->get<ByID>();
const auto iter = tasksByID.find(id);
if (iter == tasksByID.end())
return nullptr;
return *iter;
}
QList<std::shared_ptr<BitTorrent::TorrentCreationTask>> BitTorrent::TorrentCreationManager::tasks() const
{
const auto &tasksByCompletion = m_tasks->get<ByCompletion>();
return {tasksByCompletion.cbegin(), tasksByCompletion.cend()};
}
bool BitTorrent::TorrentCreationManager::deleteTask(const QString &id)
{
auto &tasksByID = m_tasks->get<ByID>();
const auto iter = tasksByID.find(id);
if (iter == tasksByID.end())
return false;
tasksByID.erase(iter);
return true;
}

View File

@@ -0,0 +1,70 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2024 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2024 Radu Carpa <radu.carpa@cern.ch>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*/
#pragma once
#include <memory>
#include <QtContainerFwd>
#include <QObject>
#include <QThreadPool>
#include "base/applicationcomponent.h"
#include "base/settingvalue.h"
#include "torrentcreationtask.h"
#include "torrentcreator.h"
namespace BitTorrent
{
class TorrentCreationManager final : public ApplicationComponent<QObject>
{
Q_OBJECT
Q_DISABLE_COPY_MOVE(TorrentCreationManager)
public:
explicit TorrentCreationManager(IApplication *app, QObject *parent = nullptr);
~TorrentCreationManager() override;
std::shared_ptr<TorrentCreationTask> createTask(const TorrentCreatorParams &params, bool startSeeding = true);
std::shared_ptr<TorrentCreationTask> getTask(const QString &id) const;
QList<std::shared_ptr<TorrentCreationTask>> tasks() const;
bool deleteTask(const QString &id);
private:
QString generateTaskID() const;
CachedSettingValue<qint32> m_maxTasks;
CachedSettingValue<qint32> m_numThreads;
class TaskSet;
std::unique_ptr<TaskSet> m_tasks;
QThreadPool m_threadPool;
};
}

View File

@@ -0,0 +1,149 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2024 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2024 Radu Carpa <radu.carpa@cern.ch>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*/
#include "torrentcreationtask.h"
#include "base/addtorrentmanager.h"
#include "base/interfaces/iapplication.h"
#include "base/bittorrent/addtorrentparams.h"
BitTorrent::TorrentCreationTask::TorrentCreationTask(IApplication *app, const QString &id
, TorrentCreator *torrentCreator, bool startSeeding, QObject *parent)
: ApplicationComponent(app, parent)
, m_id {id}
, m_params {torrentCreator->params()}
, m_timeAdded {QDateTime::currentDateTime()}
{
Q_ASSERT(torrentCreator);
connect(torrentCreator, &BitTorrent::TorrentCreator::started, this, [this]
{
m_timeStarted = QDateTime::currentDateTime();
});
connect(torrentCreator, &BitTorrent::TorrentCreator::progressUpdated, this
, [this](const int progress)
{
m_progress = progress;
});
connect(torrentCreator, &BitTorrent::TorrentCreator::creationSuccess, this
, [this, app, startSeeding](const TorrentCreatorResult &result)
{
m_timeFinished = QDateTime::currentDateTime();
m_result = result;
if (!startSeeding)
return;
BitTorrent::AddTorrentParams params;
params.savePath = result.savePath;
params.skipChecking = true;
params.useAutoTMM = false; // otherwise if it is on by default, it will overwrite `savePath` to the default save path
if (!app->addTorrentManager()->addTorrent(result.torrentFilePath.data(), params))
m_errorMsg = tr("Failed to start seeding.");
});
connect(torrentCreator, &BitTorrent::TorrentCreator::creationFailure, this
, [this](const QString &errorMsg)
{
m_timeFinished = QDateTime::currentDateTime();
m_errorMsg = errorMsg;
});
}
QString BitTorrent::TorrentCreationTask::id() const
{
return m_id;
}
const BitTorrent::TorrentCreatorParams &BitTorrent::TorrentCreationTask::params() const
{
return m_params;
}
BitTorrent::TorrentCreationTask::State BitTorrent::TorrentCreationTask::state() const
{
if (m_timeStarted.isNull())
return Queued;
if (m_timeFinished.isNull())
return Running;
return Finished;
}
bool BitTorrent::TorrentCreationTask::isQueued() const
{
return (state() == Queued);
}
bool BitTorrent::TorrentCreationTask::isRunning() const
{
return (state() == Running);
}
bool BitTorrent::TorrentCreationTask::isFinished() const
{
return (state() == Finished);
}
bool BitTorrent::TorrentCreationTask::isFailed() const
{
return !m_errorMsg.isEmpty();
}
int BitTorrent::TorrentCreationTask::progress() const
{
return m_progress;
}
const BitTorrent::TorrentCreatorResult &BitTorrent::TorrentCreationTask::result() const
{
return m_result;
}
QString BitTorrent::TorrentCreationTask::errorMsg() const
{
return m_errorMsg;
}
QDateTime BitTorrent::TorrentCreationTask::timeAdded() const
{
return m_timeAdded;
}
QDateTime BitTorrent::TorrentCreationTask::timeStarted() const
{
return m_timeStarted;
}
QDateTime BitTorrent::TorrentCreationTask::timeFinished() const
{
return m_timeFinished;
}

View File

@@ -0,0 +1,81 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2024 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2024 Radu Carpa <radu.carpa@cern.ch>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*/
#pragma once
#include <QDateTime>
#include <QObject>
#include <QString>
#include "base/applicationcomponent.h"
#include "torrentcreator.h"
namespace BitTorrent
{
class TorrentCreationTask final : public ApplicationComponent<QObject>
{
Q_OBJECT
Q_DISABLE_COPY_MOVE(TorrentCreationTask)
public:
enum State
{
Queued,
Running,
Finished
};
TorrentCreationTask(IApplication *app, const QString &id, TorrentCreator *torrentCreator
, bool startSeeding, QObject *parent = nullptr);
QString id() const;
const TorrentCreatorParams &params() const;
State state() const;
bool isQueued() const;
bool isRunning() const;
bool isFinished() const;
bool isFailed() const;
QDateTime timeAdded() const;
QDateTime timeStarted() const;
QDateTime timeFinished() const;
int progress() const;
const TorrentCreatorResult &result() const;
QString errorMsg() const;
private:
QString m_id;
TorrentCreatorParams m_params;
QDateTime m_timeAdded;
QDateTime m_timeStarted;
QDateTime m_timeFinished;
int m_progress = 0;
TorrentCreatorResult m_result;
QString m_errorMsg;
};
}

View File

@@ -1,5 +1,7 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2024 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2024 Radu Carpa <radu.carpa@cern.ch>
* Copyright (C) 2010 Christophe Dumez <chris@qbittorrent.org>
*
* This program is free software; you can redistribute it and/or
@@ -28,7 +30,7 @@
#include "torrentcreator.h"
#include <fstream>
#include <functional>
#include <libtorrent/create_torrent.hpp>
#include <libtorrent/file_storage.hpp>
@@ -41,7 +43,6 @@
#include "base/exceptions.h"
#include "base/global.h"
#include "base/utils/compare.h"
#include "base/utils/fs.h"
#include "base/utils/io.h"
#include "base/version.h"
#include "lttypecast.h"
@@ -82,7 +83,7 @@ TorrentCreator::TorrentCreator(const TorrentCreatorParams &params, QObject *pare
void TorrentCreator::sendProgressSignal(int currentPieceIdx, int totalPieces)
{
emit updateProgress(static_cast<int>((currentPieceIdx * 100.) / totalPieces));
emit progressUpdated(static_cast<int>((currentPieceIdx * 100.) / totalPieces));
}
void TorrentCreator::checkInterruptionRequested() const
@@ -103,25 +104,26 @@ bool TorrentCreator::isInterruptionRequested() const
void TorrentCreator::run()
{
emit updateProgress(0);
emit started();
emit progressUpdated(0);
try
{
const Path parentPath = m_params.inputPath.parentPath();
const Path parentPath = m_params.sourcePath.parentPath();
const Utils::Compare::NaturalLessThan<Qt::CaseInsensitive> naturalLessThan {};
// Adding files to the torrent
lt::file_storage fs;
if (QFileInfo(m_params.inputPath.data()).isFile())
if (QFileInfo(m_params.sourcePath.data()).isFile())
{
lt::add_files(fs, m_params.inputPath.toString().toStdString(), fileFilter);
lt::add_files(fs, m_params.sourcePath.toString().toStdString(), fileFilter);
}
else
{
// need to sort the file names by natural sort order
QStringList dirs = {m_params.inputPath.data()};
QStringList dirs = {m_params.sourcePath.data()};
QDirIterator dirIter {m_params.inputPath.data(), (QDir::AllDirs | QDir::NoDotAndDotDot), QDirIterator::Subdirectories};
QDirIterator dirIter {m_params.sourcePath.data(), (QDir::AllDirs | QDir::NoDotAndDotDot), QDirIterator::Subdirectories};
while (dirIter.hasNext())
{
const QString filePath = dirIter.next();
@@ -205,13 +207,29 @@ void TorrentCreator::run()
checkInterruptionRequested();
// create the torrent
const nonstd::expected<void, QString> result = Utils::IO::saveToFile(m_params.savePath, entry);
const auto result = std::invoke([torrentFilePath = m_params.torrentFilePath, entry]() -> nonstd::expected<Path, QString>
{
if (!torrentFilePath.isValid())
return Utils::IO::saveToTempFile(entry);
const nonstd::expected<void, QString> result = Utils::IO::saveToFile(torrentFilePath, entry);
if (!result)
return nonstd::make_unexpected(result.error());
return torrentFilePath;
});
if (!result)
throw RuntimeError(result.error());
emit updateProgress(100);
emit creationSuccess(m_params.savePath, parentPath);
const BitTorrent::TorrentCreatorResult creatorResult
{
.torrentFilePath = result.value(),
.savePath = parentPath,
.pieceSize = newTorrent.piece_length()
};
emit progressUpdated(100);
emit creationSuccess(creatorResult);
}
catch (const RuntimeError &err)
{
@@ -223,6 +241,11 @@ void TorrentCreator::run()
}
}
const TorrentCreatorParams &TorrentCreator::params() const
{
return m_params;
}
#ifdef QBT_USES_LIBTORRENT2
int TorrentCreator::calculateTotalPieces(const Path &inputPath, const int pieceSize, const TorrentFormat torrentFormat)
#else

View File

@@ -1,5 +1,7 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2024 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2024 Radu Carpa <radu.carpa@cern.ch>
* Copyright (C) 2010 Christophe Dumez <chris@qbittorrent.org>
*
* This program is free software; you can redistribute it and/or
@@ -53,18 +55,25 @@ namespace BitTorrent
#ifdef QBT_USES_LIBTORRENT2
TorrentFormat torrentFormat = TorrentFormat::Hybrid;
#else
bool isAlignmentOptimized;
int paddedFileSizeLimit;
bool isAlignmentOptimized = false;
int paddedFileSizeLimit = 0;
#endif
int pieceSize = 0;
Path inputPath;
Path savePath;
Path sourcePath;
Path torrentFilePath;
QString comment;
QString source;
QStringList trackers;
QStringList urlSeeds;
};
struct TorrentCreatorResult
{
Path torrentFilePath;
Path savePath;
int pieceSize;
};
class TorrentCreator final : public QObject, public QRunnable
{
Q_OBJECT
@@ -73,24 +82,26 @@ namespace BitTorrent
public:
explicit TorrentCreator(const TorrentCreatorParams &params, QObject *parent = nullptr);
void run() override;
const TorrentCreatorParams &params() const;
bool isInterruptionRequested() const;
public slots:
void requestInterruption();
void run() override;
#ifdef QBT_USES_LIBTORRENT2
static int calculateTotalPieces(const Path &inputPath, int pieceSize, TorrentFormat torrentFormat);
#else
static int calculateTotalPieces(const Path &inputPath
, const int pieceSize, const bool isAlignmentOptimized, int paddedFileSizeLimit);
static int calculateTotalPieces(const Path &inputPath, const int pieceSize
, const bool isAlignmentOptimized, int paddedFileSizeLimit);
#endif
public slots:
void requestInterruption();
signals:
void started();
void creationFailure(const QString &msg);
void creationSuccess(const Path &path, const Path &branchPath);
void updateProgress(int progress);
void creationSuccess(const TorrentCreatorResult &result);
void progressUpdated(int progress);
private:
void sendProgressSignal(int currentPieceIdx, int totalPieces);