bulkpropagatorjob.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright 2021 (c) Matthieu Gallien <matthieu.gallien@nextcloud.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * for more details.
  13. */
  14. #pragma once
  15. #include "owncloudpropagator.h"
  16. #include "abstractnetworkjob.h"
  17. #include <QLoggingCategory>
  18. #include <QVector>
  19. #include <QMap>
  20. #include <QByteArray>
  21. #include <deque>
  22. namespace OCC {
  23. Q_DECLARE_LOGGING_CATEGORY(lcBulkPropagatorJob)
  24. class ComputeChecksum;
  25. class PutMultiFileJob;
  26. class BulkPropagatorJob : public PropagatorJob
  27. {
  28. Q_OBJECT
  29. /* This is a minified version of the SyncFileItem,
  30. * that holds only the specifics about the file that's
  31. * being uploaded.
  32. *
  33. * This is needed if we wanna apply changes on the file
  34. * that's being uploaded while keeping the original on disk.
  35. */
  36. struct UploadFileInfo {
  37. QString _file; /// I'm still unsure if I should use a SyncFilePtr here.
  38. QString _path; /// the full path on disk.
  39. qint64 _size = 0LL;
  40. };
  41. struct BulkUploadItem
  42. {
  43. AccountPtr _account;
  44. SyncFileItemPtr _item;
  45. UploadFileInfo _fileToUpload;
  46. QString _remotePath;
  47. QString _localPath;
  48. qint64 _fileSize;
  49. QMap<QByteArray, QByteArray> _headers;
  50. };
  51. public:
  52. explicit BulkPropagatorJob(OwncloudPropagator *propagator,
  53. const std::deque<SyncFileItemPtr> &items);
  54. bool scheduleSelfOrChild() override;
  55. [[nodiscard]] JobParallelism parallelism() const override;
  56. private slots:
  57. void startUploadFile(OCC::SyncFileItemPtr item, OCC::BulkPropagatorJob::UploadFileInfo fileToUpload);
  58. // Content checksum computed, compute the transmission checksum
  59. void slotComputeTransmissionChecksum(OCC::SyncFileItemPtr item,
  60. OCC::BulkPropagatorJob::UploadFileInfo fileToUpload);
  61. // transmission checksum computed, prepare the upload
  62. void slotStartUpload(OCC::SyncFileItemPtr item,
  63. OCC::BulkPropagatorJob::UploadFileInfo fileToUpload,
  64. const QByteArray &transmissionChecksumType,
  65. const QByteArray &transmissionChecksum);
  66. // invoked on internal error to unlock a folder and faile
  67. void slotOnErrorStartFolderUnlock(OCC::SyncFileItemPtr item,
  68. OCC::SyncFileItem::Status status,
  69. const QString &errorString);
  70. void slotPutFinished();
  71. void slotUploadProgress(OCC::SyncFileItemPtr item, qint64 sent, qint64 total);
  72. void slotJobDestroyed(QObject *job);
  73. private:
  74. void doStartUpload(SyncFileItemPtr item,
  75. UploadFileInfo fileToUpload,
  76. QByteArray transmissionChecksumHeader);
  77. void adjustLastJobTimeout(AbstractNetworkJob *job,
  78. qint64 fileSize) const;
  79. void finalize(const QJsonObject &fullReply);
  80. void finalizeOneFile(const BulkUploadItem &oneFile);
  81. void slotPutFinishedOneFile(const BulkUploadItem &singleFile,
  82. OCC::PutMultiFileJob *job,
  83. const QJsonObject &fullReplyObject);
  84. void done(SyncFileItemPtr item,
  85. SyncFileItem::Status status,
  86. const QString &errorString);
  87. /** Bases headers that need to be sent on the PUT, or in the MOVE for chunking-ng */
  88. [[nodiscard]] QMap<QByteArray, QByteArray> headers(SyncFileItemPtr item) const;
  89. void abortWithError(SyncFileItemPtr item,
  90. SyncFileItem::Status status,
  91. const QString &error);
  92. /**
  93. * Checks whether the current error is one that should reset the whole
  94. * transfer if it happens too often. If so: Bump UploadInfo::errorCount
  95. * and maybe perform the reset.
  96. */
  97. void checkResettingErrors(SyncFileItemPtr item) const;
  98. /**
  99. * Error handling functionality that is shared between jobs.
  100. */
  101. void commonErrorHandling(SyncFileItemPtr item,
  102. const QString &errorMessage);
  103. bool checkFileStillExists(SyncFileItemPtr item,
  104. const bool finished,
  105. const QString &fullFilePath);
  106. bool checkFileChanged(SyncFileItemPtr item,
  107. const bool finished,
  108. const QString &fullFilePath);
  109. void computeFileId(SyncFileItemPtr item,
  110. const QJsonObject &fileReply) const;
  111. void handleFileRestoration(SyncFileItemPtr item,
  112. const QString &errorString) const;
  113. void handleBulkUploadBlackList(SyncFileItemPtr item) const;
  114. void handleJobDoneErrors(SyncFileItemPtr item,
  115. SyncFileItem::Status status);
  116. void triggerUpload();
  117. void checkPropagationIsDone();
  118. std::deque<SyncFileItemPtr> _items;
  119. QVector<AbstractNetworkJob *> _jobs; /// network jobs that are currently in transit
  120. QSet<QString> _pendingChecksumFiles;
  121. std::vector<BulkUploadItem> _filesToUpload;
  122. SyncFileItem::Status _finalStatus = SyncFileItem::Status::NoStatus;
  123. };
  124. }