-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
WebAPI: Support persisting WebUI client data #23088
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* Bittorrent Client using Qt and libtorrent. | ||
* Copyright (C) 2025 Thomas Piccirello <[email protected]> | ||
* | ||
* 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 "clientdatacontroller.h" | ||
|
||
#include <QJsonArray> | ||
#include <QJsonDocument> | ||
#include <QJsonObject> | ||
|
||
#include "base/global.h" | ||
#include "base/interfaces/iapplication.h" | ||
#include "base/logger.h" | ||
#include "apierror.h" | ||
#include "webui/clientdatastorage.h" | ||
|
||
ClientDataController::ClientDataController(ClientDataStorage *clientDataStorage, IApplication *app, QObject *parent) | ||
: APIController(app, parent) | ||
, m_clientDataStorage {clientDataStorage} | ||
{ | ||
} | ||
|
||
void ClientDataController::loadAction() | ||
{ | ||
const QString keysParam {params()[u"keys"_s]}; | ||
if (keysParam.isEmpty()) | ||
{ | ||
setResult(m_clientDataStorage->loadData()); | ||
return; | ||
} | ||
|
||
QJsonParseError jsonError; | ||
const auto keysJsonDocument = QJsonDocument::fromJson(keysParam.toUtf8(), &jsonError); | ||
if (jsonError.error != QJsonParseError::NoError) | ||
throw APIError(APIErrorType::BadParams, jsonError.errorString()); | ||
if (!keysJsonDocument.isArray()) | ||
throw APIError(APIErrorType::BadParams, tr("`keys` must be an array")); | ||
|
||
QStringList keys; | ||
for (const QJsonValue &keysJsonVal : asConst(keysJsonDocument.array())) | ||
{ | ||
if (!keysJsonVal.isString()) | ||
throw APIError(APIErrorType::BadParams, tr("Items of `keys` must be strings")); | ||
|
||
keys << keysJsonVal.toString(); | ||
} | ||
|
||
setResult(m_clientDataStorage->loadData(keys)); | ||
} | ||
|
||
void ClientDataController::storeAction() | ||
{ | ||
requireParams({u"data"_s}); | ||
QJsonParseError jsonError; | ||
const auto dataJsonDocument = QJsonDocument::fromJson(params()[u"data"_s].toUtf8(), &jsonError); | ||
if (jsonError.error != QJsonParseError::NoError) | ||
throw APIError(APIErrorType::BadParams, jsonError.errorString()); | ||
if (!dataJsonDocument.isObject()) | ||
throw APIError(APIErrorType::BadParams, tr("`data` must be an object")); | ||
|
||
const nonstd::expected<void, QString> result = m_clientDataStorage->storeData(dataJsonDocument.object()); | ||
if (!result) | ||
throw APIError(APIErrorType::Conflict, result.error()); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Bittorrent Client using Qt and libtorrent. | ||
* Copyright (C) 2025 Thomas Piccirello <[email protected]> | ||
* | ||
* 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" | ||
|
||
class ClientDataStorage; | ||
|
||
class ClientDataController final : public APIController | ||
{ | ||
Q_OBJECT | ||
Q_DISABLE_COPY_MOVE(ClientDataController) | ||
|
||
public: | ||
ClientDataController(ClientDataStorage *clientDataStorage, IApplication *app, QObject *parent = nullptr); | ||
|
||
private slots: | ||
void loadAction(); | ||
void storeAction(); | ||
|
||
private: | ||
ClientDataStorage *m_clientDataStorage = nullptr; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
/* | ||
* Bittorrent Client using Qt and libtorrent. | ||
* Copyright (C) 2025 Thomas Piccirello <[email protected]> | ||
* | ||
* 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 "clientdatastorage.h" | ||
|
||
#include <QJsonDocument> | ||
#include <QJsonObject> | ||
|
||
#include "base/global.h" | ||
#include "base/logger.h" | ||
#include "base/path.h" | ||
#include "base/profile.h" | ||
#include "base/utils/io.h" | ||
|
||
const int CLIENT_DATA_FILE_MAX_SIZE = 1024 * 1024; // 1 MiB | ||
const QString CLIENT_DATA_FILE_NAME = u"web_clientdata.json"_s; | ||
|
||
ClientDataStorage::ClientDataStorage(QObject *parent) | ||
: QObject(parent) | ||
, m_clientDataFilePath(specialFolderLocation(SpecialFolder::Data) / Path(CLIENT_DATA_FILE_NAME)) | ||
{ | ||
if (!m_clientDataFilePath.exists()) | ||
return; | ||
|
||
const auto readResult = Utils::IO::readFile(m_clientDataFilePath, CLIENT_DATA_FILE_MAX_SIZE); | ||
if (!readResult) | ||
{ | ||
LogMsg(tr("Failed to load web client data. %1").arg(readResult.error().message), Log::WARNING); | ||
return; | ||
} | ||
|
||
QJsonParseError jsonError; | ||
const QJsonDocument jsonDoc = QJsonDocument::fromJson(readResult.value(), &jsonError); | ||
if (jsonError.error != QJsonParseError::NoError) | ||
{ | ||
LogMsg(tr("Failed to parse web client data. File: \"%1\". Error: \"%2\"") | ||
.arg(m_clientDataFilePath.toString(), jsonError.errorString()), Log::WARNING); | ||
return; | ||
} | ||
|
||
if (!jsonDoc.isObject()) | ||
{ | ||
LogMsg(tr("Failed to load web client data. File: \"%1\". Error: \"Invalid data format\"") | ||
.arg(m_clientDataFilePath.toString()), Log::WARNING); | ||
return; | ||
} | ||
|
||
m_clientData = jsonDoc.object(); | ||
} | ||
|
||
nonstd::expected<void, QString> ClientDataStorage::storeData(const QJsonObject &object) | ||
{ | ||
QJsonObject clientData = m_clientData; | ||
bool dataModified = false; | ||
for (auto it = object.constBegin(), end = object.constEnd(); it != end; ++it) | ||
{ | ||
const QString &key = it.key(); | ||
const QJsonValue &value = it.value(); | ||
|
||
if (value.isNull()) | ||
{ | ||
if (auto it = clientData.find(key); it != clientData.end()) | ||
{ | ||
clientData.erase(it); | ||
dataModified = true; | ||
} | ||
} | ||
else | ||
{ | ||
const auto &existingValue = clientData.find(key); | ||
if (existingValue == clientData.end()) | ||
{ | ||
clientData.insert(key, value); | ||
dataModified = true; | ||
} | ||
else if (existingValue.value() != value) | ||
{ | ||
existingValue.value() = value; | ||
dataModified = true; | ||
} | ||
} | ||
} | ||
|
||
if (dataModified) | ||
{ | ||
const QByteArray json = QJsonDocument(clientData).toJson(QJsonDocument::Compact); | ||
if (json.size() > CLIENT_DATA_FILE_MAX_SIZE) | ||
return nonstd::make_unexpected(tr("Total web client data must not be larger than %1 bytes").arg(CLIENT_DATA_FILE_MAX_SIZE)); | ||
const nonstd::expected<void, QString> result = Utils::IO::saveToFile(m_clientDataFilePath, json); | ||
if (!result) | ||
return nonstd::make_unexpected(tr("Failed to save web client data. Error: \"%1\"").arg(result.error())); | ||
|
||
m_clientData = clientData; | ||
} | ||
|
||
return {}; | ||
} | ||
|
||
QJsonObject ClientDataStorage::loadData() const | ||
{ | ||
return m_clientData; | ||
} | ||
|
||
QJsonObject ClientDataStorage::loadData(const QStringList &keys) const | ||
{ | ||
QJsonObject clientData; | ||
for (const QString &key : keys) | ||
{ | ||
if (const auto iter = m_clientData.constFind(key); iter != m_clientData.constEnd()) | ||
clientData.insert(key, iter.value()); | ||
} | ||
return clientData; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Bittorrent Client using Qt and libtorrent. | ||
* Copyright (C) 2025 Thomas Piccirello <[email protected]> | ||
* | ||
* 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 <QJsonObject> | ||
|
||
#include "base/3rdparty/expected.hpp" | ||
#include "base/path.h" | ||
|
||
class ClientDataStorage final : public QObject | ||
{ | ||
Q_OBJECT | ||
Q_DISABLE_COPY_MOVE(ClientDataStorage) | ||
|
||
public: | ||
ClientDataStorage(QObject *parent = nullptr); | ||
|
||
nonstd::expected<void, QString> storeData(const QJsonObject &object); | ||
QJsonObject loadData() const; | ||
QJsonObject loadData(const QStringList &keys) const; | ||
|
||
private: | ||
Path m_clientDataFilePath; | ||
QJsonObject m_clientData; | ||
}; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.