Skip to content

Commit

Permalink
Use FetchAndUploadE2eeFolderMetadataJob in old classes.
Browse files Browse the repository at this point in the history
Signed-off-by: alex-z <[email protected]>
  • Loading branch information
allexzander committed Jun 19, 2023
1 parent b62e4c9 commit 308ea38
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 145 deletions.
93 changes: 34 additions & 59 deletions src/libsync/abstractpropagateremotedeleteencrypted.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,52 +55,39 @@ void AbstractPropagateRemoteDeleteEncrypted::storeFirstErrorString(const QString
}
}

void AbstractPropagateRemoteDeleteEncrypted::startLsColJob(const QString &path)
void AbstractPropagateRemoteDeleteEncrypted::fetchMetadataForPath(const QString &path)
{
qCDebug(ABSTRACT_PROPAGATE_REMOVE_ENCRYPTED) << "Folder is encrypted, let's get the Id from it.";
qCDebug(ABSTRACT_PROPAGATE_REMOVE_ENCRYPTED) << "Folder is encrypted, let's its metadata.";
_fullFolderRemotePath = _propagator->fullRemotePath(path);
auto job = new LsColJob(_propagator->account(), _fullFolderRemotePath, this);
job->setProperties({"resourcetype", "http://owncloud.org/ns:fileid"});
connect(job, &LsColJob::directoryListingSubfolders, this, &AbstractPropagateRemoteDeleteEncrypted::slotFolderEncryptedIdReceived);
connect(job, &LsColJob::finishedWithError, this, &AbstractPropagateRemoteDeleteEncrypted::taskFailed);
job->start();
}

void AbstractPropagateRemoteDeleteEncrypted::slotFolderEncryptedIdReceived(const QStringList &list)
{
qCDebug(ABSTRACT_PROPAGATE_REMOVE_ENCRYPTED) << "Received id of folder, trying to lock it so we can prepare the metadata";
auto job = qobject_cast<LsColJob *>(sender());
const ExtraFolderInfo folderInfo = job->_folderInfos.value(list.first());
slotTryLock(folderInfo.fileId);
}
SyncJournalFileRecord rec;
if (!_propagator->_journal->getRootE2eFolderRecord(_fullFolderRemotePath, &rec) || !rec.isValid()) {
taskFailed();
return;
}

void AbstractPropagateRemoteDeleteEncrypted::slotTryLock(const QByteArray &folderId)
{
auto lockJob = new LockEncryptFolderApiJob(_propagator->account(), folderId, _propagator->_journal, _propagator->account()->e2e()->_publicKey, this);
connect(lockJob, &LockEncryptFolderApiJob::success, this, &AbstractPropagateRemoteDeleteEncrypted::slotFolderLockedSuccessfully);
connect(lockJob, &LockEncryptFolderApiJob::error, this, &AbstractPropagateRemoteDeleteEncrypted::taskFailed);
lockJob->start();
}
_fetchAndUploadE2eeFolderMetadataJob.reset(new FetchAndUploadE2eeFolderMetadataJob(_propagator->account(),
_fullFolderRemotePath,
_propagator->_journal,
rec.path(), _fullFolderRemotePath));

connect(_fetchAndUploadE2eeFolderMetadataJob.data(),
&FetchAndUploadE2eeFolderMetadataJob::fetchFinished,
this,
&AbstractPropagateRemoteDeleteEncrypted::slotFetchMetadataJobFinished);

void AbstractPropagateRemoteDeleteEncrypted::slotFolderLockedSuccessfully(const QByteArray &folderId, const QByteArray &token)
{
qCDebug(ABSTRACT_PROPAGATE_REMOVE_ENCRYPTED) << "Folder id" << folderId << "Locked Successfully for Upload, Fetching Metadata";
_folderLocked = true;
_folderToken = token;
_folderId = folderId;

auto job = new GetMetadataApiJob(_propagator->account(), _folderId);
connect(job, &GetMetadataApiJob::jsonReceived, this, &AbstractPropagateRemoteDeleteEncrypted::slotFolderEncryptedMetadataReceived);
connect(job, &GetMetadataApiJob::error, this, &AbstractPropagateRemoteDeleteEncrypted::taskFailed);
job->start();
}

