Delete overloads for uploadImageToNuuls(). Use TypedBytes in the main

definition. Use QHttpMultiPart instead of manually creating the
request.
This commit is contained in:
Mm2PL 2019-09-24 18:28:28 +02:00
parent e0eb4e5a6d
commit 1a77df1674
No known key found for this signature in database
GPG key ID: 1C400DA5602DE62E
2 changed files with 22 additions and 35 deletions

View file

@ -4,53 +4,39 @@
#include "common/NetworkRequest.hpp" #include "common/NetworkRequest.hpp"
#include "providers/twitch/TwitchMessageBuilder.hpp" #include "providers/twitch/TwitchMessageBuilder.hpp"
#include <boost/algorithm/string/replace.hpp> #include <QHttpMultiPart>
namespace chatterino { namespace chatterino {
bool isUploading = false; bool isUploading = false;
std::queue<TypedBytes> uploadQueue; std::queue<TypedBytes> uploadQueue;
void uploadImageToNuuls(QByteArray imageData, ChannelPtr channel,
ResizingTextEdit &textEdit)
{
uploadImageToNuuls(imageData, channel, textEdit, "png");
}
void uploadImageToNuuls(TypedBytes imageData, ChannelPtr channel, void uploadImageToNuuls(TypedBytes imageData, ChannelPtr channel,
ResizingTextEdit &textEdit) ResizingTextEdit &textEdit)
{ {
uploadImageToNuuls(imageData.data, channel, textEdit, imageData.type);
}
void uploadImageToNuuls(QByteArray imageData, ChannelPtr channel,
ResizingTextEdit &textEdit, std::string format)
{
QByteArray dataToSend;
const char *boundary = "thisistheboudaryasd"; const char *boundary = "thisistheboudaryasd";
static QUrl url(Env::get().imagePasteSiteUrl); static QUrl url(Env::get().imagePasteSiteUrl);
dataToSend.insert(0, "--"); QHttpMultiPart *payload = new QHttpMultiPart(QHttpMultiPart::FormDataType);
dataToSend.append(boundary); QHttpPart part = QHttpPart();
std::string temp = "\r\n" part.setBody(imageData.data);
"Content-Disposition: form-data; name=\"attachment\"; " part.setHeader(QNetworkRequest::ContentTypeHeader,
"filename=\"control_v.%\"\r\n" QString("image/%1").arg(imageData.type));
"Content-Type: image/%\r\n" part.setHeader(QNetworkRequest::ContentLengthHeader,
"\r\n"; QVariant(imageData.data.length()));
boost::replace_all(temp, "%", format); part.setHeader(
QNetworkRequest::ContentDispositionHeader,
dataToSend.append(temp.c_str()); QString("form-data; name=\"attachment\"; filename=\"control_v.%1\""));
dataToSend.append(imageData); payload->setBoundary(boundary);
dataToSend.append("\r\n--"); payload->append(part);
dataToSend.append(boundary);
dataToSend.append("\r\n");
NetworkRequest(url, NetworkRequestType::Post) NetworkRequest(url, NetworkRequestType::Post)
.header("Content-Type", (std::string("multipart/form-data; boundary=") + .header("Content-Type", (std::string("multipart/form-data; boundary=") +
std::string(boundary)) std::string(boundary))
.c_str()) .c_str())
.payload(dataToSend) .multiPart(payload)
.onSuccess([&textEdit, channel](NetworkResult result) -> Outcome { .onSuccess([&textEdit, channel](NetworkResult result) -> Outcome {
textEdit.insertPlainText(result.getData() + QString(" ")); textEdit.insertPlainText(result.getData() + QString(" "));
// this->input_->ui_.textEdit->insertPlainText(result.getData());
if (uploadQueue.size()) if (uploadQueue.size())
{ {
channel->addMessage(makeSystemMessage( channel->addMessage(makeSystemMessage(
@ -104,8 +90,8 @@ void pasteFromClipboard(const QMimeData *source, ChannelPtr channel,
ResizingTextEdit &outputTextEdit) ResizingTextEdit &outputTextEdit)
{ {
/* /*
static QUrl url("http://localhost:7494/upload?password=xd"); http://localhost:7494/upload?password=xd
// default port and password for nuuls' filehost. default port and password for nuuls' filehost.
*/ */
if (isUploading) if (isUploading)
{ {
@ -120,7 +106,8 @@ static QUrl url("http://localhost:7494/upload?password=xd");
if (source->hasFormat("image/png")) if (source->hasFormat("image/png"))
{ {
uploadImageToNuuls(source->data("image/png"), channel, outputTextEdit); uploadImageToNuuls({source->data("image/png"), "png"}, channel,
outputTextEdit);
} }
else if (source->hasFormat("text/uri-list")) else if (source->hasFormat("text/uri-list"))
{ {
@ -199,7 +186,7 @@ static QUrl url("http://localhost:7494/upload?password=xd");
buf.open(QIODevice::WriteOnly); buf.open(QIODevice::WriteOnly);
image.save(&buf, "png"); image.save(&buf, "png");
uploadImageToNuuls(imageData, channel, outputTextEdit); uploadImageToNuuls({imageData, "png"}, channel, outputTextEdit);
} }
} }
} // namespace chatterino } // namespace chatterino

View file

@ -7,7 +7,7 @@
namespace chatterino { namespace chatterino {
struct TypedBytes { struct TypedBytes {
QByteArray data; QByteArray data;
std::string type; QString type;
}; };
void uploadImageToNuuls(QByteArray imageData, ChannelPtr channel, void uploadImageToNuuls(QByteArray imageData, ChannelPtr channel,
ResizingTextEdit &textEdit, std::string format); ResizingTextEdit &textEdit, std::string format);