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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
|
// SPDX-FileCopyrightText: 2019 Linus Jahn <lnj@kaidan.im>
//
// SPDX-License-Identifier: LGPL-2.1-or-later
#include "QXmppUploadRequestManager.h"
#include "QXmppClient.h"
#include "QXmppConstants_p.h"
#include "QXmppDataForm.h"
#include "QXmppDiscoveryIq.h"
#include "QXmppDiscoveryManager.h"
#include "QXmppFutureUtils_p.h"
#include "QXmppHttpUploadIq.h"
#include <QDomElement>
#include <QFileInfo>
#include <QMimeDatabase>
using namespace QXmpp::Private;
class QXmppUploadServicePrivate : public QSharedData
{
public:
QString jid;
qint64 sizeLimit = -1;
};
QXmppUploadService::QXmppUploadService()
: d(new QXmppUploadServicePrivate)
{
}
/// Copy constructor
QXmppUploadService::QXmppUploadService(const QXmppUploadService &) = default;
QXmppUploadService::~QXmppUploadService() = default;
/// Equal operator
QXmppUploadService &QXmppUploadService::operator=(const QXmppUploadService &) = default;
/// Returns the JID of the HTTP File Upload service.
QString QXmppUploadService::jid() const
{
return d->jid;
}
/// Sets the JID of the HTTP File Upload service.
void QXmppUploadService::setJid(const QString &jid)
{
d->jid = jid;
}
/// Returns the size limit of files that can be uploaded to this upload
/// service.
///
/// A size limit of -1 means that there is no file size limit or it could not
/// be determined.
qint64 QXmppUploadService::sizeLimit() const
{
return d->sizeLimit;
}
/// Sets the size limit of files that can be uploaded to this upload service.
void QXmppUploadService::setSizeLimit(qint64 sizeLimit)
{
d->sizeLimit = sizeLimit;
}
class QXmppUploadRequestManagerPrivate : public QSharedData
{
public:
QVector<QXmppUploadService> uploadServices;
};
///
/// \typedef QXmppUploadRequestManager::SlotResult
///
/// Contains the requested upload slot from the service or a QXmppStanza::Error
/// in case the request failed.
///
/// \since QXmpp 1.5
///
QXmppUploadRequestManager::QXmppUploadRequestManager()
: d(std::make_unique<QXmppUploadRequestManagerPrivate>())
{
}
QXmppUploadRequestManager::~QXmppUploadRequestManager() = default;
/// Requests an upload slot from the server.
///
/// \param file The info of the file to be uploaded.
/// \param uploadService The HTTP File Upload service that is used to request
/// the upload slot. If this is empty, the first
/// discovered one is used.
/// \return The id of the sent IQ. If sendPacket() isn't successful or no
/// upload service has been discovered yet, an empty string will be
/// returned.
QString QXmppUploadRequestManager::requestUploadSlot(const QFileInfo &file,
const QString &uploadService)
{
return requestUploadSlot(file, file.fileName(), uploadService);
}
/// Requests an upload slot from the server.
///
/// \param file The info of the file to be uploaded.
/// \param customFileName This name is used instead of the file's actual name
/// for requesting the upload slot.
/// \param uploadService The HTTP File Upload service that is used to request
/// the upload slot. If this is empty, the first
/// discovered one is used.
/// \return The id of the sent IQ. If sendPacket() isn't successful or no
/// upload service has been discovered yet, an empty string will be
/// returned.
QString QXmppUploadRequestManager::requestUploadSlot(const QFileInfo &file,
const QString &customFileName,
const QString &uploadService)
{
return requestUploadSlot(customFileName, file.size(),
QMimeDatabase().mimeTypeForFile(file),
uploadService);
}
/// Requests an upload slot from the server.
///
/// \param fileName The name of the file to be uploaded. This may be used by
/// the server to generate the URL.
/// \param fileSize The size of the file to be uploaded. The server may reject
/// too large files.
/// \param mimeType The content-type of the file. This can be used by the
/// server to set the HTTP MIME-type of the URL.
/// \param uploadService The HTTP File Upload service that is used to request
/// the upload slot. If this is empty, the first
/// discovered one is used.
/// \return The id of the sent IQ. If sendPacket() isn't successful or no
/// upload service has been discovered yet, an empty string will be
/// returned.
QString QXmppUploadRequestManager::requestUploadSlot(const QString &fileName,
qint64 fileSize,
const QMimeType &mimeType,
const QString &uploadService)
{
if (!serviceFound() && uploadService.isEmpty()) {
return {};
}
QXmppHttpUploadRequestIq iq;
if (uploadService.isEmpty()) {
iq.setTo(d->uploadServices.first().jid());
} else {
iq.setTo(uploadService);
}
iq.setType(QXmppIq::Get);
iq.setFileName(fileName);
iq.setSize(fileSize);
iq.setContentType(mimeType);
if (client()->sendPacket(iq)) {
return iq.id();
}
return {};
}
///
/// Requests an upload slot from the server.
///
/// \param file The info of the file to be uploaded.
/// \param uploadService The HTTP File Upload service that is used to request
/// the upload slot. If this is empty, the first
/// discovered one is used.
///
/// \warning THIS API IS NOT FINALIZED YET!
///
/// \since QXmpp 1.5
///
auto QXmppUploadRequestManager::requestSlot(const QFileInfo &file,
const QString &uploadService) -> QXmppTask<SlotResult>
{
return requestSlot(file, file.fileName(), uploadService);
}
///
/// Requests an upload slot from the server.
///
/// \param file The info of the file to be uploaded.
/// \param customFileName This name is used instead of the file's actual name
/// for requesting the upload slot.
/// \param uploadService The HTTP File Upload service that is used to request
/// the upload slot. If this is empty, the first
/// discovered one is used.
///
/// \warning THIS API IS NOT FINALIZED YET!
///
/// \since QXmpp 1.5
///
auto QXmppUploadRequestManager::requestSlot(const QFileInfo &file,
const QString &customFileName,
const QString &uploadService) -> QXmppTask<SlotResult>
{
return requestSlot(customFileName, file.size(),
QMimeDatabase().mimeTypeForFile(file),
uploadService);
}
///
/// Requests an upload slot from the server.
///
/// \param fileName The name of the file to be uploaded. This may be used by
/// the server to generate the URL.
/// \param fileSize The size of the file to be uploaded. The server may reject
/// too large files.
/// \param mimeType The content-type of the file. This can be used by the
/// server to set the HTTP MIME-type of the URL.
/// \param uploadService The HTTP File Upload service that is used to request
/// the upload slot. If this is empty, the first
/// discovered one is used.
///
/// \warning THIS API IS NOT FINALIZED YET!
///
/// \since QXmpp 1.5
///
auto QXmppUploadRequestManager::requestSlot(const QString &fileName,
qint64 fileSize,
const QMimeType &mimeType,
const QString &uploadService) -> QXmppTask<SlotResult>
{
if (!serviceFound() && uploadService.isEmpty()) {
return makeReadyTask(SlotResult(QXmppError {
QStringLiteral("Couldn't request upload slot: No service found."), {} }));
}
QXmppHttpUploadRequestIq iq;
if (uploadService.isEmpty()) {
iq.setTo(d->uploadServices.first().jid());
} else {
iq.setTo(uploadService);
}
iq.setType(QXmppIq::Get);
iq.setFileName(fileName);
iq.setSize(fileSize);
iq.setContentType(mimeType);
return chainIq<SlotResult>(client()->sendIq(std::move(iq)), this);
}
/// Returns true if an HTTP File Upload service has been discovered.
bool QXmppUploadRequestManager::serviceFound() const
{
return !d->uploadServices.isEmpty();
}
/// Returns all discovered HTTP File Upload services.
QVector<QXmppUploadService> QXmppUploadRequestManager::uploadServices() const
{
return d->uploadServices;
}
bool QXmppUploadRequestManager::handleStanza(const QDomElement &element)
{
if (QXmppHttpUploadSlotIq::isHttpUploadSlotIq(element)) {
QXmppHttpUploadSlotIq slot;
slot.parse(element);
Q_EMIT slotReceived(slot);
return true;
} else if (QXmppHttpUploadRequestIq::isHttpUploadRequestIq(element)) {
QXmppHttpUploadRequestIq requestError;
requestError.parse(element);
Q_EMIT requestFailed(requestError);
return true;
}
return false;
}
void QXmppUploadRequestManager::handleDiscoInfo(const QXmppDiscoveryIq &iq)
{
if (!iq.features().contains(ns_http_upload)) {
return;
}
const auto identities = iq.identities();
for (const QXmppDiscoveryIq::Identity &identity : identities) {
if (identity.category() == QStringLiteral("store") &&
identity.type() == QStringLiteral("file")) {
QXmppUploadService service;
service.setJid(iq.from());
// get size limit
bool isFormNsCorrect = false;
const auto fields = iq.form().fields();
for (const QXmppDataForm::Field &field : fields) {
if (field.key() == QStringLiteral("FORM_TYPE")) {
isFormNsCorrect = field.value() == ns_http_upload;
} else if (isFormNsCorrect && field.key() == QStringLiteral("max-file-size")) {
service.setSizeLimit(field.value().toLongLong());
}
}
d->uploadServices.append(service);
Q_EMIT serviceFoundChanged();
}
}
return;
}
void QXmppUploadRequestManager::setClient(QXmppClient *client)
{
QXmppClientExtension::setClient(client);
// connect to service discovery manager
auto *disco = client->findExtension<QXmppDiscoveryManager>();
if (disco) {
// scan info of all entities for upload services
connect(disco, &QXmppDiscoveryManager::infoReceived,
this, &QXmppUploadRequestManager::handleDiscoInfo);
// on client disconnect remove all upload services
connect(client, &QXmppClient::disconnected, this, [this]() {
d->uploadServices.clear();
Q_EMIT serviceFoundChanged();
});
}
}
|