void AbstractPropagateRemoteDeleteEncrypted::slotFolderUnLockedSuccessfully(const QByteArray &folderId)
void AbstractPropagateRemoteDeleteEncrypted::slotFolderUnLockFinished(const QByteArray &folderId, int statusCode)
{
Q_UNUSED(folderId);
if (statusCode != 200) {
_item->_httpErrorCode = statusCode;
_errorString = tr("\"%1 Failed to unlock encrypted folder %2\".").arg(statusCode).arg(QString::fromUtf8(folderId));
_item->_errorString = _errorString;
taskFailed();
return;
}
qCDebug(ABSTRACT_PROPAGATE_REMOVE_ENCRYPTED) << "Folder id" << folderId << "successfully unlocked";
_folderLocked = false;
_folderToken = "";
}

void AbstractPropagateRemoteDeleteEncrypted::slotDeleteRemoteItemFinished()
Expand Down Expand Up @@ -150,52 +137,40 @@ void AbstractPropagateRemoteDeleteEncrypted::slotDeleteRemoteItemFinished()
}
_propagator->_journal->commit("Remote Remove");

unlockFolder();
unlockFolder(true);
}

void AbstractPropagateRemoteDeleteEncrypted::deleteRemoteItem(const QString &filename)
{
qCInfo(ABSTRACT_PROPAGATE_REMOVE_ENCRYPTED) << "Deleting nested encrypted item" << filename;

auto deleteJob = new DeleteJob(_propagator->account(), _propagator->fullRemotePath(filename), this);
deleteJob->setFolderToken(_folderToken);
deleteJob->setFolderToken(_fetchAndUploadE2eeFolderMetadataJob->folderToken());

connect(deleteJob, &DeleteJob::finishedSignal, this, &AbstractPropagateRemoteDeleteEncrypted::slotDeleteRemoteItemFinished);

deleteJob->start();
}

