1 /*
2  * Copyright (C) by Olivier Goffart <ogoffart@owncloud.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 
15 #include "propagateremotedelete.h"
16 #include "propagateremotedeleteencrypted.h"
17 #include "propagateremotedeleteencryptedrootfolder.h"
18 #include "owncloudpropagator_p.h"
19 #include "account.h"
20 #include "deletejob.h"
21 #include "common/asserts.h"
22 
23 #include <QLoggingCategory>
24 
25 namespace OCC {
26 
27 Q_LOGGING_CATEGORY(lcPropagateRemoteDelete, "nextcloud.sync.propagator.remotedelete", QtInfoMsg)
28 
start()29 void PropagateRemoteDelete::start()
30 {
31     qCInfo(lcPropagateRemoteDelete) << "Start propagate remote delete job for" << _item->_file;
32 
33     if (propagator()->_abortRequested)
34         return;
35 
36     if (!_item->_encryptedFileName.isEmpty() || _item->_isEncrypted) {
37         if (!_item->_encryptedFileName.isEmpty()) {
38             _deleteEncryptedHelper = new PropagateRemoteDeleteEncrypted(propagator(), _item, this);
39         } else {
40             _deleteEncryptedHelper = new PropagateRemoteDeleteEncryptedRootFolder(propagator(), _item, this);
41         }
42         connect(_deleteEncryptedHelper, &AbstractPropagateRemoteDeleteEncrypted::finished, this, [this] (bool success) {
43             if (!success) {
44                 SyncFileItem::Status status = SyncFileItem::NormalError;
45                 if (_deleteEncryptedHelper->networkError() != QNetworkReply::NoError && _deleteEncryptedHelper->networkError() != QNetworkReply::ContentNotFoundError) {
46                     status = classifyError(_deleteEncryptedHelper->networkError(), _item->_httpErrorCode, &propagator()->_anotherSyncNeeded);
47                 }
48                 done(status, _deleteEncryptedHelper->errorString());
49             } else {
50                 done(SyncFileItem::Success);
51             }
52         });
53         _deleteEncryptedHelper->start();
54     } else {
55         createDeleteJob(_item->_file);
56     }
57 }
58 
createDeleteJob(const QString & filename)59 void PropagateRemoteDelete::createDeleteJob(const QString &filename)
60 {
61     qCInfo(lcPropagateRemoteDelete) << "Deleting file, local" << _item->_file << "remote" << filename;
62 
63     _job = new DeleteJob(propagator()->account(),
64         propagator()->fullRemotePath(filename),
65         this);
66 
67     connect(_job.data(), &DeleteJob::finishedSignal, this, &PropagateRemoteDelete::slotDeleteJobFinished);
68     propagator()->_activeJobList.append(this);
69     _job->start();
70 }
71 
abort(PropagatorJob::AbortType abortType)72 void PropagateRemoteDelete::abort(PropagatorJob::AbortType abortType)
73 {
74     if (_job && _job->reply())
75         _job->reply()->abort();
76 
77     if (abortType == AbortType::Asynchronous) {
78         emit abortFinished();
79     }
80 }
81 
slotDeleteJobFinished()82 void PropagateRemoteDelete::slotDeleteJobFinished()
83 {
84     propagator()->_activeJobList.removeOne(this);
85 
86     ASSERT(_job);
87 
88     QNetworkReply::NetworkError err = _job->reply()->error();
89     const int httpStatus = _job->reply()->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
90     _item->_httpErrorCode = httpStatus;
91     _item->_responseTimeStamp = _job->responseTimestamp();
92     _item->_requestId = _job->requestId();
93 
94     if (err != QNetworkReply::NoError && err != QNetworkReply::ContentNotFoundError) {
95         SyncFileItem::Status status = classifyError(err, _item->_httpErrorCode,
96             &propagator()->_anotherSyncNeeded);
97         done(status, _job->errorString());
98         return;
99     }
100 
101     // A 404 reply is also considered a success here: We want to make sure
102     // a file is gone from the server. It not being there in the first place
103     // is ok. This will happen for files that are in the DB but not on
104     // the server or the local file system.
105     if (httpStatus != 204 && httpStatus != 404) {
106         // Normally we expect "204 No Content"
107         // If it is not the case, it might be because of a proxy or gateway intercepting the request, so we must
108         // throw an error.
109         done(SyncFileItem::NormalError,
110             tr("Wrong HTTP code returned by server. Expected 204, but received \"%1 %2\".")
111                 .arg(_item->_httpErrorCode)
112                 .arg(_job->reply()->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString()));
113         return;
114     }
115 
116     propagator()->_journal->deleteFileRecord(_item->_originalFile, _item->isDirectory());
117     propagator()->_journal->commit("Remote Remove");
118 
119     done(SyncFileItem::Success);
120 }
121 }
122