-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathRetentionJob.php
280 lines (239 loc) · 8.38 KB
/
RetentionJob.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Files_Retention\BackgroundJob;
use Exception;
use OC\Files\Filesystem;
use OCA\Files_Retention\AppInfo\Application;
use OCA\Files_Retention\Constants;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJobList;
use OCP\BackgroundJob\TimedJob;
use OCP\Files\Config\ICachedMountFileInfo;
use OCP\Files\Config\IUserMountCache;
use OCP\Files\IRootFolder;
use OCP\Files\Node;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\Notification\IManager as NotificationManager;
use OCP\SystemTag\ISystemTagManager;
use OCP\SystemTag\ISystemTagObjectMapper;
use OCP\SystemTag\TagNotFoundException;
use Psr\Log\LoggerInterface;
class RetentionJob extends TimedJob {
public function __construct(
ITimeFactory $timeFactory,
private readonly ISystemTagManager $tagManager,
private readonly ISystemTagObjectMapper $tagMapper,
private readonly IUserMountCache $userMountCache,
private readonly IDBConnection $db,
private readonly IRootFolder $rootFolder,
private readonly IJobList $jobList,
private readonly LoggerInterface $logger,
private readonly NotificationManager $notificationManager,
private readonly IConfig $config,
) {
parent::__construct($timeFactory);
// Run once a day
$this->setInterval(24 * 60 * 60);
}
public function run($argument): void {
// Validate if tag still exists
$tag = $argument['tag'];
try {
$this->tagManager->getTagsByIds((string)$tag);
} catch (\InvalidArgumentException $e) {
// tag is invalid remove backgroundjob and exit
$this->jobList->remove($this, $argument);
$this->logger->debug("Background job was removed, because tag $tag is invalid", [
'exception' => $e,
]);
return;
} catch (TagNotFoundException $e) {
// tag no longer exists remove backgroundjob and exit
$this->jobList->remove($this, $argument);
$this->logger->debug("Background job was removed, because tag $tag no longer exists", [
'exception' => $e,
]);
return;
}
// Validate if there is an entry in the DB
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from('retention')
->where($qb->expr()->eq('tag_id', $qb->createNamedParameter($tag)));
$cursor = $qb->executeQuery();
$data = $cursor->fetch();
$cursor->closeCursor();
if ($data === false) {
// No entry anymore in the retention db
$this->jobList->remove($this, $argument);
$this->logger->debug("Background job was removed, because tag $tag has no retention configured");
return;
}
// Do we notify the user before
$notifyDayBefore = $this->config->getAppValue(Application::APP_ID, 'notify_before', 'no') === 'yes';
// Calculate before date only once
$deleteBefore = $this->getBeforeDate((int)$data['time_unit'], (int)$data['time_amount']);
$notifyBefore = $this->getNotifyBeforeDate($deleteBefore);
if ($notifyDayBefore) {
$this->logger->debug("Running retention for Tag $tag with delete before " . $deleteBefore->format(\DateTimeInterface::ATOM) . ' and notify before ' . $notifyBefore->format(\DateTimeInterface::ATOM));
} else {
$this->logger->debug("Running retention for Tag $tag with delete before " . $deleteBefore->format(\DateTimeInterface::ATOM));
}
$timeAfter = (int)$data['time_after'];
$offset = '';
$limit = 1000;
while ($offset !== null) {
$fileIds = $this->tagMapper->getObjectIdsForTags((string)$tag, 'files', $limit, $offset);
$this->logger->debug('Checking retention for ' . count($fileIds) . ' files in this chunk');
foreach ($fileIds as $fileId) {
$fileId = (int)$fileId;
try {
$node = $this->checkFileId($fileId);
} catch (NotFoundException $e) {
$this->logger->debug("Node with id $fileId was not found", [
'exception' => $e,
]);
continue;
}
$deleted = $this->expireNode($node, $deleteBefore, $timeAfter);
if ($notifyDayBefore && !$deleted) {
$this->notifyNode($node, $notifyBefore);
}
}
if (empty($fileIds) || count($fileIds) < $limit) {
break;
}
$offset = (string)array_pop($fileIds);
}
}
/**
* Get a node for the given fileid.
*
* @param int $fileId
* @return Node
* @throws NotFoundException
*/
private function checkFileId(int $fileId): Node {
$mountPoints = $this->userMountCache->getMountsForFileId($fileId);
if (empty($mountPoints)) {
throw new NotFoundException("No mount points found for file $fileId");
}
foreach ($mountPoints as $mountPoint) {
try {
return $this->getDeletableNodeFromMountPoint($mountPoint, $fileId);
} catch (NotPermittedException $e) {
// Check the next mount point
$this->logger->debug('Mount point ' . ($mountPoint->getMountId() ?? 'null') . ' has no delete permissions for file ' . $fileId);
} catch (NotFoundException $e) {
// Already logged explicitly inside
}
}
throw new NotFoundException("No mount point with delete permissions found for file $fileId");
}
protected function getDeletableNodeFromMountPoint(ICachedMountFileInfo $mountPoint, int $fileId): Node {
try {
$userId = $mountPoint->getUser()->getUID();
$userFolder = $this->rootFolder->getUserFolder($userId);
if (!Filesystem::$loaded) {
// Filesystem wasn't loaded for anyone,
// so we boot it up in order to make hooks in the View work.
Filesystem::init($userId, '/' . $userId . '/files');
}
} catch (Exception $e) {
$this->logger->debug($e->getMessage(), [
'exception' => $e,
]);
throw new NotFoundException('Could not get user', 0, $e);
}
$nodes = $userFolder->getById($fileId);
if (empty($nodes)) {
throw new NotFoundException('No node for file ' . $fileId . ' and user ' . $userId);
}
foreach ($nodes as $node) {
if ($node->isDeletable()) {
return $node;
}
$this->logger->debug('Mount point ' . ($mountPoint->getMountId() ?? 'null') . ' has access to node ' . $node->getId() . ' but permissions are ' . $node->getPermissions());
}
throw new NotPermittedException();
}
private function expireNode(Node $node, \DateTime $deleteBefore, int $timeAfter): bool {
$mtime = new \DateTime();
// Fallback is the mtime
$mtime->setTimestamp($node->getMTime());
// Use the upload time if we have it
if ($timeAfter === Constants::CTIME && $node->getUploadTime() !== 0) {
$mtime->setTimestamp($node->getUploadTime());
}
if ($mtime < $deleteBefore) {
$this->logger->debug('Expiring file ' . $node->getId());
try {
$node->delete();
return true;
} catch (Exception $e) {
$this->logger->debug($e->getMessage(), [
'exception' => $e,
]);
}
} else {
$this->logger->debug('Skipping file ' . $node->getId() . ' from expiration');
}
return false;
}
private function notifyNode(Node $node, \DateTime $notifyBefore): void {
$mtime = new \DateTime();
// Fallback is the mtime
$mtime->setTimestamp($node->getMTime());
// Use the upload time if we have it
if ($node->getUploadTime() !== 0) {
$mtime->setTimestamp($node->getUploadTime());
}
if ($mtime < $notifyBefore) {
$this->logger->debug('Notifying about retention tomorrow for file ' . $node->getId());
try {
$notification = $this->notificationManager->createNotification();
$notification->setApp(Application::APP_ID)
->setUser($node->getOwner()->getUID())
->setDateTime(new \DateTime())
->setObject('retention', (string)$node->getId())
->setSubject('deleteTomorrow', [
'fileId' => $node->getId(),
]);
$this->notificationManager->notify($notification);
} catch (Exception $e) {
$this->logger->error($e->getMessage(), [
'exception' => $e,
]);
}
}
}
private function getBeforeDate(int $timeunit, int $timeAmount): \DateTime {
$spec = 'P' . $timeAmount;
if ($timeunit === Constants::DAY) {
$spec .= 'D';
} elseif ($timeunit === Constants::WEEK) {
$spec .= 'W';
} elseif ($timeunit === Constants::MONTH) {
$spec .= 'M';
} elseif ($timeunit === Constants::YEAR) {
$spec .= 'Y';
}
$delta = new \DateInterval($spec);
$currentDate = new \DateTime();
$currentDate->setTimestamp($this->time->getTime());
return $currentDate->sub($delta);
}
private function getNotifyBeforeDate(\DateTime $retentionDate): \DateTime {
$spec = 'P1D';
$delta = new \DateInterval($spec);
$retentionDate = clone $retentionDate;
return $retentionDate->add($delta);
}
}