void AbstractPropagateRemoteDeleteEncrypted::unlockFolder()
void AbstractPropagateRemoteDeleteEncrypted::unlockFolder(bool success)
{
if (!_folderLocked) {
if (!_fetchAndUploadE2eeFolderMetadataJob->isFolderLocked()) {
emit finished(true);
return;
}

qCDebug(ABSTRACT_PROPAGATE_REMOVE_ENCRYPTED) << "Unlocking folder" << _folderId;
auto unlockJob = new UnlockEncryptFolderApiJob(_propagator->account(), _folderId, _folderToken, _propagator->_journal, this);

connect(unlockJob, &UnlockEncryptFolderApiJob::success, this, &AbstractPropagateRemoteDeleteEncrypted::slotFolderUnLockedSuccessfully);
connect(unlockJob, &UnlockEncryptFolderApiJob::error, this, [this] (const QByteArray& fileId, int httpReturnCode) {
Q_UNUSED(fileId);
_folderLocked = false;
_folderToken = "";
_item->_httpErrorCode = httpReturnCode;
_errorString = tr("\"%1 Failed to unlock encrypted folder %2\".")
.arg(httpReturnCode)
.arg(QString::fromUtf8(fileId));
_item->_errorString =_errorString;
taskFailed();
});
unlockJob->start();
qCDebug(ABSTRACT_PROPAGATE_REMOVE_ENCRYPTED) << "Unlocking folder" << _fetchAndUploadE2eeFolderMetadataJob->folderId();

connect(_fetchAndUploadE2eeFolderMetadataJob.data(), &FetchAndUploadE2eeFolderMetadataJob::folderUnlocked, this, &AbstractPropagateRemoteDeleteEncrypted::slotFolderUnLockFinished);
_fetchAndUploadE2eeFolderMetadataJob->unlockFolder(success);
}

void AbstractPropagateRemoteDeleteEncrypted::taskFailed()
{
qCDebug(ABSTRACT_PROPAGATE_REMOVE_ENCRYPTED) << "Task failed for job" << sender();
_isTaskFailed = true;
if (_folderLocked) {
unlockFolder();
if (_fetchAndUploadE2eeFolderMetadataJob->isFolderLocked()) {
unlockFolder(false);
} else {
emit finished(false);
}
Expand Down
18 changes: 7 additions & 11 deletions src/libsync/abstractpropagateremotedeleteencrypted.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include <QObject>
#include <QString>
#include <QNetworkReply>

#include "fetchanduploade2eefoldermetadatajob.h"
#include "syncfileitem.h"

namespace OCC {
Expand Down Expand Up @@ -46,28 +46,24 @@ class AbstractPropagateRemoteDeleteEncrypted : public QObject
void storeFirstError(QNetworkReply::NetworkError err);
void storeFirstErrorString(const QString &errString);

void startLsColJob(const QString &path);
void slotFolderEncryptedIdReceived(const QStringList &list);
void slotTryLock(const QByteArray &folderId);
void slotFolderLockedSuccessfully(const QByteArray &folderId, const QByteArray &token);
virtual void slotFolderUnLockedSuccessfully(const QByteArray &folderId);
virtual void slotFolderEncryptedMetadataReceived(const QJsonDocument &json, int statusCode) = 0;
void fetchMetadataForPath(const QString &path);
virtual void slotFolderUnLockFinished(const QByteArray &folderId, int statusCode);
virtual void slotFetchMetadataJobFinished(int statusCode, const QString &message) = 0;
virtual void slotUpdateMetadataJobFinished(int statusCode, const QString &message) = 0;
void slotDeleteRemoteItemFinished();

void deleteRemoteItem(const QString &filename);
void unlockFolder();
void unlockFolder(bool success);
void taskFailed();

protected:
OwncloudPropagator *_propagator = nullptr;
SyncFileItemPtr _item;
QByteArray _folderToken;
QByteArray _folderId;
bool _folderLocked = false;
bool _isTaskFailed = false;
QNetworkReply::NetworkError _networkError = QNetworkReply::NoError;
QString _errorString;
QString _fullFolderRemotePath;
QScopedPointer<FetchAndUploadE2eeFolderMetadataJob> _fetchAndUploadE2eeFolderMetadataJob;
};

}
76 changes: 34 additions & 42 deletions src/libsync/propagateremotedeleteencrypted.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@ void PropagateRemoteDeleteEncrypted::start()
Q_ASSERT(!_item->_encryptedFileName.isEmpty());

const QFileInfo info(_item->_encryptedFileName);
startLsColJob(info.path());
fetchMetadataForPath(info.path());
}

void PropagateRemoteDeleteEncrypted::slotFolderUnLockedSuccessfully(const QByteArray &folderId)
void PropagateRemoteDeleteEncrypted::slotFolderUnLockFinished(const QByteArray &folderId, int statusCode)
{
AbstractPropagateRemoteDeleteEncrypted::slotFolderUnLockedSuccessfully(folderId);
AbstractPropagateRemoteDeleteEncrypted::slotFolderUnLockFinished(folderId, statusCode);
emit finished(!_isTaskFailed);
}

void PropagateRemoteDeleteEncrypted::slotFolderEncryptedMetadataReceived(const QJsonDocument &json, int statusCode)
void PropagateRemoteDeleteEncrypted::slotFetchMetadataJobFinished(int statusCode, const QString &message)
{
if (statusCode == 404) {
qCDebug(PROPAGATE_REMOVE_ENCRYPTED) << "Metadata not found, but let's proceed with removing the file anyway.";
Expand All @@ -58,48 +58,40 @@ void PropagateRemoteDeleteEncrypted::slotFolderEncryptedMetadataReceived(const Q
return;
}

auto metadata = (new FolderMetadata(
_propagator->account(),
statusCode == 404 ? QByteArray{} : json.toJson(QJsonDocument::Compact),
FolderMetadata::RootEncryptedFolderInfo(FolderMetadata::RootEncryptedFolderInfo::createRootPath(rec.path(), fullFolderRemotePathSanitized))));
connect(metadata, &FolderMetadata::setupComplete, this, [this, metadata] {
if (!metadata->isValid()) {
taskFailed();
metadata->deleteLater();
return;
}
const auto metadata = _fetchAndUploadE2eeFolderMetadataJob->folderMetadata();

qCDebug(PROPAGATE_REMOVE_ENCRYPTED) << "Metadata Received, preparing it for removal of the file";
if (!metadata->isValid()) {
taskFailed();
return;
}

const QFileInfo info(_propagator->fullLocalPath(_item->_file));
const QString fileName = info.fileName();
qCDebug(PROPAGATE_REMOVE_ENCRYPTED) << "Metadata Received, preparing it for removal of the file";

// Find existing metadata for this file
bool found = false;
const QVector<FolderMetadata::EncryptedFile> files = metadata->files();
for (const FolderMetadata::EncryptedFile &file : files) {
if (file.originalFilename == fileName) {
metadata->removeEncryptedFile(file);
found = true;
break;
}
}
const QFileInfo info(_propagator->fullLocalPath(_item->_file));
const QString fileName = info.fileName();

if (!found) {
// file is not found in the metadata, but we still need to remove it
deleteRemoteItem(_item->_encryptedFileName);
return;
// Find existing metadata for this file
bool found = false;
const QVector<FolderMetadata::EncryptedFile> files = metadata->files();
for (const FolderMetadata::EncryptedFile &file : files) {
if (file.originalFilename == fileName) {
metadata->removeEncryptedFile(file);
found = true;
break;
}
}

qCDebug(PROPAGATE_REMOVE_ENCRYPTED) << "Metadata updated, sending to the server.";

metadata->deleteLater();
const auto job = new UpdateMetadataApiJob(_propagator->account(), _folderId, metadata->encryptedMetadata(), _folderToken);
connect(job, &UpdateMetadataApiJob::success, this, [this](const QByteArray &fileId) {
Q_UNUSED(fileId);
deleteRemoteItem(_item->_encryptedFileName);
});
connect(job, &UpdateMetadataApiJob::error, this, &PropagateRemoteDeleteEncrypted::taskFailed);
job->start();
});
if (!found) {
// file is not found in the metadata, but we still need to remove it
deleteRemoteItem(_item->_encryptedFileName);
return;
}

qCDebug(PROPAGATE_REMOVE_ENCRYPTED) << "Metadata updated, sending to the server.";
_fetchAndUploadE2eeFolderMetadataJob->uploadMetadata(true);
}

void PropagateRemoteDeleteEncrypted::slotUpdateMetadataJobFinished(int statusCode, const QString &message)
{
deleteRemoteItem(_item->_encryptedFileName);
}
5 changes: 3 additions & 2 deletions src/libsync/propagateremotedeleteencrypted.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ class PropagateRemoteDeleteEncrypted : public AbstractPropagateRemoteDeleteEncry
void start() override;

private:
void slotFolderUnLockedSuccessfully(const QByteArray &folderId) override;
void slotFolderEncryptedMetadataReceived(const QJsonDocument &json, int statusCode) override;
void slotFolderUnLockFinished(const QByteArray &folderId, int statusCode) override;
void slotFetchMetadataJobFinished(int statusCode, const QString &message) override;
void slotUpdateMetadataJobFinished(int statusCode, const QString &message) override;
};

}
62 changes: 33 additions & 29 deletions src/libsync/propagateremotedeleteencryptedrootfolder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,51 +62,55 @@ void PropagateRemoteDeleteEncryptedRootFolder::start()
return;
}

