Skip to content

Commit

Permalink
Merge branch '1.5'
Browse files Browse the repository at this point in the history
  • Loading branch information
lnjX committed Oct 4, 2023
2 parents d5f40ce + 092ee8d commit 75ca355
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/base/QXmppHashing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ QFuture<HashVerificationResultPtr> QXmpp::Private::verifyHashes(std::unique_ptr<
}

std::sort(hashes.begin(), hashes.end(), [](const auto &a, const auto &b) {
return hashPriority(a.algorithm()) > hashPriority(b.algorithm());
return hashPriority(a.algorithm()) < hashPriority(b.algorithm());
});

auto expected = hashes.back();
Expand Down
2 changes: 2 additions & 0 deletions src/client/QXmppFileSharingManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,7 @@ std::shared_ptr<QXmppFileDownload> QXmppFileSharingManager::downloadFile(
download->reportFinished(QXmppError::fromFileDevice(*file));
return;
}
qDebug() << "filesharingmanager:" << "file size" << file->size();

download->d->hashesFuture = verifyHashes(
std::move(file),
Expand All @@ -574,6 +575,7 @@ std::shared_ptr<QXmppFileDownload> QXmppFileSharingManager::downloadFile(
};
},
[](HashVerificationResult::NotMatching) {
qDebug() << "hash verification result :: not matching";
return QXmppError {
QStringLiteral("Checksum does not match"),
{}
Expand Down
31 changes: 20 additions & 11 deletions src/client/QXmppHttpFileSharingProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ auto QXmppHttpFileSharingProvider::downloadFile(const std::any &source,
{
~State() override = default;

std::unique_ptr<QIODevice> output;
std::function<void(DownloadResult)> reportFinished;
std::optional<QXmppError> error;
QNetworkReply *reply = nullptr;
Expand All @@ -68,6 +69,15 @@ auto QXmppHttpFileSharingProvider::downloadFile(const std::any &source,
reply->abort();
}
}
void finish(DownloadResult &&result)
{
if (output && output->isOpen()) {
output->close();
}
reportFinished(std::move(result));
finished = true;
reply->deleteLater();
}
};

QXmppHttpFileSource httpSource;
Expand All @@ -78,27 +88,28 @@ auto QXmppHttpFileSharingProvider::downloadFile(const std::any &source,
}

auto state = std::make_shared<State>();
state->output = std::move(target);
state->reportFinished = std::move(reportFinished);
state->reply = d->netManager->get(QNetworkRequest(httpSource.url()));

QObject::connect(state->reply, &QNetworkReply::finished, [state]() mutable {
if (!state->finished) {
if (state->error) {
state->reportFinished(std::move(*state->error));
state->finish(std::move(*state->error));
} else if (state->cancelled) {
state->reportFinished(Cancelled());
state->finish(Cancelled());
} else {
state->reportFinished(Success());
state->finish(Success());
}
state->finished = true;
state->reply->deleteLater();
}
});

QObject::connect(state->reply, &QNetworkReply::readyRead, [state, file = std::move(target)]() {
QObject::connect(state->reply, &QNetworkReply::readyRead, [state]() {
Q_ASSERT(state->output);

auto data = state->reply->readAll();
if (file->write(data) != data.size()) {
state->error = QXmppError::fromIoDevice(*file);
if (state->output->write(data) != data.size()) {
state->error = QXmppError::fromIoDevice(*state->output);
}
});

Expand All @@ -112,9 +123,7 @@ auto QXmppHttpFileSharingProvider::downloadFile(const std::any &source,
[state](QNetworkReply::NetworkError) {
// Qt doc: the finished() signal will "probably" follow
// => we can't be sure that finished() is going to be called
state->reportFinished(QXmppError::fromNetworkReply(*state->reply));
state->finished = true;
state->reply->deleteLater();
state->finish(QXmppError::fromNetworkReply(*state->reply));
});

return std::dynamic_pointer_cast<QXmppFileSharingProvider::Download>(state);
Expand Down

0 comments on commit 75ca355

Please sign in to comment.