-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmydayyyimageupload.cpp
168 lines (137 loc) · 5.57 KB
/
mydayyyimageupload.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <purpose/job.h>
#include <purpose/pluginbase.h>
#include <KJob>
#include <KIO/TransferJob>
#include <KIO/StoredTransferJob>
#include <QHttpMultiPart>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QMimeDatabase>
#include <QMimeType>
#include <QStandardPaths>
#include <QJsonDocument>
#include <QJsonArray>
#include <KPluginFactory>
const char* ENDPOINT_URL = "YOUR_ENDPOINT_HERE";
class MydayyyShareJob : public Purpose::Job
{
Q_OBJECT
public:
MydayyyShareJob(QObject* parent)
: Purpose::Job(parent)
{}
// All errors which can be thrown
enum {
InvalidFoo = UserDefinedError,
GenericError,
NotConnectedError,
UnknownMimeError
};
// Entrypoint for the share job. This will be called by purpose
void start() override {
const QJsonValue url = data().value(QStringLiteral("urls")).toArray().first();
QString u = url.toString();
// As this is an image plugin, we are receiving a data URL containing an
// image in base64. We basically fire a http get onto this url to get the
// raw image
KIO::StoredTransferJob* job = KIO::storedGet(QUrl(u));
connect(job, &KJob::finished, this, &MydayyyShareJob::fileFetched);
}
void fileFetched(KJob* j) {
// If any errors were thrown, return them.
if (j->error()) {
setError(j->error());
setErrorText(j->errorText());
emitResult();
return;
}
// Otherwise we check whether a mimetype was found. If we can't
// find one, the remote server probably can't either
KIO::StoredTransferJob* job = qobject_cast<KIO::StoredTransferJob*>(j);
QMimeDatabase db;
QMimeType ptr = db.mimeTypeForData(job->data());
QString mime = ptr.name();
if (mime.isEmpty())
{
return emitMimeNotFound();
}
QString suffix = ptr.preferredSuffix(); // Get the suffix corresponding to the mime
// Create a multipart http request containing the image
QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
QHttpPart imagePart;
imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"image\"; filename=\"image."+suffix+"\""));
imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant(mime));
imagePart.setBody(job->data());
multiPart->append(imagePart);
QUrl url(ENDPOINT_URL);
QNetworkRequest request(url);
QNetworkAccessManager *networkManager= new QNetworkAccessManager;
QNetworkReply *reply = networkManager->post(request, multiPart);
multiPart->setParent(reply); // Delete the multipart object once reply is deleted
connect(reply, SIGNAL(finished()), this, SLOT(uploadDone()));
}
public slots:
void emitGenericError() {
setError(GenericError);
setErrorText(QString::fromUtf8("An unknown error occured."));
emitResult();
}
void emitNotConnectedError() {
setError(NotConnectedError);
setErrorText(QString::fromUtf8("You are not connected to the designated teamspeak server"));
emitResult();
}
void emitMimeNotFound() {
setError(UnknownMimeError);
setErrorText(QString::fromUtf8("Could not determine mimetype"));
emitResult();
}
void emitError(QString error) {
setError(GenericError);
setErrorText(error);
emitResult();
}
void emitURL(QString url) {
setOutput({ { QStringLiteral("url"), url } });
emitResult();
}
void uploadDone() {
QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
reply->deleteLater();
// Check for any http errors
if(reply->error() != QNetworkReply::NoError) {
if(reply->error() == QNetworkReply::ContentAccessDenied || reply->error() == QNetworkReply::RemoteHostClosedError) {
return emitNotConnectedError();
}
return emitGenericError();
}
// We expect a json from our server, so try to parse the response
// and throw an error when it't not a valid json
QByteArray ba = reply->readAll();
QJsonParseError error;
QJsonDocument doc = QJsonDocument::fromJson(ba, &error);
if (error.error == QJsonParseError::NoError) {
QJsonObject root = doc.object();
if (root.value("success").toBool() == true) {
emitURL(root.value("message").toString());
} else {
emitError(root.value("message").toString());
}
} else {
return emitGenericError();
}
}
};
class Q_DECL_EXPORT MydayyyImagePlugin : public Purpose::PluginBase
{
Q_OBJECT
public:
MydayyyImagePlugin(QObject* p, const QVariantList& ) : Purpose::PluginBase(p) {}
Purpose::Job* createJob() const override
{
return new MydayyyShareJob(nullptr);
}
};
K_PLUGIN_FACTORY_WITH_JSON(MydayyyImageShare, "mydayyyimageupload.json", registerPlugin<MydayyyImagePlugin>();)
#include "mydayyyimageupload.moc"