startLsColJob(_item->_file);
fetchMetadataForPath(_item->_file);
}

void PropagateRemoteDeleteEncryptedRootFolder::slotFolderUnLockedSuccessfully(const QByteArray &folderId)
void PropagateRemoteDeleteEncryptedRootFolder::slotFolderUnLockFinished(const QByteArray &folderId, int statusCode)
{
AbstractPropagateRemoteDeleteEncrypted::slotFolderUnLockedSuccessfully(folderId);
decryptAndRemoteDelete();
AbstractPropagateRemoteDeleteEncrypted::slotFolderUnLockFinished(folderId, statusCode);
if (statusCode == 200) {
decryptAndRemoteDelete();
}
}

void PropagateRemoteDeleteEncryptedRootFolder::slotFolderEncryptedMetadataReceived(const QJsonDocument &json, int statusCode)
void PropagateRemoteDeleteEncryptedRootFolder::slotFetchMetadataJobFinished(int statusCode, const QString &message)
{
Q_UNUSED(message);
if (statusCode == 404) {
// we've eneded up having no metadata, but, _nestedItems is not empty since we went this far, let's proceed with removing the nested items without modifying the metadata
// we've eneded up having no metadata, but, _nestedItems is not empty since we went this far, let's proceed with removing the nested items without
// modifying the metadata
qCDebug(PROPAGATE_REMOVE_ENCRYPTED_ROOTFOLDER) << "There is no metadata for this folder. Just remove it's nested items.";
for (auto it = _nestedItems.constBegin(); it != _nestedItems.constEnd(); ++it) {
deleteNestedRemoteItem(it.key());
}
return;
}

auto metadata = new FolderMetadata(_propagator->account(), statusCode == 404 ? QByteArray{} : json.toJson(QJsonDocument::Compact), FolderMetadata::RootEncryptedFolderInfo::makeDefault());
connect(metadata, &FolderMetadata::setupComplete, this, [this, metadata] {
if (!metadata->isValid()) {
metadata->deleteLater();
taskFailed();
return;
}
const auto metadata = _fetchAndUploadE2eeFolderMetadataJob->folderMetadata();

qCDebug(PROPAGATE_REMOVE_ENCRYPTED_ROOTFOLDER) << "It's a root encrypted folder. Let's remove nested items first.";
if (!metadata->isValid()) {
taskFailed();
return;
}

metadata->removeAllEncryptedFiles();
qCDebug(PROPAGATE_REMOVE_ENCRYPTED_ROOTFOLDER) << "It's a root encrypted folder. Let's remove nested items first.";

qCDebug(PROPAGATE_REMOVE_ENCRYPTED_ROOTFOLDER) << "Metadata updated, sending to the server.";
metadata->removeAllEncryptedFiles();

const auto job = new UpdateMetadataApiJob(_propagator->account(), _folderId, metadata->encryptedMetadata(), _folderToken);
metadata->deleteLater();
connect(job, &UpdateMetadataApiJob::success, this, [this](const QByteArray &fileId) {
Q_UNUSED(fileId);
for (auto it = _nestedItems.constBegin(); it != _nestedItems.constEnd(); ++it) {
deleteNestedRemoteItem(it.key());
}
});
connect(job, &UpdateMetadataApiJob::error, this, &PropagateRemoteDeleteEncryptedRootFolder::taskFailed);
job->start();
});
qCDebug(PROPAGATE_REMOVE_ENCRYPTED_ROOTFOLDER) << "Metadata updated, sending to the server.";
_fetchAndUploadE2eeFolderMetadataJob->uploadMetadata(true);
}

