Passed
Push — master ( 8f4e6f...2bd98d )
by Morris
14:37 queued 02:45
created

ChunkCleanup::run()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 30
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 16
nc 2
nop 1
dl 0
loc 30
rs 9.7333
c 0
b 0
f 0
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 GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
25
namespace OCA\DAV\Migration;
26
27
use OCA\DAV\BackgroundJob\UploadCleanup;
28
use OCP\BackgroundJob\IJobList;
29
use OCP\Files\Folder;
30
use OCP\Files\IRootFolder;
31
use OCP\Files\NotFoundException;
32
use OCP\IConfig;
33
use OCP\IUser;
34
use OCP\IUserManager;
35
use OCP\Migration\IOutput;
36
use OCP\Migration\IRepairStep;
37
38
class ChunkCleanup implements IRepairStep {
39
40
	/** @var IConfig */
41
	private $config;
42
	/** @var IUserManager */
43
	private $userManager;
44
	/** @var IRootFolder */
45
	private $rootFolder;
46
	/** @var IJobList */
47
	private $jobList;
48
49
	public function __construct(IConfig $config,
50
								IUserManager $userManager,
51
								IRootFolder $rootFolder,
52
								IJobList $jobList) {
53
		$this->config = $config;
54
		$this->userManager = $userManager;
55
		$this->rootFolder = $rootFolder;
56
		$this->jobList = $jobList;
57
	}
58
59
	public function getName(): string {
60
		return 'Chunk cleanup scheduler';
61
	}
62
63
	public function run(IOutput $output) {
64
		// If we already ran this onec there is no need to run it again
65
		if ($this->config->getAppValue('dav', 'chunks_migrated', '0') === '1') {
66
			$output->info('Cleanup not required');
67
		}
68
69
		$output->startProgress();
70
		// Loop over all seen users
71
		$this->userManager->callForSeenUsers(function (IUser $user) use ($output) {
72
			try {
73
				$userFolder = $this->rootFolder->getUserFolder($user->getUID());
74
				$userRoot = $userFolder->getParent();
75
				/** @var Folder $uploadFolder */
76
				$uploadFolder = $userRoot->get('uploads');
77
			} catch (NotFoundException $e) {
78
				// No folder so skipping
79
				return;
80
			}
81
82
			// Insert a cleanup job for each folder we find
83
			$uploads = $uploadFolder->getDirectoryListing();
84
			foreach ($uploads as $upload) {
85
				$this->jobList->add(UploadCleanup::class, ['uid' => $user->getUID(), 'folder' => $upload->getName()]);
86
			}
87
			$output->advance();
88
		});
89
		$output->finishProgress();
90
91
92
		$this->config->setAppValue('dav', 'chunks_migrated', '1');
93
	}
94
95
}
96