2023-11-19 12:05:30 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "common/Singleton.hpp"
|
|
|
|
|
|
|
|
#include <QMimeData>
|
|
|
|
#include <QMutex>
|
|
|
|
#include <QString>
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <queue>
|
|
|
|
|
|
|
|
namespace chatterino {
|
|
|
|
|
|
|
|
class ResizingTextEdit;
|
|
|
|
class Channel;
|
|
|
|
class NetworkResult;
|
|
|
|
using ChannelPtr = std::shared_ptr<Channel>;
|
|
|
|
|
|
|
|
struct RawImageData {
|
|
|
|
QByteArray data;
|
|
|
|
QString format;
|
|
|
|
QString filePath;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ImageUploader final : public Singleton
|
|
|
|
{
|
|
|
|
public:
|
2024-04-12 23:48:08 +02:00
|
|
|
/**
|
|
|
|
* Tries to get the image(s) from the given QMimeData
|
|
|
|
*
|
|
|
|
* If no images were found, the second value in the pair will contain an error message
|
|
|
|
*/
|
|
|
|
std::pair<std::queue<RawImageData>, QString> getImages(
|
|
|
|
const QMimeData *source) const;
|
|
|
|
|
2023-11-19 12:05:30 +01:00
|
|
|
void save() override;
|
2024-04-12 23:48:08 +02:00
|
|
|
void upload(std::queue<RawImageData> images, ChannelPtr channel,
|
2023-11-19 12:05:30 +01:00
|
|
|
QPointer<ResizingTextEdit> outputTextEdit);
|
|
|
|
|
|
|
|
private:
|
|
|
|
void sendImageUploadRequest(RawImageData imageData, ChannelPtr channel,
|
|
|
|
QPointer<ResizingTextEdit> textEdit);
|
|
|
|
|
|
|
|
// This is called from the onSuccess handler of the NetworkRequest in sendImageUploadRequest
|
|
|
|
void handleSuccessfulUpload(const NetworkResult &result,
|
|
|
|
QString originalFilePath, ChannelPtr channel,
|
|
|
|
QPointer<ResizingTextEdit> textEdit);
|
|
|
|
void handleFailedUpload(const NetworkResult &result, ChannelPtr channel);
|
|
|
|
|
|
|
|
void logToFile(const QString &originalFilePath, const QString &imageLink,
|
|
|
|
const QString &deletionLink, ChannelPtr channel);
|
|
|
|
|
|
|
|
// These variables are only used from the main thread.
|
|
|
|
QMutex uploadMutex_;
|
|
|
|
std::queue<RawImageData> uploadQueue_;
|
|
|
|
};
|
|
|
|
} // namespace chatterino
|