void PropagateRemoteDeleteEncryptedRootFolder::slotUpdateMetadataJobFinished(int statusCode, const QString &message)
{
Q_UNUSED(message);
if (statusCode != 200) {
taskFailed();
return;
}
for (auto it = _nestedItems.constBegin(); it != _nestedItems.constEnd(); ++it) {
deleteNestedRemoteItem(it.key());
}
}

void PropagateRemoteDeleteEncryptedRootFolder::slotDeleteNestedRemoteItemFinished()
Expand Down Expand Up @@ -169,7 +173,7 @@ void PropagateRemoteDeleteEncryptedRootFolder::slotDeleteNestedRemoteItemFinishe
taskFailed();
return;
}
unlockFolder();
unlockFolder(true);
}
}

Expand All @@ -178,7 +182,7 @@ void PropagateRemoteDeleteEncryptedRootFolder::deleteNestedRemoteItem(const QStr
qCInfo(PROPAGATE_REMOVE_ENCRYPTED_ROOTFOLDER) << "Deleting nested encrypted remote item" << filename;

auto deleteJob = new DeleteJob(_propagator->account(), _propagator->fullRemotePath(filename), this);
deleteJob->setFolderToken(_folderToken);
deleteJob->setFolderToken(_fetchAndUploadE2eeFolderMetadataJob->folderToken());
deleteJob->setProperty(encryptedFileNamePropertyKey, filename);

connect(deleteJob, &DeleteJob::finishedSignal, this, &PropagateRemoteDeleteEncryptedRootFolder::slotDeleteNestedRemoteItemFinished);
Expand Down
Loading

0 comments on commit 308ea38

Please sign in to comment.