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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
|
// SPDX-FileCopyrightText: 2022 Jonah Brüchert <jbb@kaidan.im>
// SPDX-FileCopyrightText: 2022 Linus Jahn <lnj@kaidan.im>
//
// SPDX-License-Identifier: LGPL-2.1-or-later
#include "QXmppFileSharingManager.h"
#include "QXmppBitsOfBinaryContentId.h"
#include "QXmppBitsOfBinaryData.h"
#include "QXmppClient.h"
#include "QXmppFileMetadata.h"
#include "QXmppFileShare.h"
#include "QXmppFutureUtils_p.h"
#include "QXmppHashing_p.h"
#include "QXmppThumbnail.h"
#include "QXmppUploadRequestManager.h"
#include "QXmppUtils_p.h"
#include <any>
#include <unordered_map>
#include <utility>
#include <QFile>
#include <QFileInfo>
#include <QFutureInterface>
#include <QMimeDatabase>
#include <QNetworkAccessManager>
#include <QNetworkReply>
using namespace QXmpp;
using namespace QXmpp::Private;
using MetadataGenerator = QXmppFileSharingManager::MetadataGenerator;
using MetadataGeneratorResult = QXmppFileSharingManager::MetadataGeneratorResult;
// The manager generates a hash with each hash algorithm
static std::vector<HashAlgorithm> hashAlgorithms()
{
return {
HashAlgorithm::Sha256,
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
HashAlgorithm::Blake2b_256,
#else
HashAlgorithm::Sha3_256,
#endif
};
}
template<typename T, typename Converter>
auto transform(T &input, Converter convert)
{
using Output = std::decay_t<decltype(convert(input.front()))>;
std::vector<Output> output;
output.reserve(input.size());
std::transform(input.begin(), input.end(), std::back_inserter(output), std::move(convert));
return output;
}
class QXmppFileUploadPrivate
{
public:
std::shared_ptr<QXmppFileSharingProvider::Upload> providerUpload;
QFuture<std::shared_ptr<MetadataGeneratorResult>> metadataFuture;
QFuture<HashingResultPtr> hashesFuture;
std::optional<QXmppError> error;
QXmppFileMetadata metadata;
QXmppBitsOfBinaryDataList dataBlobs;
std::any source;
quint64 bytesSent = 0;
quint64 bytesTotal = 0;
bool finished = false;
bool cancelled = false;
bool success = false;
};
///
/// \class QXmppFileUpload
///
/// \brief Provides progress of stateless file sharing uploads.
///
/// \since QXmpp 1.5
///
///
/// \class QXmppFileUpload::FileResult
///
/// \brief Contains QXmppFileShare of the uploaded file and possible data blobs containing
/// referenced thumbnails.
///
///
/// \var QXmppFileUpload::FileResult::fileShare
///
/// \brief File share with file metadata and file shares of the uploaded file.
///
///
/// \var QXmppFileUpload::FileResult::dataBlobs
///
/// \brief Data blobs of possibly in the metadata referenced thumbnails.
///
/// The QXmppFileSharingManager may generate file thumbnails.
///
///
/// \typedef QXmppFileUpload::Result
///
/// \brief Contains FileResult (successfully finished), QXmpp::Cancelled (manually cancelled)
/// or QXmppError (an error occured while uploading).
///
QXmppFileUpload::~QXmppFileUpload() = default;
///
/// Returns the current progress between 0.0 and 1.0.
///
float QXmppFileUpload::progress() const
{
return calculateProgress(d->bytesSent, d->bytesTotal);
}
///
/// \fn QXmppFileUpload::progressChanged()
///
/// \brief Emitted when new bytes have been transferred.
///
///
/// \brief Cancels the file transfer. finished() will be emitted.
///
void QXmppFileUpload::cancel()
{
if (d->providerUpload) {
d->providerUpload->cancel();
}
d->metadataFuture.cancel();
d->hashesFuture.cancel();
}
///
/// \brief Returns whether the file transfer is finished.
///
bool QXmppFileUpload::isFinished() const
{
return d->finished;
}
///
/// \brief Returns the number of bytes that have been uploaded or downloaded.
///
quint64 QXmppFileUpload::bytesTransferred() const
{
return d->bytesSent;
}
///
/// \brief Returns the number of bytes that totally need to be transferred.
///
quint64 QXmppFileUpload::bytesTotal() const
{
return d->bytesTotal;
}
///
/// \brief Returns the result of the upload.
///
/// The upload must be finished when calling this.
///
QXmppFileUpload::Result QXmppFileUpload::result() const
{
Q_ASSERT(d->finished);
if (d->error) {
return *d->error;
}
if (d->cancelled) {
return Cancelled();
}
if (d->success) {
QXmppFileShare fs;
fs.setMetadata(d->metadata);
fs.addSource(d->source);
return FileResult { std::move(fs), d->dataBlobs };
}
Q_UNREACHABLE();
}
///
/// \fn QXmppFileUpload::finished
///
/// Emitted when the upload has finished.
///
QXmppFileUpload::QXmppFileUpload()
: d(std::make_unique<QXmppFileUploadPrivate>())
{
}
void QXmppFileUpload::reportFinished()
{
Q_ASSERT(d->error || d->cancelled || d->success);
if (!d->finished) {
d->finished = true;
Q_EMIT finished();
}
}
class QXmppFileDownloadPrivate
{
public:
std::shared_ptr<QXmppFileSharingProvider::Download> providerDownload;
QFuture<HashVerificationResultPtr> hashesFuture;
QVector<QXmppHash> hashes;
QXmppFileDownload::Result result;
quint64 bytesReceived = 0;
quint64 bytesTotal = 0;
bool finished = false;
};
///
/// \class QXmppFileDownload
///
/// \brief Provides progress of stateless file sharing uploads.
///
/// \since QXmpp 1.5
///
///
/// \enum QXmppFileDownload::HashVerificationResult
///
/// Describes the result of the hash verification.
///
///
/// \struct QXmppFileDownload::Downloaded
///
/// Indicates that the file could be downloaded.
///
///
/// \var QXmppFileDownload::Downloaded::hashVerificationResult
///
/// Describes the result of the hash verification.
///
///
/// \typedef QXmppFileDownload::Result
///
/// \brief Contains QXmpp::Success (successfully finished), QXmpp::Cancelled (manually cancelled)
/// or QXmppError (an error occured while downloading).
///
QXmppFileDownload::~QXmppFileDownload() = default;
///
/// Returns the current progress between 0.0 and 1.0.
///
float QXmppFileDownload::progress() const
{
return calculateProgress(d->bytesReceived, d->bytesTotal);
}
///
/// \fn QXmppFileDownload::progressChanged
///
/// \brief Emitted when new bytes have been transferred.
///
///
/// \brief Cancels the file transfer. finished() will be emitted.
///
void QXmppFileDownload::cancel()
{
if (d->providerDownload) {
d->providerDownload->cancel();
}
d->hashesFuture.cancel();
}
///
/// \brief Returns whether the file transfer is finished.
///
bool QXmppFileDownload::isFinished() const
{
return d->finished;
}
///
/// \brief Returns the number of bytes that have been uploaded or downloaded.
///
quint64 QXmppFileDownload::bytesTransferred() const
{
return d->bytesReceived;
}
///
/// \brief Returns the number of bytes that totally need to be transferred.
///
quint64 QXmppFileDownload::bytesTotal() const
{
return d->bytesTotal;
}
///
/// \brief Returns the result of the download.
///
/// The download must be finished when calling this.
///
QXmppFileDownload::Result QXmppFileDownload::result() const
{
Q_ASSERT(d->finished);
return d->result;
}
///
/// \fn QXmppFileDownload::finished
///
/// Emitted when the download has finished.
///
QXmppFileDownload::QXmppFileDownload()
: d(std::make_unique<QXmppFileDownloadPrivate>())
{
}
void QXmppFileDownload::reportProgress(quint64 bytesReceived, quint64 bytesTotal)
{
d->bytesReceived = bytesReceived;
d->bytesTotal = bytesTotal;
Q_EMIT progressChanged();
}
void QXmppFileDownload::reportFinished(Result result)
{
d->finished = true;
d->result = std::move(result);
Q_EMIT finished();
}
class QXmppFileSharingManagerPrivate
{
public:
MetadataGenerator metadataGenerator = [](std::unique_ptr<QIODevice>) -> QFuture<std::shared_ptr<MetadataGeneratorResult>> {
return makeReadyFuture(std::make_shared<MetadataGeneratorResult>());
};
std::unordered_map<std::type_index, std::shared_ptr<QXmppFileSharingProvider>> providers;
};
///
/// \class QXmppFileSharingProvider
///
/// Base class for Stateless File Sharing providers
///
/// A provider defines the way that files can be uploaded and downloaded.
///
/// An example is the QXmppHttpFileSharingProvider, which uses HTTP File Upload.
///
/// \since QXmpp 1.5
///
///
/// \class QXmppFileSharingManager
///
/// The file sharing manager allows to easily send and retrieve files in a chat.
///
/// \since QXmpp 1.5
///
QXmppFileSharingManager::QXmppFileSharingManager()
: d(std::make_unique<QXmppFileSharingManagerPrivate>())
{
}
QXmppFileSharingManager::~QXmppFileSharingManager() = default;
///
/// \typedef QXmppFileSharingManager::MetadataGenerator
///
/// The function signature of a metadata generator function
///
///
/// \brief Register a function that is called when metadata needs to be gererated for a file.
///
/// The function is passed a QIODevice, so if you need the path of the file on disk,
/// you can dynamically cast it to a QFile and access the fileName.
/// When doing that, make sure to check the result,
/// as in the future this function might be passed other QIODevices than QFile.
///
void QXmppFileSharingManager::setMetadataGenerator(MetadataGenerator &&generator)
{
d->metadataGenerator = std::move(generator);
}
///
/// \brief Upload a file in a way that it can be attached to a message.
/// \param provider The provider class decides how the file is uploaded
/// \param filePath Path to a file that should be uploaded
/// \param description Optional description of the file
/// \return An object that allows to track the progress of the upload.
/// Once the upload is finished, the finished signal is emitted on it.
///
std::shared_ptr<QXmppFileUpload> QXmppFileSharingManager::uploadFile(std::shared_ptr<QXmppFileSharingProvider> provider,
const QString &filePath,
const std::optional<QString> &description)
{
QFileInfo fileInfo(filePath);
auto metadata = QXmppFileMetadata::fromFileInfo(fileInfo);
metadata.setDescription(description);
std::shared_ptr<QXmppFileUpload> upload(new QXmppFileUpload());
upload->d->metadata = std::move(metadata);
auto openFile = [=]() -> std::unique_ptr<QIODevice> {
auto device = std::make_unique<QFile>(fileInfo.absoluteFilePath());
if (!device->open(QIODevice::ReadOnly)) {
upload->d->error = QXmppError::fromIoDevice(*device);
upload->reportFinished();
}
return device;
};
auto metadataIoDevice = openFile();
auto hashesIoDevice = openFile();
auto uploadIoDevice = openFile();
if (upload->d->finished) {
// error occurred while opening file
return upload;
}
upload->d->metadataFuture = d->metadataGenerator(std::move(metadataIoDevice));
upload->d->hashesFuture = calculateHashes(std::move(hashesIoDevice), hashAlgorithms());
auto onProgress = [upload](quint64 sent, quint64 total) {
upload->d->bytesSent = sent;
upload->d->bytesTotal = total;
Q_EMIT upload->progressChanged();
};
auto onFinished = [this, upload](QXmppFileSharingProvider::UploadResult uploadResult) {
// free memory
upload->d->providerUpload.reset();
if (std::holds_alternative<std::any>(uploadResult)) {
upload->d->source = std::get<std::any>(std::move(uploadResult));
await(upload->d->metadataFuture, this, [this, upload](auto &&result) mutable {
if (result->dimensions) {
upload->d->metadata.setWidth(result->dimensions->width());
upload->d->metadata.setHeight(result->dimensions->height());
}
if (result->length) {
upload->d->metadata.setLength(*result->length);
}
if (!result->thumbnails.empty()) {
QVector<QXmppThumbnail> thumbnails;
thumbnails.reserve(result->thumbnails.size());
upload->d->dataBlobs.reserve(result->thumbnails.size());
for (const auto &metadataThumb : result->thumbnails) {
auto bobData = QXmppBitsOfBinaryData::fromByteArray(metadataThumb.data);
bobData.setContentType(metadataThumb.mimeType);
QXmppThumbnail thumbnail;
thumbnail.setHeight(metadataThumb.height);
thumbnail.setWidth(metadataThumb.width);
thumbnail.setMediaType(metadataThumb.mimeType);
thumbnail.setUri(bobData.cid().toCidUrl());
thumbnails.append(std::move(thumbnail));
upload->d->dataBlobs.append(std::move(bobData));
}
upload->d->metadata.setThumbnails(thumbnails);
}
await(upload->d->hashesFuture, this, [upload](auto hashResult) mutable {
auto &hashValue = hashResult->result;
if (std::holds_alternative<std::vector<QXmppHash>>(hashValue)) {
const auto &hashesVector = std::get<std::vector<QXmppHash>>(hashValue);
QVector<QXmppHash> hashes;
hashes.reserve(hashesVector.size());
std::transform(hashesVector.begin(), hashesVector.end(),
std::back_inserter(hashes), [](auto &&hash) {
return hash;
});
upload->d->metadata.setHashes(hashes);
upload->d->success = true;
} else if (std::holds_alternative<Cancelled>(hashValue)) {
upload->d->cancelled = true;
} else if (std::holds_alternative<QXmppError>(hashValue)) {
upload->d->error = std::get<QXmppError>(std::move(hashValue));
}
upload->reportFinished();
});
});
} else if (std::holds_alternative<Cancelled>(uploadResult)) {
upload->d->cancelled = true;
upload->reportFinished();
} else if (std::holds_alternative<QXmppError>(uploadResult)) {
upload->d->error = std::get<QXmppError>(std::move(uploadResult));
upload->reportFinished();
}
};
upload->d->providerUpload = provider->uploadFile(std::move(uploadIoDevice), upload->d->metadata, std::move(onProgress), std::move(onFinished));
return upload;
}
///
/// \brief Download a file from a QXmppFileShare
///
/// \warning This function currently does not check the hash of the downloaded file.
///
/// Make sure to register the provider
/// that handles the sources used in this file share before calling this function.
///
/// \param fileShare The file share object which you want to download
/// \param output An open QIODevice that the data should be written into.
/// In most cases, a QFile
/// \return An object that allows to track the progress of the download.
///
std::shared_ptr<QXmppFileDownload> QXmppFileSharingManager::downloadFile(
const QXmppFileShare &fileShare,
std::unique_ptr<QIODevice> output)
{
std::shared_ptr<QXmppFileDownload> download(new QXmppFileDownload());
download->d->hashes = fileShare.metadata().hashes();
// currently hashing does only work with QFiles
auto filePath = [&]() -> QString {
if (auto *file = dynamic_cast<QFile *>(output.get())) {
return file->fileName();
}
return {};
}();
auto onProgress = [download](quint64 received, quint64 total) {
download->reportProgress(received, total);
};
auto onFinished = [this, download, filePath](QXmppFileSharingProvider::DownloadResult result) mutable {
// reduce ref count
download->d->providerDownload.reset();
// pass errors directly
if (std::holds_alternative<Cancelled>(result)) {
download->reportFinished(Cancelled());
return;
}
if (std::holds_alternative<QXmppError>(result)) {
download->reportFinished(std::get<QXmppError>(std::move(result)));
return;
}
// try to do hash verification
if (filePath.isEmpty()) {
warning(QStringLiteral("Can't verify hashes of other io devices than QFile!"));
download->reportFinished(QXmppFileDownload::Downloaded { QXmppFileDownload::NoStrongHashes });
return;
}
auto file = std::make_unique<QFile>(filePath);
if (!file->open(QIODevice::ReadOnly)) {
download->reportFinished(QXmppError::fromFileDevice(*file));
return;
}
download->d->hashesFuture = verifyHashes(
std::move(file),
transform(download->d->hashes, [](auto hash) { return hash; }));
await(download->d->hashesFuture, this, [download](HashVerificationResultPtr hashResult) {
auto convert = overloaded {
[](HashVerificationResult::NoStrongHashes) {
return QXmppFileDownload::Downloaded {
QXmppFileDownload::NoStrongHashes
};
},
[](HashVerificationResult::NotMatching) {
return QXmppError {
QStringLiteral("Checksum does not match"),
{}
};
},
[](HashVerificationResult::Verified) {
return QXmppFileDownload::Downloaded {
QXmppFileDownload::HashVerified
};
}
};
download->reportFinished(visitForward<QXmppFileDownload::Result>(hashResult->result, convert));
});
};
fileShare.visitSources([&](const std::any &source) {
if (auto provider = providerForSource(source)) {
download->d->providerDownload = provider->downloadFile(source, std::move(output), std::move(onProgress), std::move(onFinished));
return true;
}
return false;
});
return download;
}
void QXmppFileSharingManager::internalRegisterProvider(std::type_index index, std::shared_ptr<QXmppFileSharingProvider> provider)
{
d->providers.insert_or_assign(index, provider);
}
std::shared_ptr<QXmppFileSharingProvider> QXmppFileSharingManager::providerForSource(const std::any &source) const
{
if (auto provider = d->providers.find(std::type_index(source.type()));
provider != d->providers.cend()) {
return provider->second;
}
return {};
}
|