Move ImageUploader into a directory, refactor out UploadedImage

and all its template implementations
This commit is contained in:
Mm2PL 2023-12-02 00:43:04 +01:00
parent d7a888d651
commit 6e8af5e3b3
No known key found for this signature in database
GPG key ID: 94AC9B80EFA15ED9
6 changed files with 130 additions and 9 deletions

View file

@ -12,7 +12,7 @@
#include "controllers/notifications/NotificationController.hpp" #include "controllers/notifications/NotificationController.hpp"
#include "controllers/sound/ISoundController.hpp" #include "controllers/sound/ISoundController.hpp"
#include "providers/seventv/SeventvAPI.hpp" #include "providers/seventv/SeventvAPI.hpp"
#include "singletons/ImageUploader.hpp" #include "singletons/imageuploader/ImageUploader.hpp"
#ifdef CHATTERINO_HAVE_PLUGINS #ifdef CHATTERINO_HAVE_PLUGINS
# include "controllers/plugins/PluginController.hpp" # include "controllers/plugins/PluginController.hpp"
#endif #endif

View file

@ -429,8 +429,9 @@ set(SOURCE_FILES
singletons/Emotes.hpp singletons/Emotes.hpp
singletons/Fonts.cpp singletons/Fonts.cpp
singletons/Fonts.hpp singletons/Fonts.hpp
singletons/ImageUploader.cpp singletons/imageuploader/ImageUploader.cpp
singletons/ImageUploader.hpp singletons/imageuploader/ImageUploader.hpp
singletons/imageuploader/UploadedImage.hpp
singletons/Logging.cpp singletons/Logging.cpp
singletons/Logging.hpp singletons/Logging.hpp
singletons/NativeMessaging.cpp singletons/NativeMessaging.cpp

View file

@ -1,4 +1,4 @@
#include "singletons/ImageUploader.hpp" #include "singletons/imageuploader/ImageUploader.hpp"
#include "common/Env.hpp" #include "common/Env.hpp"
#include "common/NetworkRequest.hpp" #include "common/NetworkRequest.hpp"
@ -6,6 +6,7 @@
#include "common/QLogging.hpp" #include "common/QLogging.hpp"
#include "messages/MessageBuilder.hpp" #include "messages/MessageBuilder.hpp"
#include "providers/twitch/TwitchMessageBuilder.hpp" #include "providers/twitch/TwitchMessageBuilder.hpp"
#include "singletons/imageuploader/UploadedImageModel.hpp"
#include "singletons/Paths.hpp" #include "singletons/Paths.hpp"
#include "singletons/Settings.hpp" #include "singletons/Settings.hpp"
#include "util/CombinePath.hpp" #include "util/CombinePath.hpp"
@ -16,9 +17,10 @@
#include <QHttpMultiPart> #include <QHttpMultiPart>
#include <QJsonArray> #include <QJsonArray>
#include <QJsonDocument> #include <QJsonDocument>
#include <qloggingcategory.h> #include <QLoggingCategory>
#include <QMimeDatabase> #include <QMimeDatabase>
#include <QMutex> #include <QMutex>
#include <QObject>
#include <QPointer> #include <QPointer>
#include <QSaveFile> #include <QSaveFile>
#include <rapidjson/document.h> #include <rapidjson/document.h>
@ -53,7 +55,7 @@ void ImageUploader::logToFile(const QString &originalFilePath,
const QString &imageLink, const QString &imageLink,
const QString &deletionLink, ChannelPtr channel) const QString &deletionLink, ChannelPtr channel)
{ {
this->imageLogSetting_->push_back(UploadedImage{ this->images_.append(UploadedImage{
.channelName = channel->getName(), .channelName = channel->getName(),
.deletionLink = deletionLink, .deletionLink = deletionLink,
.imageLink = imageLink, .imageLink = imageLink,
@ -91,9 +93,17 @@ QString getLinkFromResponse(NetworkResult response, QString pattern)
void ImageUploader::save() void ImageUploader::save()
{ {
this->uploadedImagesSetting_->setValue(this->images_.raw());
this->sm_->save(); this->sm_->save();
} }
UploadedImageModel *ImageUploader::createModel(QObject *parent)
{
auto *model = new UploadedImageModel(parent);
model->initialize(&this->images_);
return model;
}
void ImageUploader::initialize(Settings &settings, Paths &paths) void ImageUploader::initialize(Settings &settings, Paths &paths)
{ {
auto logPath = (getSettings()->logPath.getValue().isEmpty() auto logPath = (getSettings()->logPath.getValue().isEmpty()
@ -108,10 +118,19 @@ void ImageUploader::initialize(Settings &settings, Paths &paths)
this->sm_->setBackupEnabled(true); this->sm_->setBackupEnabled(true);
this->sm_->setBackupSlots(9); this->sm_->setBackupSlots(9);
this->imageLogSetting_ = std::make_unique< this->uploadedImagesSetting_ = std::make_unique<
pajlada::Settings::Setting<std::vector<UploadedImage>>>( pajlada::Settings::Setting<std::vector<UploadedImage>>>(
"/uploadedImages", this->sm_); "/uploadedImages", this->sm_);
for (const auto &item : this->uploadedImagesSetting_->getValue())
{
this->images_.append(item);
}
this->signals_.addConnection(
this->images_.delayedItemsChanged.connect([this]() {
this->uploadedImagesSetting_->setValue(this->images_.raw());
}));
// try to read old log // try to read old log
QFile oldLogFile(oldLogName); QFile oldLogFile(oldLogName);
bool isOldLogFileOkay = bool isOldLogFileOkay =
@ -139,7 +158,10 @@ void ImageUploader::initialize(Settings &settings, Paths &paths)
<< "Unable to parse ImageUploader.json: getSafe failed"; << "Unable to parse ImageUploader.json: getSafe failed";
return; return;
} }
this->imageLogSetting_->setValue(temporary); for (const auto &t : temporary)
{
this->images_.append(t);
}
oldLogFile.close(); oldLogFile.close();
oldLogFile.rename(combinePath(logPath, "ImageUploader.old.json")); oldLogFile.rename(combinePath(logPath, "ImageUploader.old.json"));
this->sm_->save(); this->sm_->save();

View file

@ -0,0 +1,98 @@
#pragma once
#include "util/RapidjsonHelpers.hpp"
#include <QString>
#include <Qt>
#include <cstdint>
namespace chatterino {
struct UploadedImage {
QString channelName;
QString deletionLink;
QString imageLink;
QString localPath;
int64_t timestamp{};
bool deleted = false;
};
} // namespace chatterino
namespace pajlada {
template <>
struct Serialize<chatterino::UploadedImage> {
static rapidjson::Value get(const chatterino::UploadedImage &value,
rapidjson::Document::AllocatorType &a)
{
rapidjson::Value ret(rapidjson::kObjectType);
chatterino::rj::set(ret, "channelName", value.channelName, a);
chatterino::rj::set(ret, "imageLink", value.imageLink, a);
chatterino::rj::set(ret, "timestamp", value.timestamp, a);
chatterino::rj::set(ret, "localPath", value.localPath, a);
chatterino::rj::set(ret, "deletionLink", value.deletionLink, a);
if (value.deleted)
{
chatterino::rj::set(ret, "deleted", value.deleted, a);
}
return ret;
}
};
template <>
struct Deserialize<chatterino::UploadedImage> {
static chatterino::UploadedImage get(const rapidjson::Value &value,
bool *error = nullptr)
{
chatterino::UploadedImage img;
if (!value.IsObject())
{
PAJLADA_REPORT_ERROR(error);
return img;
}
if (value["localPath"].IsNull())
{
img.localPath = QString();
}
else if (!chatterino::rj::getSafe(value, "localPath", img.localPath))
{
PAJLADA_REPORT_ERROR(error);
return img;
}
if (!chatterino::rj::getSafe(value, "imageLink", img.imageLink))
{
PAJLADA_REPORT_ERROR(error);
return img;
}
if (value["deletionLink"].IsNull())
{
img.deletionLink = QString();
}
else if (!chatterino::rj::getSafe(value, "deletionLink",
img.deletionLink))
{
PAJLADA_REPORT_ERROR(error);
return img;
}
if (!chatterino::rj::getSafe(value, "channelName", img.channelName))
{
PAJLADA_REPORT_ERROR(error);
return img;
}
if (!chatterino::rj::getSafe(value, "timestamp", img.timestamp))
{
PAJLADA_REPORT_ERROR(error);
return img;
}
if (!chatterino::rj::getSafe(value, "deleted", img.deleted))
{
img.deleted = false;
return img;
}
return img;
}
};
} // namespace pajlada

View file

@ -16,7 +16,7 @@
#include "providers/twitch/TwitchIrcServer.hpp" #include "providers/twitch/TwitchIrcServer.hpp"
#include "providers/twitch/TwitchMessageBuilder.hpp" #include "providers/twitch/TwitchMessageBuilder.hpp"
#include "singletons/Fonts.hpp" #include "singletons/Fonts.hpp"
#include "singletons/ImageUploader.hpp" #include "singletons/imageuploader/ImageUploader.hpp"
#include "singletons/Settings.hpp" #include "singletons/Settings.hpp"
#include "singletons/Theme.hpp" #include "singletons/Theme.hpp"
#include "singletons/WindowManager.hpp" #include "singletons/WindowManager.hpp"