From 8fb5a45163c29e6ff4747f2ced28a54778904e4e Mon Sep 17 00:00:00 2001 From: Jonah BrĂ¼chert Date: Thu, 29 Sep 2022 19:24:57 +0200 Subject: HttpUploadManager: Use unique_ptr for data device (#479) --- src/client/QXmppHttpUploadManager.cpp | 13 +++++++------ src/client/QXmppHttpUploadManager.h | 2 +- 2 files changed, 8 insertions(+), 7 deletions(-) (limited to 'src/client') diff --git a/src/client/QXmppHttpUploadManager.cpp b/src/client/QXmppHttpUploadManager.cpp index 728956e5..f6261773 100644 --- a/src/client/QXmppHttpUploadManager.cpp +++ b/src/client/QXmppHttpUploadManager.cpp @@ -273,7 +273,7 @@ QXmppHttpUploadManager::~QXmppHttpUploadManager() = default; /// }); /// \endcode /// -std::shared_ptr QXmppHttpUploadManager::uploadFile(QIODevice *data, const QString &filename, const QMimeType &mimeType, qint64 fileSize, const QString &uploadServiceJid) +std::shared_ptr QXmppHttpUploadManager::uploadFile(std::unique_ptr data, const QString &filename, const QMimeType &mimeType, qint64 fileSize, const QString &uploadServiceJid) { using SlotResult = QXmppUploadRequestManager::SlotResult; @@ -304,7 +304,7 @@ std::shared_ptr QXmppHttpUploadManager::uploadFile(QIODevice *d } auto future = client()->findExtension()->requestSlot(filename, fileSize, mimeType, uploadServiceJid); - await(future, this, [this, upload, data](SlotResult result) { + await(future, this, [this, upload, data = std::move(data)](SlotResult result) mutable { // first check whether upload was cancelled in the meantime if (upload->d->cancelled) { upload->d->reportFinished(); @@ -331,7 +331,10 @@ std::shared_ptr QXmppHttpUploadManager::uploadFile(QIODevice *d for (auto itr = headers.cbegin(); itr != headers.cend(); ++itr) { request.setRawHeader(itr.key().toUtf8(), itr.value().toUtf8()); } - auto *reply = d->netManager->put(request, data); + + auto *rawSourceDevice = data.release(); + auto *reply = d->netManager->put(request, rawSourceDevice); + rawSourceDevice->setParent(reply); upload->d->reply = reply; connect(reply, &QNetworkReply::finished, this, [reply, upload]() { @@ -400,12 +403,10 @@ std::shared_ptr QXmppHttpUploadManager::uploadFile(const QFileI } auto upload = uploadFile( - file.get(), + std::move(file), filename.isEmpty() ? fileInfo.fileName() : filename, QMimeDatabase().mimeTypeForFile(fileInfo), -1, uploadServiceJid); - // from now on lifetime is bound to the upload - file.release()->setParent(upload.get()); return upload; } diff --git a/src/client/QXmppHttpUploadManager.h b/src/client/QXmppHttpUploadManager.h index ff72e59d..5b0b0f68 100644 --- a/src/client/QXmppHttpUploadManager.h +++ b/src/client/QXmppHttpUploadManager.h @@ -58,7 +58,7 @@ public: explicit QXmppHttpUploadManager(QNetworkAccessManager *netManager); ~QXmppHttpUploadManager(); - std::shared_ptr uploadFile(QIODevice *data, const QString &filename, const QMimeType &mimeType, qint64 fileSize = -1, const QString &uploadServiceJid = {}); + std::shared_ptr uploadFile(std::unique_ptr data, const QString &filename, const QMimeType &mimeType, qint64 fileSize = -1, const QString &uploadServiceJid = {}); std::shared_ptr uploadFile(const QFileInfo &fileInfo, const QString &filename = {}, const QString &uploadServiceJid = {}); private: -- cgit v1.2.3