2019-09-24 16:08:12 +02:00
|
|
|
#include "NuulsUploader.hpp"
|
|
|
|
|
|
|
|
#include "common/Env.hpp"
|
|
|
|
#include "common/NetworkRequest.hpp"
|
|
|
|
#include "providers/twitch/TwitchMessageBuilder.hpp"
|
2020-06-13 14:53:09 +02:00
|
|
|
#include "singletons/Paths.hpp"
|
|
|
|
#include "singletons/Settings.hpp"
|
2019-09-24 16:08:12 +02:00
|
|
|
|
2020-02-08 16:26:32 +01:00
|
|
|
#include <QBuffer>
|
2019-09-24 18:28:28 +02:00
|
|
|
#include <QHttpMultiPart>
|
2020-02-08 16:26:32 +01:00
|
|
|
#include <QMimeDatabase>
|
|
|
|
#include <QMutex>
|
2019-09-24 16:08:12 +02:00
|
|
|
|
2019-10-11 15:41:33 +02:00
|
|
|
#define UPLOAD_DELAY 2000
|
|
|
|
// Delay between uploads in milliseconds
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
boost::optional<QByteArray> convertToPng(QImage image)
|
|
|
|
{
|
|
|
|
QByteArray imageData;
|
|
|
|
QBuffer buf(&imageData);
|
|
|
|
buf.open(QIODevice::WriteOnly);
|
|
|
|
bool success = image.save(&buf, "png");
|
|
|
|
if (success)
|
|
|
|
{
|
|
|
|
return boost::optional<QByteArray>(imageData);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return boost::optional<QByteArray>(boost::none);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2019-09-24 16:08:12 +02:00
|
|
|
namespace chatterino {
|
2019-10-19 11:41:23 +02:00
|
|
|
// These variables are only used from the main thread.
|
2020-03-29 13:47:52 +02:00
|
|
|
static auto uploadMutex = QMutex();
|
|
|
|
static std::queue<RawImageData> uploadQueue;
|
2019-09-24 16:08:12 +02:00
|
|
|
|
2020-06-13 14:53:09 +02:00
|
|
|
//logging information on successful uploads to a csv file
|
|
|
|
void logToCsv(const QString originalFilePath, const QString link,
|
|
|
|
ChannelPtr channel)
|
|
|
|
{
|
|
|
|
const QString csvFileName = (getSettings()->logPath.getValue().isEmpty()
|
|
|
|
? getPaths()->messageLogDirectory
|
|
|
|
: getSettings()->logPath) +
|
|
|
|
"/ImageUploader.csv";
|
|
|
|
QFile csvFile(csvFileName);
|
|
|
|
bool csvExisted = csvFile.exists();
|
|
|
|
bool isCsvOkay = csvFile.open(QIODevice::Append | QIODevice::Text);
|
|
|
|
if (!isCsvOkay)
|
|
|
|
{
|
|
|
|
channel->addMessage(makeSystemMessage(
|
|
|
|
QString("Failed to open csv file with links at ") + csvFileName));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QTextStream out(&csvFile);
|
|
|
|
qDebug() << csvExisted;
|
|
|
|
if (!csvExisted)
|
|
|
|
{
|
|
|
|
out << "localPath,imageLink,timestamp,channelName\n";
|
|
|
|
}
|
|
|
|
out << originalFilePath + QString(",") << link + QString(",")
|
|
|
|
<< QDateTime::currentSecsSinceEpoch()
|
|
|
|
<< QString(",%1\n").arg(channel->getName());
|
|
|
|
// image path (can be empty)
|
|
|
|
// image link
|
|
|
|
// timestamp
|
|
|
|
// channel name
|
|
|
|
csvFile.close();
|
|
|
|
}
|
|
|
|
|
2019-10-22 17:21:46 +02:00
|
|
|
void uploadImageToNuuls(RawImageData imageData, ChannelPtr channel,
|
2019-09-24 16:08:12 +02:00
|
|
|
ResizingTextEdit &textEdit)
|
|
|
|
{
|
2020-03-29 13:47:52 +02:00
|
|
|
const static char *const boundary = "thisistheboudaryasd";
|
2020-01-03 14:43:05 +01:00
|
|
|
const static QString contentType =
|
2019-10-11 15:41:33 +02:00
|
|
|
QString("multipart/form-data; boundary=%1").arg(boundary);
|
2019-09-25 12:51:17 +02:00
|
|
|
static QUrl url(Env::get().imageUploaderUrl);
|
2020-05-30 12:30:30 +02:00
|
|
|
static QString formBody(Env::get().imageUploaderFormBody);
|
2020-06-13 14:53:09 +02:00
|
|
|
QString originalFilePath = imageData.filePath;
|
2019-09-24 16:08:12 +02:00
|
|
|
|
2019-09-24 18:28:28 +02:00
|
|
|
QHttpMultiPart *payload = new QHttpMultiPart(QHttpMultiPart::FormDataType);
|
|
|
|
QHttpPart part = QHttpPart();
|
|
|
|
part.setBody(imageData.data);
|
|
|
|
part.setHeader(QNetworkRequest::ContentTypeHeader,
|
2019-10-22 17:21:46 +02:00
|
|
|
QString("image/%1").arg(imageData.format));
|
2019-09-24 18:28:28 +02:00
|
|
|
part.setHeader(QNetworkRequest::ContentLengthHeader,
|
|
|
|
QVariant(imageData.data.length()));
|
2020-05-30 12:30:30 +02:00
|
|
|
part.setHeader(QNetworkRequest::ContentDispositionHeader,
|
|
|
|
QString("form-data; name=\"%1\"; filename=\"control_v.%2\"")
|
|
|
|
.arg(formBody)
|
|
|
|
.arg(imageData.format));
|
2019-09-24 18:28:28 +02:00
|
|
|
payload->setBoundary(boundary);
|
|
|
|
payload->append(part);
|
2019-09-24 16:08:12 +02:00
|
|
|
NetworkRequest(url, NetworkRequestType::Post)
|
2019-10-11 15:41:33 +02:00
|
|
|
.header("Content-Type", contentType)
|
2019-09-24 16:08:12 +02:00
|
|
|
|
2019-09-24 18:28:28 +02:00
|
|
|
.multiPart(payload)
|
2020-06-13 14:53:09 +02:00
|
|
|
.onSuccess([&textEdit, channel,
|
|
|
|
originalFilePath](NetworkResult result) -> Outcome {
|
2019-09-24 16:08:12 +02:00
|
|
|
textEdit.insertPlainText(result.getData() + QString(" "));
|
2019-09-25 22:21:26 +02:00
|
|
|
if (uploadQueue.empty())
|
|
|
|
{
|
|
|
|
channel->addMessage(makeSystemMessage(
|
2020-06-13 14:53:09 +02:00
|
|
|
QString("Your image has been uploaded to ") +
|
|
|
|
result.getData()));
|
2020-02-08 16:26:32 +01:00
|
|
|
uploadMutex.unlock();
|
2019-09-25 22:21:26 +02:00
|
|
|
}
|
|
|
|
else
|
2019-09-24 16:08:12 +02:00
|
|
|
{
|
|
|
|
channel->addMessage(makeSystemMessage(
|
2020-06-13 14:53:09 +02:00
|
|
|
QString(
|
|
|
|
"Your image has been uploaded to %1 . %2 left. Please "
|
|
|
|
"wait until all of them are uploaded. About %3 "
|
|
|
|
"seconds left.")
|
|
|
|
.arg(result.getData() + QString(""))
|
2020-01-03 15:31:39 +01:00
|
|
|
.arg(uploadQueue.size())
|
|
|
|
.arg(uploadQueue.size() * (UPLOAD_DELAY / 1000 + 1))));
|
2020-01-03 14:43:05 +01:00
|
|
|
// 2 seconds for the timer that's there not to spam the remote server
|
2019-09-24 16:08:12 +02:00
|
|
|
// and 1 second of actual uploading.
|
2019-10-11 15:41:33 +02:00
|
|
|
|
|
|
|
QTimer::singleShot(UPLOAD_DELAY, [channel, &textEdit]() {
|
2019-09-24 16:08:12 +02:00
|
|
|
uploadImageToNuuls(uploadQueue.front(), channel, textEdit);
|
|
|
|
uploadQueue.pop();
|
2019-09-25 22:21:26 +02:00
|
|
|
});
|
|
|
|
}
|
2020-06-13 14:53:09 +02:00
|
|
|
|
|
|
|
logToCsv(originalFilePath, result.getData(), channel);
|
|
|
|
|
2019-09-24 16:08:12 +02:00
|
|
|
return Success;
|
|
|
|
})
|
|
|
|
.onError([channel](NetworkResult result) -> bool {
|
|
|
|
channel->addMessage(makeSystemMessage(
|
|
|
|
QString("An error happened while uploading your image: %1")
|
|
|
|
.arg(result.status())));
|
2020-02-08 16:26:32 +01:00
|
|
|
uploadMutex.unlock();
|
2019-09-24 16:08:12 +02:00
|
|
|
return true;
|
|
|
|
})
|
|
|
|
.execute();
|
|
|
|
}
|
|
|
|
|
2019-09-25 22:21:26 +02:00
|
|
|
void upload(const QMimeData *source, ChannelPtr channel,
|
|
|
|
ResizingTextEdit &outputTextEdit)
|
2019-09-24 16:08:12 +02:00
|
|
|
{
|
2020-02-08 16:26:32 +01:00
|
|
|
if (!uploadMutex.tryLock())
|
2019-09-24 16:08:12 +02:00
|
|
|
{
|
|
|
|
channel->addMessage(makeSystemMessage(
|
2019-09-25 22:21:26 +02:00
|
|
|
QString("Please wait until the upload finishes.")));
|
2019-09-24 16:08:12 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
channel->addMessage(makeSystemMessage(QString("Started upload...")));
|
2020-06-13 14:53:09 +02:00
|
|
|
if (source->hasUrls())
|
2019-09-25 22:21:26 +02:00
|
|
|
{
|
2020-02-08 16:26:32 +01:00
|
|
|
auto mimeDb = QMimeDatabase();
|
2019-10-11 15:41:33 +02:00
|
|
|
// This path gets chosen when files are copied from a file manager, like explorer.exe, caja.
|
2019-10-19 11:41:23 +02:00
|
|
|
// Each entry in source->urls() is a QUrl pointing to a file that was copied.
|
2019-09-26 14:24:41 +02:00
|
|
|
for (const QUrl &path : source->urls())
|
2019-09-24 16:08:12 +02:00
|
|
|
{
|
2019-10-22 16:04:36 +02:00
|
|
|
QString localPath = path.toLocalFile();
|
2020-02-08 16:26:32 +01:00
|
|
|
QMimeType mime = mimeDb.mimeTypeForUrl(path);
|
|
|
|
qDebug() << mime.name();
|
|
|
|
if (mime.name().startsWith("image") && !mime.inherits("image/gif"))
|
2019-09-24 16:08:12 +02:00
|
|
|
{
|
2019-09-25 22:21:26 +02:00
|
|
|
channel->addMessage(makeSystemMessage(
|
2019-10-22 16:04:36 +02:00
|
|
|
QString("Uploading image: %1").arg(localPath)));
|
|
|
|
QImage img = QImage(localPath);
|
2019-09-25 22:21:26 +02:00
|
|
|
if (img.isNull())
|
2019-09-24 16:08:12 +02:00
|
|
|
{
|
2019-09-25 22:21:26 +02:00
|
|
|
channel->addMessage(
|
|
|
|
makeSystemMessage(QString("Couldn't load image :(")));
|
2020-02-08 16:26:32 +01:00
|
|
|
uploadMutex.unlock();
|
2019-09-25 22:21:26 +02:00
|
|
|
return;
|
2019-09-24 16:08:12 +02:00
|
|
|
}
|
2019-09-25 22:21:26 +02:00
|
|
|
|
2019-10-11 15:41:33 +02:00
|
|
|
boost::optional<QByteArray> imageData = convertToPng(img);
|
|
|
|
if (imageData)
|
|
|
|
{
|
2020-06-13 14:53:09 +02:00
|
|
|
RawImageData data = {imageData.get(), "png", localPath};
|
2019-10-11 15:41:33 +02:00
|
|
|
uploadQueue.push(data);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
channel->addMessage(makeSystemMessage(
|
2020-06-13 14:53:09 +02:00
|
|
|
QString("Cannot upload file: %1. Couldn't convert "
|
2019-10-11 15:41:33 +02:00
|
|
|
"image to png.")
|
2019-10-22 16:04:36 +02:00
|
|
|
.arg(localPath)));
|
2020-02-10 16:55:59 +01:00
|
|
|
uploadMutex.unlock();
|
|
|
|
return;
|
2019-10-11 15:41:33 +02:00
|
|
|
}
|
2019-09-25 22:21:26 +02:00
|
|
|
}
|
2020-02-08 16:26:32 +01:00
|
|
|
else if (mime.inherits("image/gif"))
|
2019-09-25 22:21:26 +02:00
|
|
|
{
|
|
|
|
channel->addMessage(makeSystemMessage(
|
2019-10-22 16:04:36 +02:00
|
|
|
QString("Uploading GIF: %1").arg(localPath)));
|
|
|
|
QFile file(localPath);
|
2019-09-25 22:21:26 +02:00
|
|
|
bool isOkay = file.open(QIODevice::ReadOnly);
|
|
|
|
if (!isOkay)
|
2019-09-24 16:08:12 +02:00
|
|
|
{
|
2019-09-25 22:21:26 +02:00
|
|
|
channel->addMessage(
|
|
|
|
makeSystemMessage(QString("Failed to open file. :(")));
|
2020-02-08 16:26:32 +01:00
|
|
|
uploadMutex.unlock();
|
2019-09-25 22:21:26 +02:00
|
|
|
return;
|
2019-09-24 16:08:12 +02:00
|
|
|
}
|
2020-06-13 14:53:09 +02:00
|
|
|
RawImageData data = {file.readAll(), "gif", localPath};
|
2019-09-25 22:21:26 +02:00
|
|
|
uploadQueue.push(data);
|
|
|
|
file.close();
|
|
|
|
// file.readAll() => might be a bit big but it /should/ work
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
channel->addMessage(makeSystemMessage(
|
2020-06-13 14:53:09 +02:00
|
|
|
QString("Cannot upload file: %1. Not an image.")
|
2019-10-22 16:04:36 +02:00
|
|
|
.arg(localPath)));
|
2020-02-08 16:26:32 +01:00
|
|
|
uploadMutex.unlock();
|
|
|
|
return;
|
2019-09-24 16:08:12 +02:00
|
|
|
}
|
|
|
|
}
|
2019-10-11 15:41:33 +02:00
|
|
|
if (!uploadQueue.empty())
|
2019-09-24 16:08:12 +02:00
|
|
|
{
|
|
|
|
uploadImageToNuuls(uploadQueue.front(), channel, outputTextEdit);
|
|
|
|
uploadQueue.pop();
|
|
|
|
}
|
|
|
|
}
|
2020-06-13 14:53:09 +02:00
|
|
|
else if (source->hasFormat("image/png"))
|
|
|
|
{
|
|
|
|
// the path to file is not present every time, thus the filePath is empty
|
|
|
|
uploadImageToNuuls({source->data("image/png"), "png", ""}, channel,
|
|
|
|
outputTextEdit);
|
|
|
|
}
|
|
|
|
else if (source->hasFormat("image/jpeg"))
|
|
|
|
{
|
|
|
|
uploadImageToNuuls({source->data("image/jpeg"), "jpeg", ""}, channel,
|
|
|
|
outputTextEdit);
|
|
|
|
}
|
|
|
|
else if (source->hasFormat("image/gif"))
|
|
|
|
{
|
|
|
|
uploadImageToNuuls({source->data("image/gif"), "gif", ""}, channel,
|
|
|
|
outputTextEdit);
|
|
|
|
}
|
|
|
|
|
2019-09-24 16:08:12 +02:00
|
|
|
else
|
|
|
|
{ // not PNG, try loading it into QImage and save it to a PNG.
|
|
|
|
QImage image = qvariant_cast<QImage>(source->imageData());
|
2019-10-11 15:41:33 +02:00
|
|
|
boost::optional<QByteArray> imageData = convertToPng(image);
|
|
|
|
if (imageData)
|
2019-09-25 22:21:26 +02:00
|
|
|
{
|
2020-06-13 14:53:09 +02:00
|
|
|
uploadImageToNuuls({imageData.get(), "png", ""}, channel,
|
2019-10-11 15:41:33 +02:00
|
|
|
outputTextEdit);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
channel->addMessage(makeSystemMessage(
|
|
|
|
QString("Cannot upload file, failed to convert to png.")));
|
2020-02-08 16:26:32 +01:00
|
|
|
uploadMutex.unlock();
|
2019-09-25 22:21:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-13 14:53:09 +02:00
|
|
|
|
2019-10-11 15:41:33 +02:00
|
|
|
} // namespace chatterino
|