Passed
Push — master ( c5dc9b...fc7f04 )
by Morris
29:58 queued 19:19
created

CleanupCardDAVPhotoCache::repair()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 15
nc 5
nop 1
dl 0
loc 24
rs 9.4555
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
/**
4
 * @copyright Copyright (c) 2019, Daniel Kesselberg ([email protected])
5
 *
6
 * @license GNU AGPL version 3 or any later version
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License as
10
 * published by the Free Software Foundation, either version 3 of the
11
 * License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
23
namespace OC\Repair\NC16;
24
25
use OCP\Files\IAppData;
26
use OCP\Files\NotFoundException;
27
use OCP\Files\SimpleFS\ISimpleFolder;
28
use OCP\IConfig;
29
use OCP\ILogger;
30
use OCP\Migration\IOutput;
31
use OCP\Migration\IRepairStep;
32
33
/**
34
 * Class CleanupCardDAVPhotoCache
35
 *
36
 * This repair step removes "photo." files created by photocache
37
 *
38
 * Before https://github.com/nextcloud/server/pull/13843 a "photo." file could be created
39
 * for unsupported image formats by photocache. Because a file is present but not jpg, png or gif no
40
 * photo could be returned for this vcard. These invalid files are removed by this migration step.
41
 */
42
class CleanupCardDAVPhotoCache implements IRepairStep {
43
44
	/** @var IConfig */
45
	private $config;
46
47
	/** @var IAppData */
48
	private $appData;
49
50
	/** @var ILogger */
51
	private $logger;
52
53
	public function __construct(IConfig $config, IAppData $appData, ILogger $logger) {
54
		$this->config = $config;
55
		$this->appData = $appData;
56
		$this->logger = $logger;
57
	}
58
59
	public function getName(): string {
60
		return 'Cleanup invalid photocache files for carddav';
61
	}
62
63
	private function repair(IOutput $output): void {
64
		try {
65
			$folders = $this->appData->getDirectoryListing();
66
		} catch (NotFoundException $e) {
67
			return;
68
		}
69
70
		$folders = array_filter($folders, function (ISimpleFolder $folder) {
71
			return $folder->fileExists('photo.');
72
		});
73
74
		if (empty($folders)) {
75
			return;
76
		}
77
78
		$output->info('Delete ' . count($folders) . ' "photo." files');
79
80
		foreach ($folders as $folder) {
81
			try {
82
				/** @var ISimpleFolder $folder */
83
				$folder->getFile('photo.')->delete();
84
			} catch (\Exception $e) {
85
				$this->logger->logException($e);
86
				$output->warning('Could not delete file "dav-photocache/' . $folder->getName() . '/photo."');
87
			}
88
		}
89
	}
90
91
	private function shouldRun(): bool {
92
		return version_compare(
93
			$this->config->getSystemValue('version', '0.0.0.0'),
94
			'16.0.0.0',
95
			'<='
96
		);
97
	}
98
99
	public function run(IOutput $output): void {
100
		if ($this->shouldRun()) {
101
			$this->repair($output);
102
		}
103
	}
104
}
105