mirror of
https://github.com/qbittorrent/qBittorrent.git
synced 2024-11-27 11:40:33 +00:00
Provide torrent creation feature via WebAPI
PR #20366. Closes #5614. Co-authored-by: Radu Carpa <radu.carpa@cern.ch>
This commit is contained in:
parent
15697f904d
commit
0114610a40
@ -37,6 +37,8 @@ add_library(qbt_base STATIC
|
||||
bittorrent/torrent.h
|
||||
bittorrent/torrentcontenthandler.h
|
||||
bittorrent/torrentcontentlayout.h
|
||||
bittorrent/torrentcreationmanager.h
|
||||
bittorrent/torrentcreationtask.h
|
||||
bittorrent/torrentcreator.h
|
||||
bittorrent/torrentdescriptor.h
|
||||
bittorrent/torrentimpl.h
|
||||
@ -140,6 +142,8 @@ add_library(qbt_base STATIC
|
||||
bittorrent/sslparameters.cpp
|
||||
bittorrent/torrent.cpp
|
||||
bittorrent/torrentcontenthandler.cpp
|
||||
bittorrent/torrentcreationmanager.cpp
|
||||
bittorrent/torrentcreationtask.cpp
|
||||
bittorrent/torrentcreator.cpp
|
||||
bittorrent/torrentdescriptor.cpp
|
||||
bittorrent/torrentimpl.cpp
|
||||
|
134
src/base/bittorrent/torrentcreationmanager.cpp
Normal file
134
src/base/bittorrent/torrentcreationmanager.cpp
Normal 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 ¶ms, 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;
|
||||
}
|
70
src/base/bittorrent/torrentcreationmanager.h
Normal file
70
src/base/bittorrent/torrentcreationmanager.h
Normal 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 ¶ms, 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;
|
||||
};
|
||||
}
|
149
src/base/bittorrent/torrentcreationtask.cpp
Normal file
149
src/base/bittorrent/torrentcreationtask.cpp
Normal 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;
|
||||
}
|
81
src/base/bittorrent/torrentcreationtask.h
Normal file
81
src/base/bittorrent/torrentcreationtask.h
Normal 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 ¶ms() 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;
|
||||
};
|
||||
}
|
@ -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 ¶ms, 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
|
||||
|
@ -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 ¶ms, QObject *parent = nullptr);
|
||||
|
||||
void run() override;
|
||||
|
||||
const TorrentCreatorParams ¶ms() 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);
|
||||
|
@ -30,7 +30,6 @@
|
||||
#include "downloadhandlerimpl.h"
|
||||
|
||||
#include <QtSystemDetection>
|
||||
#include <QTemporaryFile>
|
||||
#include <QUrl>
|
||||
|
||||
#include "base/3rdparty/expected.hpp"
|
||||
@ -49,19 +48,6 @@
|
||||
|
||||
const int MAX_REDIRECTIONS = 20; // the common value for web browsers
|
||||
|
||||
namespace
|
||||
{
|
||||
nonstd::expected<Path, QString> saveToTempFile(const QByteArray &data)
|
||||
{
|
||||
QTemporaryFile file {Utils::Fs::tempPath().data()};
|
||||
if (!file.open() || (file.write(data) != data.length()) || !file.flush())
|
||||
return nonstd::make_unexpected(file.errorString());
|
||||
|
||||
file.setAutoRemove(false);
|
||||
return Path(file.fileName());
|
||||
}
|
||||
}
|
||||
|
||||
Net::DownloadHandlerImpl::DownloadHandlerImpl(DownloadManager *manager
|
||||
, const DownloadRequest &downloadRequest, const bool useProxy)
|
||||
: DownloadHandler {manager}
|
||||
@ -163,7 +149,7 @@ void Net::DownloadHandlerImpl::processFinishedDownload()
|
||||
const Path destinationPath = m_downloadRequest.destFileName();
|
||||
if (destinationPath.isEmpty())
|
||||
{
|
||||
const nonstd::expected<Path, QString> result = saveToTempFile(m_result.data);
|
||||
const nonstd::expected<Path, QString> result = Utils::IO::saveToTempFile(m_result.data);
|
||||
if (result)
|
||||
{
|
||||
m_result.filePath = result.value();
|
||||
|
@ -40,6 +40,7 @@
|
||||
#include <QFileDevice>
|
||||
#include <QSaveFile>
|
||||
#include <QString>
|
||||
#include <QTemporaryFile>
|
||||
|
||||
#include "base/path.h"
|
||||
#include "base/utils/fs.h"
|
||||
@ -149,3 +150,27 @@ nonstd::expected<void, QString> Utils::IO::saveToFile(const Path &path, const lt
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
nonstd::expected<Path, QString> Utils::IO::saveToTempFile(const QByteArray &data)
|
||||
{
|
||||
QTemporaryFile file {(Utils::Fs::tempPath() / Path(u"file_"_s)).data()};
|
||||
if (!file.open() || (file.write(data) != data.length()) || !file.flush())
|
||||
return nonstd::make_unexpected(file.errorString());
|
||||
|
||||
file.setAutoRemove(false);
|
||||
return Path(file.fileName());
|
||||
}
|
||||
|
||||
nonstd::expected<Path, QString> Utils::IO::saveToTempFile(const lt::entry &data)
|
||||
{
|
||||
QTemporaryFile file {(Utils::Fs::tempPath() / Path(u"file_"_s)).data()};
|
||||
if (!file.open())
|
||||
return nonstd::make_unexpected(file.errorString());
|
||||
|
||||
const int bencodedDataSize = lt::bencode(Utils::IO::FileDeviceOutputIterator {file}, data);
|
||||
if ((file.size() != bencodedDataSize) || !file.flush())
|
||||
return nonstd::make_unexpected(file.errorString());
|
||||
|
||||
file.setAutoRemove(false);
|
||||
return Path(file.fileName());
|
||||
}
|
||||
|
@ -103,4 +103,6 @@ namespace Utils::IO
|
||||
|
||||
nonstd::expected<void, QString> saveToFile(const Path &path, const QByteArray &data);
|
||||
nonstd::expected<void, QString> saveToFile(const Path &path, const lt::entry &data);
|
||||
nonstd::expected<Path, QString> saveToTempFile(const QByteArray &data);
|
||||
nonstd::expected<Path, QString> saveToTempFile(const lt::entry &data);
|
||||
}
|
||||
|
@ -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) 2017 Mike Tzou (Chocobo1)
|
||||
* Copyright (C) 2010 Christophe Dumez <chris@qbittorrent.org>
|
||||
*
|
||||
@ -235,8 +237,8 @@ void TorrentCreatorDialog::onCreateButtonClicked()
|
||||
.paddedFileSizeLimit = getPaddedFileSizeLimit(),
|
||||
#endif
|
||||
.pieceSize = getPieceSize(),
|
||||
.inputPath = inputPath,
|
||||
.savePath = destPath,
|
||||
.sourcePath = inputPath,
|
||||
.torrentFilePath = destPath,
|
||||
.comment = m_ui->txtComment->toPlainText(),
|
||||
.source = m_ui->lineEditSource->text(),
|
||||
.trackers = trackers,
|
||||
@ -247,7 +249,7 @@ void TorrentCreatorDialog::onCreateButtonClicked()
|
||||
connect(this, &QDialog::rejected, torrentCreator, &BitTorrent::TorrentCreator::requestInterruption);
|
||||
connect(torrentCreator, &BitTorrent::TorrentCreator::creationSuccess, this, &TorrentCreatorDialog::handleCreationSuccess);
|
||||
connect(torrentCreator, &BitTorrent::TorrentCreator::creationFailure, this, &TorrentCreatorDialog::handleCreationFailure);
|
||||
connect(torrentCreator, &BitTorrent::TorrentCreator::updateProgress, this, &TorrentCreatorDialog::updateProgressBar);
|
||||
connect(torrentCreator, &BitTorrent::TorrentCreator::progressUpdated, this, &TorrentCreatorDialog::updateProgressBar);
|
||||
|
||||
// run the torrentCreator in a thread
|
||||
m_threadPool.start(torrentCreator);
|
||||
@ -261,36 +263,36 @@ void TorrentCreatorDialog::handleCreationFailure(const QString &msg)
|
||||
setInteractionEnabled(true);
|
||||
}
|
||||
|
||||
void TorrentCreatorDialog::handleCreationSuccess(const Path &path, const Path &branchPath)
|
||||
void TorrentCreatorDialog::handleCreationSuccess(const BitTorrent::TorrentCreatorResult &result)
|
||||
{
|
||||
setCursor(QCursor(Qt::ArrowCursor));
|
||||
setInteractionEnabled(true);
|
||||
|
||||
QMessageBox::information(this, tr("Torrent creator")
|
||||
, u"%1\n%2"_s.arg(tr("Torrent created:"), path.toString()));
|
||||
, u"%1\n%2"_s.arg(tr("Torrent created:"), result.torrentFilePath.toString()));
|
||||
|
||||
if (m_ui->checkStartSeeding->isChecked())
|
||||
{
|
||||
const auto loadResult = BitTorrent::TorrentDescriptor::loadFromFile(path);
|
||||
if (!loadResult)
|
||||
if (const auto loadResult = BitTorrent::TorrentDescriptor::loadFromFile(result.torrentFilePath))
|
||||
{
|
||||
BitTorrent::AddTorrentParams params;
|
||||
params.savePath = result.savePath;
|
||||
params.skipChecking = true;
|
||||
if (m_ui->checkIgnoreShareLimits->isChecked())
|
||||
{
|
||||
params.ratioLimit = BitTorrent::Torrent::NO_RATIO_LIMIT;
|
||||
params.seedingTimeLimit = BitTorrent::Torrent::NO_SEEDING_TIME_LIMIT;
|
||||
params.inactiveSeedingTimeLimit = BitTorrent::Torrent::NO_INACTIVE_SEEDING_TIME_LIMIT;
|
||||
}
|
||||
params.useAutoTMM = false; // otherwise if it is on by default, it will overwrite `savePath` to the default save path
|
||||
|
||||
BitTorrent::Session::instance()->addTorrent(loadResult.value(), params);
|
||||
}
|
||||
else
|
||||
{
|
||||
const QString message = tr("Add torrent to transfer list failed.") + u'\n' + tr("Reason: \"%1\"").arg(loadResult.error());
|
||||
QMessageBox::critical(this, tr("Add torrent failed"), message);
|
||||
return;
|
||||
}
|
||||
|
||||
BitTorrent::AddTorrentParams params;
|
||||
params.savePath = branchPath;
|
||||
params.skipChecking = true;
|
||||
if (m_ui->checkIgnoreShareLimits->isChecked())
|
||||
{
|
||||
params.ratioLimit = BitTorrent::Torrent::NO_RATIO_LIMIT;
|
||||
params.seedingTimeLimit = BitTorrent::Torrent::NO_SEEDING_TIME_LIMIT;
|
||||
params.inactiveSeedingTimeLimit = BitTorrent::Torrent::NO_INACTIVE_SEEDING_TIME_LIMIT;
|
||||
}
|
||||
params.useAutoTMM = false; // otherwise if it is on by default, it will overwrite `savePath` to the default save path
|
||||
|
||||
BitTorrent::Session::instance()->addTorrent(loadResult.value(), params);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
/*
|
||||
* Bittorrent Client using Qt and libtorrent.
|
||||
* Copyright (C) 2024 Radu Carpa <radu.carpa@cern.ch>
|
||||
* Copyright (C) 2017 Mike Tzou (Chocobo1)
|
||||
* Copyright (C) 2010 Christophe Dumez <chris@qbittorrent.org>
|
||||
*
|
||||
@ -58,7 +59,7 @@ private slots:
|
||||
void onAddFileButtonClicked();
|
||||
void onAddFolderButtonClicked();
|
||||
void handleCreationFailure(const QString &msg);
|
||||
void handleCreationSuccess(const Path &path, const Path &branchPath);
|
||||
void handleCreationSuccess(const BitTorrent::TorrentCreatorResult &result);
|
||||
|
||||
private:
|
||||
void dropEvent(QDropEvent *event) override;
|
||||
|
@ -9,6 +9,7 @@ add_library(qbt_webui STATIC
|
||||
api/rsscontroller.h
|
||||
api/searchcontroller.h
|
||||
api/synccontroller.h
|
||||
api/torrentcreatorcontroller.h
|
||||
api/torrentscontroller.h
|
||||
api/transfercontroller.h
|
||||
api/serialize/serialize_torrent.h
|
||||
@ -25,6 +26,7 @@ add_library(qbt_webui STATIC
|
||||
api/rsscontroller.cpp
|
||||
api/searchcontroller.cpp
|
||||
api/synccontroller.cpp
|
||||
api/torrentcreatorcontroller.cpp
|
||||
api/torrentscontroller.cpp
|
||||
api/transfercontroller.cpp
|
||||
api/serialize/serialize_torrent.cpp
|
||||
|
247
src/webui/api/torrentcreatorcontroller.cpp
Normal file
247
src/webui/api/torrentcreatorcontroller.cpp
Normal file
@ -0,0 +1,247 @@
|
||||
/*
|
||||
* 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 "torrentcreatorcontroller.h"
|
||||
|
||||
#include <QJsonArray>
|
||||
#include <QJsonObject>
|
||||
#include <QStringList>
|
||||
|
||||
#include "base/global.h"
|
||||
#include "base/bittorrent/torrentcreationmanager.h"
|
||||
#include "base/preferences.h"
|
||||
#include "base/utils/io.h"
|
||||
#include "base/utils/string.h"
|
||||
#include "apierror.h"
|
||||
|
||||
const QString KEY_COMMENT = u"comment"_s;
|
||||
const QString KEY_ERROR_MESSAGE = u"errorMessage"_s;
|
||||
const QString KEY_FORMAT = u"format"_s;
|
||||
const QString KEY_OPTIMIZE_ALIGNMENT = u"optimizeAlignment"_s;
|
||||
const QString KEY_PADDED_FILE_SIZE_LIMIT = u"paddedFileSizeLimit"_s;
|
||||
const QString KEY_PIECE_SIZE = u"pieceSize"_s;
|
||||
const QString KEY_PRIVATE = u"private"_s;
|
||||
const QString KEY_PROGRESS = u"progress"_s;
|
||||
const QString KEY_SOURCE = u"source"_s;
|
||||
const QString KEY_SOURCE_PATH = u"sourcePath"_s;
|
||||
const QString KEY_STATUS = u"status"_s;
|
||||
const QString KEY_TASK_ID = u"taskID"_s;
|
||||
const QString KEY_TIME_ADDED = u"timeAdded"_s;
|
||||
const QString KEY_TIME_FINISHED = u"timeFinished"_s;
|
||||
const QString KEY_TIME_STARTED = u"timeStarted"_s;
|
||||
const QString KEY_TORRENT_FILE_PATH = u"torrentFilePath"_s;
|
||||
const QString KEY_TRACKERS = u"trackers"_s;
|
||||
const QString KEY_URL_SEEDS = u"urlSeeds"_s;
|
||||
|
||||
namespace
|
||||
{
|
||||
using Utils::String::parseBool;
|
||||
using Utils::String::parseInt;
|
||||
|
||||
#ifdef QBT_USES_LIBTORRENT2
|
||||
BitTorrent::TorrentFormat parseTorrentFormat(const QString &str)
|
||||
{
|
||||
if (str == u"v1")
|
||||
return BitTorrent::TorrentFormat::V1;
|
||||
if (str == u"v2")
|
||||
return BitTorrent::TorrentFormat::V2;
|
||||
return BitTorrent::TorrentFormat::Hybrid;
|
||||
}
|
||||
|
||||
QString torrentFormatToString(const BitTorrent::TorrentFormat torrentFormat)
|
||||
{
|
||||
switch (torrentFormat)
|
||||
{
|
||||
case BitTorrent::TorrentFormat::V1:
|
||||
return u"v1"_s;
|
||||
case BitTorrent::TorrentFormat::V2:
|
||||
return u"v2"_s;
|
||||
default:
|
||||
return u"hybrid"_s;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
QString taskStatusString(const std::shared_ptr<BitTorrent::TorrentCreationTask> task)
|
||||
{
|
||||
if (task->isFailed())
|
||||
return u"Failed"_s;
|
||||
|
||||
switch (task->state())
|
||||
{
|
||||
case BitTorrent::TorrentCreationTask::Queued:
|
||||
default:
|
||||
return u"Queued"_s;
|
||||
case BitTorrent::TorrentCreationTask::Running:
|
||||
return u"Running"_s;
|
||||
case BitTorrent::TorrentCreationTask::Finished:
|
||||
return u"Finished"_s;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TorrentCreatorController::TorrentCreatorController(BitTorrent::TorrentCreationManager *torrentCreationManager, IApplication *app, QObject *parent)
|
||||
: APIController(app, parent)
|
||||
, m_torrentCreationManager {torrentCreationManager}
|
||||
{
|
||||
}
|
||||
|
||||
void TorrentCreatorController::addTaskAction()
|
||||
{
|
||||
requireParams({KEY_SOURCE_PATH});
|
||||
|
||||
const BitTorrent::TorrentCreatorParams createTorrentParams
|
||||
{
|
||||
.isPrivate = parseBool(params()[KEY_PRIVATE]).value_or(false),
|
||||
#ifdef QBT_USES_LIBTORRENT2
|
||||
.torrentFormat = parseTorrentFormat(params()[KEY_FORMAT].toLower()),
|
||||
#else
|
||||
.isAlignmentOptimized = parseBool(params()[KEY_OPTIMIZE_ALIGNMENT]).value_or(true),
|
||||
.paddedFileSizeLimit = parseInt(params()[KEY_PADDED_FILE_SIZE_LIMIT]).value_or(-1),
|
||||
#endif
|
||||
.pieceSize = parseInt(params()[KEY_PIECE_SIZE]).value_or(0),
|
||||
.sourcePath = Path(params()[KEY_SOURCE_PATH]),
|
||||
.torrentFilePath = Path(params()[KEY_TORRENT_FILE_PATH]),
|
||||
.comment = params()[KEY_COMMENT],
|
||||
.source = params()[KEY_COMMENT],
|
||||
.trackers = params()[KEY_TRACKERS].split(u'|'),
|
||||
.urlSeeds = params()[KEY_URL_SEEDS].split(u'|')
|
||||
};
|
||||
|
||||
bool const startSeeding = parseBool(params()[u"startSeeding"_s]).value_or(createTorrentParams.torrentFilePath.isEmpty());
|
||||
|
||||
auto task = m_torrentCreationManager->createTask(createTorrentParams, startSeeding);
|
||||
if (!task)
|
||||
throw APIError(APIErrorType::Conflict, tr("Too many active tasks"));
|
||||
|
||||
setResult(QJsonObject {{KEY_TASK_ID, task->id()}});
|
||||
}
|
||||
|
||||
void TorrentCreatorController::statusAction()
|
||||
{
|
||||
const QString id = params()[KEY_TASK_ID];
|
||||
|
||||
const auto singleTask = m_torrentCreationManager->getTask(id);
|
||||
if (!id.isEmpty() && !singleTask)
|
||||
throw APIError(APIErrorType::NotFound);
|
||||
|
||||
const QList<std::shared_ptr<BitTorrent::TorrentCreationTask>> tasks = id.isEmpty()
|
||||
? m_torrentCreationManager->tasks()
|
||||
: QList<std::shared_ptr<BitTorrent::TorrentCreationTask>> {singleTask};
|
||||
QJsonArray statusArray;
|
||||
for (const auto &task : tasks)
|
||||
{
|
||||
QJsonObject taskJson {
|
||||
{KEY_TASK_ID, task->id()},
|
||||
{KEY_SOURCE_PATH, task->params().sourcePath.toString()},
|
||||
{KEY_PIECE_SIZE, task->params().pieceSize},
|
||||
{KEY_PRIVATE, task->params().isPrivate},
|
||||
{KEY_TIME_ADDED, task->timeAdded().toString()},
|
||||
#ifdef QBT_USES_LIBTORRENT2
|
||||
{KEY_FORMAT, torrentFormatToString(task->params().torrentFormat)},
|
||||
#else
|
||||
{KEY_OPTIMIZE_ALIGNMENT, task->params().isAlignmentOptimized},
|
||||
{KEY_PADDED_FILE_SIZE_LIMIT, task->params().paddedFileSizeLimit},
|
||||
#endif
|
||||
{KEY_STATUS, taskStatusString(task)},
|
||||
};
|
||||
|
||||
if (!task->params().comment.isEmpty())
|
||||
taskJson[KEY_COMMENT] = task->params().comment;
|
||||
|
||||
if (!task->params().torrentFilePath.isEmpty())
|
||||
taskJson[KEY_TORRENT_FILE_PATH] = task->params().torrentFilePath.toString();
|
||||
|
||||
if (!task->params().source.isEmpty())
|
||||
taskJson[KEY_SOURCE] = task->params().source;
|
||||
|
||||
if (!task->params().trackers.isEmpty())
|
||||
taskJson[KEY_TRACKERS] = QJsonArray::fromStringList(task->params().trackers);
|
||||
|
||||
if (!task->params().urlSeeds.isEmpty())
|
||||
taskJson[KEY_URL_SEEDS] = QJsonArray::fromStringList(task->params().urlSeeds);
|
||||
|
||||
if (const QDateTime timeStarted = task->timeStarted(); !timeStarted.isNull())
|
||||
taskJson[KEY_TIME_STARTED] = timeStarted.toString();
|
||||
|
||||
if (const QDateTime timeFinished = task->timeFinished(); !task->timeFinished().isNull())
|
||||
taskJson[KEY_TIME_FINISHED] = timeFinished.toString();
|
||||
|
||||
if (task->isFinished())
|
||||
{
|
||||
if (task->isFailed())
|
||||
{
|
||||
taskJson[KEY_ERROR_MESSAGE] = task->errorMsg();
|
||||
}
|
||||
else
|
||||
{
|
||||
taskJson[KEY_PIECE_SIZE] = task->result().pieceSize;
|
||||
}
|
||||
}
|
||||
else if (task->isRunning())
|
||||
{
|
||||
taskJson[KEY_PROGRESS] = task->progress();
|
||||
}
|
||||
|
||||
statusArray.append(taskJson);
|
||||
}
|
||||
|
||||
setResult(statusArray);
|
||||
}
|
||||
|
||||
void TorrentCreatorController::torrentFileAction()
|
||||
{
|
||||
requireParams({KEY_TASK_ID});
|
||||
const QString id = params()[KEY_TASK_ID];
|
||||
|
||||
const auto task = m_torrentCreationManager->getTask(id);
|
||||
if (!task)
|
||||
throw APIError(APIErrorType::NotFound);
|
||||
|
||||
if (!task->isFinished())
|
||||
throw APIError(APIErrorType::Conflict, tr("Torrent creation is still unfinished."));
|
||||
|
||||
if (task->isFailed())
|
||||
throw APIError(APIErrorType::Conflict, tr("Torrent creation failed."));
|
||||
|
||||
const auto readResult = Utils::IO::readFile(task->result().torrentFilePath, Preferences::instance()->getTorrentFileSizeLimit());
|
||||
if (!readResult)
|
||||
throw APIError(APIErrorType::Conflict, readResult.error().message);
|
||||
|
||||
setResult(readResult.value(), u"application/x-bittorrent"_s, (id + u".torrent"));
|
||||
}
|
||||
|
||||
void TorrentCreatorController::deleteTaskAction()
|
||||
{
|
||||
requireParams({KEY_TASK_ID});
|
||||
const QString id = params()[KEY_TASK_ID];
|
||||
|
||||
if (!m_torrentCreationManager->deleteTask(id))
|
||||
throw APIError(APIErrorType::NotFound);
|
||||
}
|
55
src/webui/api/torrentcreatorcontroller.h
Normal file
55
src/webui/api/torrentcreatorcontroller.h
Normal file
@ -0,0 +1,55 @@
|
||||
/*
|
||||
* 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 "apicontroller.h"
|
||||
|
||||
namespace BitTorrent
|
||||
{
|
||||
class TorrentCreationManager;
|
||||
}
|
||||
|
||||
class TorrentCreatorController final : public APIController
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_DISABLE_COPY_MOVE(TorrentCreatorController)
|
||||
|
||||
public:
|
||||
TorrentCreatorController(BitTorrent::TorrentCreationManager *torrentCreationManager, IApplication *app, QObject *parent = nullptr);
|
||||
|
||||
private slots:
|
||||
void addTaskAction();
|
||||
void statusAction();
|
||||
void torrentFileAction();
|
||||
void deleteTaskAction();
|
||||
|
||||
private:
|
||||
BitTorrent::TorrentCreationManager *m_torrentCreationManager = nullptr;
|
||||
};
|
@ -1,6 +1,7 @@
|
||||
/*
|
||||
* Bittorrent Client using Qt and libtorrent.
|
||||
* Copyright (C) 2014, 2022-2023 Vladimir Golovnev <glassez@yandex.ru>
|
||||
* Copyright (C) 2014-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
|
||||
@ -46,6 +47,7 @@
|
||||
#include <QUrl>
|
||||
|
||||
#include "base/algorithm.h"
|
||||
#include "base/bittorrent/torrentcreationmanager.h"
|
||||
#include "base/http/httperror.h"
|
||||
#include "base/logger.h"
|
||||
#include "base/preferences.h"
|
||||
@ -62,6 +64,7 @@
|
||||
#include "api/rsscontroller.h"
|
||||
#include "api/searchcontroller.h"
|
||||
#include "api/synccontroller.h"
|
||||
#include "api/torrentcreatorcontroller.h"
|
||||
#include "api/torrentscontroller.h"
|
||||
#include "api/transfercontroller.h"
|
||||
#include "freediskspacechecker.h"
|
||||
@ -112,9 +115,8 @@ namespace
|
||||
if (contentType.startsWith(u"image/"))
|
||||
return u"private, max-age=604800"_s; // 1 week
|
||||
|
||||
if ((contentType == Http::CONTENT_TYPE_CSS)
|
||||
|| (contentType == Http::CONTENT_TYPE_JS))
|
||||
{
|
||||
if ((contentType == Http::CONTENT_TYPE_CSS) || (contentType == Http::CONTENT_TYPE_JS))
|
||||
{
|
||||
// short interval in case of program update
|
||||
return u"private, max-age=43200"_s; // 12 hrs
|
||||
}
|
||||
@ -162,6 +164,7 @@ WebApplication::WebApplication(IApplication *app, QObject *parent)
|
||||
, m_workerThread {new QThread}
|
||||
, m_freeDiskSpaceChecker {new FreeDiskSpaceChecker}
|
||||
, m_freeDiskSpaceCheckingTimer {new QTimer(this)}
|
||||
, m_torrentCreationManager {new BitTorrent::TorrentCreationManager(app, this)}
|
||||
{
|
||||
declarePublicAPI(u"auth/login"_s);
|
||||
|
||||
@ -724,16 +727,18 @@ void WebApplication::sessionStart()
|
||||
m_currentSession = new WebSession(generateSid(), app());
|
||||
m_sessions[m_currentSession->id()] = m_currentSession;
|
||||
|
||||
m_currentSession->registerAPIController<AppController>(u"app"_s);
|
||||
m_currentSession->registerAPIController<LogController>(u"log"_s);
|
||||
m_currentSession->registerAPIController<RSSController>(u"rss"_s);
|
||||
m_currentSession->registerAPIController<SearchController>(u"search"_s);
|
||||
m_currentSession->registerAPIController<TorrentsController>(u"torrents"_s);
|
||||
m_currentSession->registerAPIController<TransferController>(u"transfer"_s);
|
||||
m_currentSession->registerAPIController(u"app"_s, new AppController(app(), this));
|
||||
m_currentSession->registerAPIController(u"log"_s, new LogController(app(), this));
|
||||
m_currentSession->registerAPIController(u"torrentcreator"_s, new TorrentCreatorController(m_torrentCreationManager, app(), this));
|
||||
m_currentSession->registerAPIController(u"rss"_s, new RSSController(app(), this));
|
||||
m_currentSession->registerAPIController(u"search"_s, new SearchController(app(), this));
|
||||
m_currentSession->registerAPIController(u"torrents"_s, new TorrentsController(app(), this));
|
||||
m_currentSession->registerAPIController(u"transfer"_s, new TransferController(app(), this));
|
||||
|
||||
auto *syncController = m_currentSession->registerAPIController<SyncController>(u"sync"_s);
|
||||
auto *syncController = new SyncController(app(), this);
|
||||
syncController->updateFreeDiskSpace(m_freeDiskSpaceChecker->lastResult());
|
||||
connect(m_freeDiskSpaceChecker, &FreeDiskSpaceChecker::checked, syncController, &SyncController::updateFreeDiskSpace);
|
||||
m_currentSession->registerAPIController(u"sync"_s, syncController);
|
||||
|
||||
QNetworkCookie cookie {m_sessionCookieName.toLatin1(), m_currentSession->id().toUtf8()};
|
||||
cookie.setHttpOnly(true);
|
||||
@ -908,6 +913,12 @@ void WebSession::updateTimestamp()
|
||||
m_timer.start();
|
||||
}
|
||||
|
||||
void WebSession::registerAPIController(const QString &scope, APIController *controller)
|
||||
{
|
||||
Q_ASSERT(controller);
|
||||
m_apiControllers[scope] = controller;
|
||||
}
|
||||
|
||||
APIController *WebSession::getAPIController(const QString &scope) const
|
||||
{
|
||||
return m_apiControllers.value(scope);
|
||||
|
@ -1,6 +1,7 @@
|
||||
/*
|
||||
* Bittorrent Client using Qt and libtorrent.
|
||||
* Copyright (C) 2014, 2017, 2022-2023 Vladimir Golovnev <glassez@yandex.ru>
|
||||
* Copyright (C) 2014-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
|
||||
@ -62,6 +63,11 @@ class AuthController;
|
||||
class FreeDiskSpaceChecker;
|
||||
class WebApplication;
|
||||
|
||||
namespace BitTorrent
|
||||
{
|
||||
class TorrentCreationManager;
|
||||
}
|
||||
|
||||
class WebSession final : public ApplicationComponent<QObject>, public ISession
|
||||
{
|
||||
public:
|
||||
@ -72,15 +78,7 @@ public:
|
||||
bool hasExpired(qint64 seconds) const;
|
||||
void updateTimestamp();
|
||||
|
||||
template <typename T>
|
||||
T *registerAPIController(const QString &scope)
|
||||
{
|
||||
static_assert(std::is_base_of_v<APIController, T>, "Class should be derived from APIController.");
|
||||
auto *controller = new T(app(), this);
|
||||
m_apiControllers[scope] = controller;
|
||||
return controller;
|
||||
}
|
||||
|
||||
void registerAPIController(const QString &scope, APIController *controller);
|
||||
APIController *getAPIController(const QString &scope) const;
|
||||
|
||||
private:
|
||||
@ -172,6 +170,8 @@ private:
|
||||
{{u"search"_s, u"stop"_s}, Http::METHOD_POST},
|
||||
{{u"search"_s, u"uninstallPlugin"_s}, Http::METHOD_POST},
|
||||
{{u"search"_s, u"updatePlugins"_s}, Http::METHOD_POST},
|
||||
{{u"torrentcreator"_s, u"addTask"_s}, Http::METHOD_POST},
|
||||
{{u"torrentcreator"_s, u"deleteTask"_s}, Http::METHOD_POST},
|
||||
{{u"torrents"_s, u"add"_s}, Http::METHOD_POST},
|
||||
{{u"torrents"_s, u"addPeers"_s}, Http::METHOD_POST},
|
||||
{{u"torrents"_s, u"addTags"_s}, Http::METHOD_POST},
|
||||
@ -254,4 +254,5 @@ private:
|
||||
Utils::Thread::UniquePtr m_workerThread;
|
||||
FreeDiskSpaceChecker *m_freeDiskSpaceChecker = nullptr;
|
||||
QTimer *m_freeDiskSpaceCheckingTimer = nullptr;
|
||||
BitTorrent::TorrentCreationManager *m_torrentCreationManager = nullptr;
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user