Passed
Push — master ( 3c693d...ccd5ca )
by Roeland
28:56 queued 14:09
created

CleanPreviewsBackgroundJob::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 5
dl 0
loc 10
rs 10
1
<?php
2
/**
3
 * @copyright 2016 Roeland Jago Douma <[email protected]>
4
 *
5
 * @author Roeland Jago Douma <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
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
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
namespace OC\Repair\Owncloud;
24
25
use OC\BackgroundJob\QueuedJob;
26
use OCP\AppFramework\Utility\ITimeFactory;
27
use OCP\BackgroundJob\IJobList;
28
use OCP\Files\Folder;
29
use OCP\Files\IRootFolder;
30
use OCP\Files\NotFoundException;
31
use OCP\Files\NotPermittedException;
32
use OCP\ILogger;
33
use OCP\IUserManager;
34
35
class CleanPreviewsBackgroundJob extends QueuedJob {
36
	/** @var IRootFolder */
37
	private $rootFolder;
38
39
	/** @var ILogger */
40
	private $logger;
41
42
	/** @var IJobList */
43
	private $jobList;
44
45
	/** @var ITimeFactory */
46
	private $timeFactory;
47
48
	/** @var IUserManager */
49
	private $userManager;
50
51
	/**
52
	 * CleanPreviewsBackgroundJob constructor.
53
	 *
54
	 * @param IRootFolder $rootFolder
55
	 * @param ILogger $logger
56
	 * @param IJobList $jobList
57
	 * @param ITimeFactory $timeFactory
58
	 * @param IUserManager $userManager
59
	 */
60
	public function __construct(IRootFolder $rootFolder,
61
								ILogger $logger,
62
								IJobList $jobList,
63
								ITimeFactory $timeFactory,
64
								IUserManager $userManager) {
65
		$this->rootFolder = $rootFolder;
66
		$this->logger = $logger;
67
		$this->jobList = $jobList;
68
		$this->timeFactory = $timeFactory;
69
		$this->userManager = $userManager;
70
	}
71
72
	public function run($arguments) {
73
		$uid = $arguments['uid'];
74
		if (!$this->userManager->userExists($uid)) {
75
			$this->logger->info('User no longer exists, skip user ' . $uid);
76
			return;
77
		}
78
		$this->logger->info('Started preview cleanup for ' . $uid);
79
		$empty = $this->cleanupPreviews($uid);
80
81
		if (!$empty) {
82
			$this->jobList->add(self::class, ['uid' => $uid]);
83
			$this->logger->info('New preview cleanup scheduled for ' . $uid);
84
		} else {
85
			$this->logger->info('Preview cleanup done for ' . $uid);
86
		}
87
	}
88
89
	/**
90
	 * @param $uid
91
	 * @return bool
92
	 */
93
	private function cleanupPreviews($uid) {
94
		try {
95
			$userFolder = $this->rootFolder->getUserFolder($uid);
96
		} catch (NotFoundException $e) {
97
			return true;
98
		}
99
100
		$userRoot = $userFolder->getParent();
101
102
		try {
103
			/** @var Folder $thumbnailFolder */
104
			$thumbnailFolder = $userRoot->get('thumbnails');
105
		} catch (NotFoundException $e) {
106
			return true;
107
		}
108
109
		$thumbnails = $thumbnailFolder->getDirectoryListing();
110
111
		$start = $this->timeFactory->getTime();
112
		foreach ($thumbnails as $thumbnail) {
113
			try {
114
				$thumbnail->delete();
115
			} catch (NotPermittedException $e) {
116
				// Ignore
117
			}
118
119
			if (($this->timeFactory->getTime() - $start) > 15) {
120
				return false;
121
			}
122
		}
123
124
		try {
125
			$thumbnailFolder->delete();
126
		} catch (NotPermittedException $e) {
127
			// Ignore
128
		}
129
130
		return true;
131
	}
132
}
133