Changeset View
Changeset View
Standalone View
Standalone View
ipfs/ipfsglobaluploadapi.cpp
- This file was added.
1 | /* ============================================================ | ||||
---|---|---|---|---|---|
2 | * | ||||
3 | * This file is a part of KDE project | ||||
4 | * | ||||
5 | * | ||||
6 | * Date : 2016-06-06 | ||||
7 | * Description : a kipi plugin to export images to the IPFS web service | ||||
8 | * | ||||
9 | * Copyright (C) 2018 by Amar Lakshya <amar dot lakshya at xaviers dot edu dot in> | ||||
10 | * | ||||
11 | * This program is free software; you can redistribute it | ||||
12 | * and/or modify it under the terms of the GNU General | ||||
13 | * Public License as published by the Free Software Foundation; | ||||
14 | * either version 2, or (at your option) any later version. | ||||
15 | * | ||||
16 | * This program is distributed in the hope that it will be useful, | ||||
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
19 | * GNU General Public License for more details. | ||||
20 | * | ||||
21 | * ============================================================ */ | ||||
22 | | ||||
23 | #include "ipfsglobaluploadapi.h" | ||||
24 | | ||||
25 | // Qt includes | ||||
26 | | ||||
27 | #include <QFileInfo> | ||||
28 | #include <QHttpMultiPart> | ||||
29 | #include <QJsonDocument> | ||||
30 | #include <QJsonObject> | ||||
31 | #include <QTimerEvent> | ||||
32 | #include <QUrlQuery> | ||||
33 | #include <QStandardPaths> | ||||
34 | | ||||
35 | // KDE includes | ||||
36 | | ||||
37 | #include <klocalizedstring.h> | ||||
38 | | ||||
39 | // Local includes | ||||
40 | | ||||
41 | #include "kipiplugins_debug.h" | ||||
42 | #include "o0settingsstore.h" | ||||
43 | #include "o0globals.h" | ||||
44 | | ||||
45 | static const QString ipfs_upload_url = QLatin1String("https://api.globalupload.io/transport/add"); | ||||
46 | | ||||
47 | IPFSGLOBALUPLOADAPI::IPFSGLOBALUPLOADAPI(QObject* parent) | ||||
48 | : QObject(parent) | ||||
49 | { | ||||
50 | } | ||||
51 | | ||||
52 | IPFSGLOBALUPLOADAPI::~IPFSGLOBALUPLOADAPI() | ||||
53 | { | ||||
54 | /* Disconnect all signals as cancelAllWork may emit */ | ||||
55 | disconnect(this, 0, 0, 0); | ||||
56 | cancelAllWork(); | ||||
57 | } | ||||
58 | | ||||
59 | unsigned int IPFSGLOBALUPLOADAPI::workQueueLength() | ||||
60 | { | ||||
61 | return m_work_queue.size(); | ||||
62 | } | ||||
63 | | ||||
64 | void IPFSGLOBALUPLOADAPI::queueWork(const IPFSGLOBALUPLOADAPIAction& action) | ||||
65 | { | ||||
66 | m_work_queue.push(action); | ||||
67 | startWorkTimer(); | ||||
68 | } | ||||
69 | | ||||
70 | void IPFSGLOBALUPLOADAPI::cancelAllWork() | ||||
71 | { | ||||
72 | stopWorkTimer(); | ||||
73 | | ||||
74 | if (m_reply) | ||||
75 | m_reply->abort(); | ||||
76 | | ||||
77 | /* Should error be emitted for those actions? */ | ||||
78 | while (!m_work_queue.empty()) | ||||
79 | m_work_queue.pop(); | ||||
80 | } | ||||
81 | | ||||
82 | void IPFSGLOBALUPLOADAPI::uploadProgress(qint64 sent, qint64 total) | ||||
83 | { | ||||
84 | if (total > 0) /* Don't divide by 0 */ | ||||
85 | emit progress((sent * 100) / total, m_work_queue.front()); | ||||
86 | } | ||||
87 | | ||||
88 | void IPFSGLOBALUPLOADAPI::replyFinished() | ||||
89 | { | ||||
90 | auto* reply = m_reply; | ||||
91 | reply->deleteLater(); | ||||
92 | m_reply = nullptr; | ||||
93 | | ||||
94 | if (this->m_image) | ||||
95 | { | ||||
96 | delete this->m_image; | ||||
97 | this->m_image = nullptr; | ||||
98 | } | ||||
99 | | ||||
100 | if (m_work_queue.empty()) | ||||
101 | { | ||||
102 | qCDebug(KIPIPLUGINS_LOG) << "Received result without request"; | ||||
103 | return; | ||||
104 | } | ||||
105 | | ||||
106 | /* toInt() returns 0 if conversion fails. That fits nicely already. */ | ||||
107 | int code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); | ||||
108 | auto response = QJsonDocument::fromJson(reply->readAll()); | ||||
109 | if (code == 200 && !response.isEmpty()) | ||||
110 | { | ||||
111 | /* Success! */ | ||||
112 | IPFSGLOBALUPLOADAPIResult result; | ||||
113 | result.action = &m_work_queue.front(); | ||||
114 | switch (result.action->type) | ||||
115 | { | ||||
116 | case IPFSGLOBALUPLOADAPIActionType::IMG_UPLOAD: | ||||
117 | result.image.name = response.object()[QLatin1String("Name")].toString(); | ||||
118 | result.image.size = response.object()[QLatin1String("Size")].toInt(); | ||||
119 | result.image.url = QLatin1String("https://ipfs.io/ipfs/") + response.object()[QLatin1String("Hash")].toString(); | ||||
120 | break; | ||||
121 | default: | ||||
122 | qCWarning(KIPIPLUGINS_LOG) << "Unexpected action"; | ||||
123 | qCDebug(KIPIPLUGINS_LOG) << response.toJson(); | ||||
124 | break; | ||||
125 | } | ||||
126 | | ||||
127 | emit success(result); | ||||
128 | } | ||||
129 | else | ||||
130 | { | ||||
131 | if (code == 403) | ||||
132 | { | ||||
133 | /* HTTP 403 Forbidden -> Invalid token? | ||||
134 | * That needs to be handled internally, so don't emit progress | ||||
135 | * and keep the action in the queue for later retries. */ | ||||
136 | return; | ||||
137 | } | ||||
138 | else | ||||
139 | { | ||||
140 | /* Failed. */ | ||||
141 | auto msg = response.object()[QLatin1String("data")] | ||||
142 | .toObject()[QLatin1String("error")] | ||||
143 | .toString(QLatin1String("Could not read response.")); | ||||
144 | | ||||
145 | emit error(msg, m_work_queue.front()); | ||||
146 | } | ||||
147 | } | ||||
148 | | ||||
149 | /* Next work item. */ | ||||
150 | m_work_queue.pop(); | ||||
151 | startWorkTimer(); | ||||
152 | } | ||||
153 | | ||||
154 | void IPFSGLOBALUPLOADAPI::timerEvent(QTimerEvent* event) | ||||
155 | { | ||||
156 | if (event->timerId() != m_work_timer) | ||||
157 | return QObject::timerEvent(event); | ||||
158 | | ||||
159 | event->accept(); | ||||
160 | | ||||
161 | /* One-shot only. */ | ||||
162 | QObject::killTimer(event->timerId()); | ||||
163 | m_work_timer = 0; | ||||
164 | | ||||
165 | doWork(); | ||||
166 | } | ||||
167 | | ||||
168 | void IPFSGLOBALUPLOADAPI::startWorkTimer() | ||||
169 | { | ||||
170 | if (!m_work_queue.empty() && m_work_timer == 0) | ||||
171 | { | ||||
172 | m_work_timer = QObject::startTimer(0); | ||||
173 | emit busy(true); | ||||
174 | } | ||||
175 | else | ||||
176 | emit busy(false); | ||||
177 | } | ||||
178 | | ||||
179 | void IPFSGLOBALUPLOADAPI::stopWorkTimer() | ||||
180 | { | ||||
181 | if (m_work_timer != 0) | ||||
182 | { | ||||
183 | QObject::killTimer(m_work_timer); | ||||
184 | m_work_timer = 0; | ||||
185 | } | ||||
186 | } | ||||
187 | | ||||
188 | void IPFSGLOBALUPLOADAPI::doWork() | ||||
189 | { | ||||
190 | if (m_work_queue.empty() || m_reply != nullptr) | ||||
191 | return; | ||||
192 | | ||||
193 | auto &work = m_work_queue.front(); | ||||
194 | | ||||
195 | switch(work.type) | ||||
196 | { | ||||
197 | case IPFSGLOBALUPLOADAPIActionType::IMG_UPLOAD: | ||||
198 | { | ||||
199 | this->m_image = new QFile(work.upload.imgpath); | ||||
200 | | ||||
201 | if (!m_image->open(QIODevice::ReadOnly)) | ||||
202 | { | ||||
203 | delete this->m_image; | ||||
204 | this->m_image = nullptr; | ||||
205 | | ||||
206 | /* Failed. */ | ||||
207 | emit error(i18n("Could not open file"), m_work_queue.front()); | ||||
208 | | ||||
209 | m_work_queue.pop(); | ||||
210 | return doWork(); | ||||
211 | } | ||||
212 | | ||||
213 | /* Set ownership to m_image to delete that as well. */ | ||||
214 | auto* multipart = new QHttpMultiPart(QHttpMultiPart::FormDataType, m_image); | ||||
215 | QHttpPart title; | ||||
216 | title.setHeader(QNetworkRequest::ContentDispositionHeader, | ||||
217 | QLatin1String("form-data; name=\"keyphrase\"")); | ||||
218 | multipart->append(title); | ||||
219 | | ||||
220 | QHttpPart description; | ||||
221 | description.setHeader(QNetworkRequest::ContentDispositionHeader, | ||||
222 | QLatin1String("form-data; name=\"user\"")); | ||||
223 | multipart->append(description); | ||||
224 | | ||||
225 | QHttpPart image; | ||||
226 | image.setHeader(QNetworkRequest::ContentDispositionHeader, | ||||
227 | QVariant(QString::fromLatin1("form-data; name=\"file\"; filename=\"%1\"") | ||||
228 | .arg(QLatin1String(QFileInfo(work.upload.imgpath).fileName().toUtf8().toPercentEncoding())))); | ||||
229 | image.setHeader(QNetworkRequest::ContentTypeHeader, QLatin1String("image/jpeg")); | ||||
230 | image.setBodyDevice(this->m_image); | ||||
231 | multipart->append(image); | ||||
232 | QNetworkRequest request(QUrl(QLatin1String("https://api.globalupload.io/transport/add"))); | ||||
233 | this->m_reply = this->m_net.post(request, multipart); | ||||
234 | | ||||
235 | break; | ||||
236 | } | ||||
237 | } | ||||
238 | | ||||
239 | if (this->m_reply) | ||||
240 | { | ||||
241 | connect(m_reply, &QNetworkReply::uploadProgress, this, &IPFSGLOBALUPLOADAPI::uploadProgress); | ||||
242 | connect(m_reply, &QNetworkReply::finished, this, &IPFSGLOBALUPLOADAPI::replyFinished); | ||||
243 | } | ||||
244 | } |