Completed
Push — master ( d2cc48...0dcb6b )
by Morris
37:29 queued 19:35
created

BackgroundCleanupJob   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
dl 0
loc 59
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
B run() 0 36 5
1
<?php
2
declare(strict_types=1);
3
/**
4
 * @copyright Copyright (c) 2018, Roeland Jago Douma <[email protected]>
5
 *
6
 * @author Roeland Jago Douma <[email protected]>
7
 *
8
 * @license AGPL-3.0
9
 *
10
 * This code is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License, version 3,
12
 * as published by the Free Software Foundation.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License, version 3,
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
21
 *
22
 */
23
24
namespace OC\Preview;
25
26
use OC\BackgroundJob\TimedJob;
27
use OC\Files\AppData\Factory;
28
use OCP\DB\QueryBuilder\IQueryBuilder;
29
use OCP\Files\NotFoundException;
30
use OCP\Files\NotPermittedException;
31
use OCP\IDBConnection;
32
33
class BackgroundCleanupJob extends TimedJob {
34
35
	/** @var IDBConnection */
36
	private $connection;
37
38
	/** @var Factory */
39
	private $appDataFactory;
40
41
	/** @var bool */
42
	private $isCLI;
43
44
	public function __construct(IDBConnection $connection,
45
								Factory $appDataFactory,
46
								bool $isCLI) {
47
		// Run at most once an hour
48
		$this->setInterval(3600);
49
50
		$this->connection = $connection;
51
		$this->appDataFactory = $appDataFactory;
52
		$this->isCLI = $isCLI;
53
	}
54
55
	public function run($argument) {
56
		$previews = $this->appDataFactory->get('preview');
57
58
		$previewFodlerId = $previews->getId();
59
60
		$qb = $this->connection->getQueryBuilder();
61
		$qb->select('a.name')
62
			->from('filecache', 'a')
63
			->leftJoin('a', 'filecache', 'b', $qb->expr()->eq(
64
				$qb->expr()->castColumn('a.name', IQueryBuilder::PARAM_INT), 'b.fileid'
65
			))
66
			->where(
67
				$qb->expr()->isNull('b.fileid')
68
			)->andWhere(
69
				$qb->expr()->eq('a.parent', $qb->createNamedParameter($previewFodlerId))
70
			);
71
72
		if (!$this->isCLI) {
73
			$qb->setMaxResults(10);
74
		}
75
76
		$cursor = $qb->execute();
77
78
		while ($row = $cursor->fetch()) {
79
			try {
80
				$preview = $previews->getFolder($row['name']);
81
				$preview->delete();
82
			} catch (NotFoundException $e) {
83
				// continue
84
			} catch (NotPermittedException $e) {
85
				// continue
86
			}
87
		}
88
89
		$cursor->closeCursor();
90
	}
91
}
92