Refactor startChunkUpload, move upload finalisation to separate method
authorClaudio Cambra <claudio.cambra@nextcloud.com>
Thu, 27 Jul 2023 08:10:13 +0000 (16:10 +0800)
committerMatthieu Gallien <matthieu_gallien@yahoo.fr>
Thu, 31 Aug 2023 13:25:00 +0000 (15:25 +0200)
Signed-off-by: Claudio Cambra <claudio.cambra@nextcloud.com>
src/libsync/propagateupload.h
src/libsync/propagateuploadng.cpp

index f2c947ce689b685e821ae1e420888695b46d1e18..10f42ddc8dadb116565c404b7277a4690c9bbc40 100644 (file)
@@ -411,8 +411,11 @@ public:
 private:
     void startNewUpload();
     void startNextChunk();
+    void finishUpload();
+
 public slots:
     void abort(OCC::PropagateUploadFileNG::AbortType abortType) override;
+
 private slots:
     void slotPropfindFinished();
     void slotPropfindFinishedWithError();
index 528b0a27cae042f1ff6070379ff551e09654772b..e0458b2ef58075303437194823e8d95fb7f328a1 100644 (file)
@@ -287,6 +287,40 @@ void PropagateUploadFileNG::slotMkColFinished()
     startNextChunk();
 }
 
+void PropagateUploadFileNG::finishUpload()
+{
+    Q_ASSERT(_jobs.isEmpty()); // There should be no running job anymore
+    _finished = true;
+
+    // Finish with a MOVE
+    // If we changed the file name, we must store the changed filename in the remote folder, not the original one.
+    const auto destination = QDir::cleanPath(propagator()->account()->davUrl().path() + propagator()->fullRemotePath(_fileToUpload._file));
+    auto headers = PropagateUploadFileCommon::headers();
+
+    // "If-Match applies to the source, but we are interested in comparing the etag of the destination
+    const auto ifMatch = headers.take(QByteArrayLiteral("If-Match"));
+    if (!ifMatch.isEmpty()) {
+        headers[QByteArrayLiteral("If")] = "<" + QUrl::toPercentEncoding(destination, "/") + "> ([" + ifMatch + "])";
+    }
+
+    if (!_transmissionChecksumHeader.isEmpty()) {
+        qCInfo(lcPropagateUpload) << destination << _transmissionChecksumHeader;
+        headers[checkSumHeaderC] = _transmissionChecksumHeader;
+    }
+
+    const auto fileSize = _fileToUpload._size;
+    headers[QByteArrayLiteral("OC-Total-Length")] = QByteArray::number(fileSize);
+
+    const auto job = new MoveJob(propagator()->account(), Utility::concatUrlPath(chunkUrl(), "/.file"), destination, headers, this);
+    _jobs.append(job);
+    connect(job, &MoveJob::finishedSignal, this, &PropagateUploadFileNG::slotMoveJobFinished);
+    connect(job, &QObject::destroyed, this, &PropagateUploadFileCommon::slotJobDestroyed);
+    propagator()->_activeJobList.append(this);
+    adjustLastJobTimeout(job, fileSize);
+    job->start();
+    return;
+}
+
 void PropagateUploadFileNG::startNextChunk()
 {
     if (propagator()->_abortRequested)
@@ -294,38 +328,11 @@ void PropagateUploadFileNG::startNextChunk()
 
     const auto fileSize = _fileToUpload._size;
     ENFORCE(fileSize >= _sent, "Sent data exceeds file size")
-
-    const auto destination = QDir::cleanPath(propagator()->account()->davUrl().path() + propagator()->fullRemotePath(_fileToUpload._file));
-
     // prevent situation that chunk size is bigger then required one to send
     _currentChunkSize = qMin(propagator()->_chunkSize, fileSize - _sent);
 
     if (_currentChunkSize == 0) {
-        Q_ASSERT(_jobs.isEmpty()); // There should be no running job anymore
-        _finished = true;
-
-        // Finish with a MOVE
-        // If we changed the file name, we must store the changed filename in the remote folder, not the original one.
-        auto headers = PropagateUploadFileCommon::headers();
-
-        // "If-Match applies to the source, but we are interested in comparing the etag of the destination
-        auto ifMatch = headers.take(QByteArrayLiteral("If-Match"));
-        if (!ifMatch.isEmpty()) {
-            headers[QByteArrayLiteral("If")] = "<" + QUrl::toPercentEncoding(destination, "/") + "> ([" + ifMatch + "])";
-        }
-        if (!_transmissionChecksumHeader.isEmpty()) {
-            qCInfo(lcPropagateUpload) << destination << _transmissionChecksumHeader;
-            headers[checkSumHeaderC] = _transmissionChecksumHeader;
-        }
-        headers[QByteArrayLiteral("OC-Total-Length")] = QByteArray::number(fileSize);
-
-        const auto job = new MoveJob(propagator()->account(), Utility::concatUrlPath(chunkUrl(), "/.file"), destination, headers, this);
-        _jobs.append(job);
-        connect(job, &MoveJob::finishedSignal, this, &PropagateUploadFileNG::slotMoveJobFinished);
-        connect(job, &QObject::destroyed, this, &PropagateUploadFileCommon::slotJobDestroyed);
-        propagator()->_activeJobList.append(this);
-        adjustLastJobTimeout(job, fileSize);
-        job->start();
+        finishUpload();
         return;
     }