Welcome to mirror list, hosted at ThFree Co, Russian Federation.

RetentionJob.php « BackgroundJob « lib - github.com/nextcloud/files_retention.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 497ef1bed880460ef57ddd14a4e88193c9f19936 (plain)
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
<?php
/**
 *
 * @author Roeland Jago Douma <roeland@famdouma.nl>
 *
 * @license GNU AGPL version 3 or any later version
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
namespace OCA\Files_Retention\BackgroundJob;

use OC\BackgroundJob\TimedJob;
use OCA\Files_Retention\Constants;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJobList;
use OCP\Files\Config\IUserMountCache;
use OCP\IDBConnection;
use OCP\Files\Node;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\Files\IRootFolder;
use OCP\SystemTag\ISystemTagManager;
use OCP\SystemTag\ISystemTagObjectMapper;

class RetentionJob extends TimedJob {
	/** @var ISystemTagManager */
	private $tagManager;

	/** @var ISystemTagObjectMapper */
	private $tagMapper;

	/** @var IUserMountCache */
	private $userMountCache;

	/** @var IDBConnection */
	private $db;

	/** @var IRootFolder */
	private $rootFolder;

	/** @var ITimeFactory */
	private $timeFactory;

	/** @var IJobList */
	private $jobList;

	/**
	 * RetentionJob constructor.
	 *
	 * @param ISystemTagManager $tagManager
	 * @param ISystemTagObjectMapper $tagMapper
	 * @param IUserMountCache $userMountCache
	 * @param IDBConnection $db
	 * @param IRootFolder $rootFolder
	 * @param ITimeFactory $timeFactory
	 * @param IJobList $jobList
	 */
	public function __construct(ISystemTagManager $tagManager,
								ISystemTagObjectMapper $tagMapper,
								IUserMountCache $userMountCache,
								IDBConnection $db,
								IRootFolder $rootFolder,
								ITimeFactory $timeFactory,
								IJobList $jobList) {
		// Run once a day
		$this->setInterval(24 * 60 * 60);

		$this->tagManager = $tagManager;
		$this->tagMapper = $tagMapper;
		$this->userMountCache = $userMountCache;
		$this->db = $db;
		$this->rootFolder = $rootFolder;
		$this->timeFactory = $timeFactory;
		$this->jobList = $jobList;
	}

	public function run($argument) {
		// Validate if tag still exists
		$tag = $argument['tag'];
		try {
			$this->tagManager->getTagsByIds($tag);
		} catch (\InvalidArgumentException $e) {
			// tag no longer exists remove backgroundjob and exit
			$this->jobList->remove($this, $argument);
			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->execute();
		$data = $cursor->fetch();
		$cursor->closeCursor();

		if ($data === false) {
			// No entry anymore in the retention db
			$this->jobList->remove($this, $argument);
			return;
		}

		// Calculate before date only once
		$deleteBefore = $this->getBeforeDate((int)$data['time_unit'], (int)$data['time_amount']);

		$offset = 0;
		$limit = 1000;
		while(true) {
			$fileids = $this->tagMapper->getObjectIdsForTags($tag, 'files', $limit, $offset);

			foreach ($fileids as $fileid) {
				try {
					$node = $this->checkFileId($fileid);
				} catch (NotFoundException $e) {
					continue;
				}

				$this->expireNode($node, $deleteBefore);
			}

			if (empty($fileids) || count($fileids) < $limit) {
				break;
			}

			$offset += $limit;
		}
	}

	/**
	 * Get a node for the given fileid.
	 *
	 * @param int $fileid
	 * @return Node
	 * @throws NotFoundException
	 */
	private function checkFileId($fileid) {
		$mountPoints = $this->userMountCache->getMountsForFileId($fileid);

		if (count($mountPoints) === 0) {
			throw new NotFoundException();
		}

		$mountPoint = $mountPoints[0];

		$userFolder = $this->rootFolder->getUserFolder($mountPoint->getUser()->getUID());

		$nodes = $userFolder->getById($fileid);
		if (count($nodes) === 0) {
			throw new NotFoundException();
		}

		return $nodes[0];
	}

	/**
	 * @param Node $node
	 * @param \DateTime $deleteBefore
	 */
	private function expireNode(Node $node, \DateTime $deleteBefore) {
		$mtime = new \DateTime();
		$mtime->setTimestamp($node->getMTime());

		if ($mtime < $deleteBefore) {
			try {
				$node->delete();
			} catch (NotPermittedException $e) {
				//LOG?
			}
		}

	}

	/**
	 * @param int $timeunit
	 * @param int $timeamount
	 * @return \DateTime
	 */
	private function getBeforeDate($timeunit, $timeamount) {
		$spec = 'P' . $timeamount;

		if ($timeunit === Constants::DAY) {
			$spec .= 'D';
		} else if ($timeunit === Constants::WEEK) {
			$spec .= 'W';
		} else if ($timeunit === Constants::MONTH) {
			$spec .= 'M';
		} else if ($timeunit === Constants::YEAR) {
			$spec .= 'Y';
		}

		$delta = new \DateInterval($spec);
		$currentDate = new \DateTime();
		$currentDate->setTimestamp($this->timeFactory->getTime());

		return $currentDate->sub($delta);
	}
}