Passed
Push — master ( 5eaeba...884c80 )
by Roeland
11:13 queued 10s
created

BackgroundService::getAppDataFolder()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 5
rs 10
c 1
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2020 Julius Härtl <[email protected]>
4
 *
5
 * @author Julius Härtl <[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
24
declare(strict_types=1);
25
26
27
namespace OCA\Dashboard\Service;
28
29
use InvalidArgumentException;
30
use OC\User\NoUserException;
31
use OCP\Files\File;
32
use OCP\Files\IAppData;
33
use OCP\Files\IRootFolder;
34
use OCP\Files\NotFoundException;
35
use OCP\Files\NotPermittedException;
36
use OCP\Files\SimpleFS\ISimpleFile;
37
use OCP\Files\SimpleFS\ISimpleFolder;
38
use OCP\IConfig;
39
use OCP\Lock\LockedException;
40
use OCP\PreConditionNotMetException;
41
42
class BackgroundService {
43
	public const THEMING_MODE_DARK = 'dark';
44
45
	public const SHIPPED_BACKGROUNDS = [
46
		'anatoly-mikhaltsov-butterfly-wing-scale.jpg' => [
47
			'attribution' => 'Butterfly wing scale (Anatoly Mikhaltsov, CC BY-SA)',
48
			'attribution_url' => 'https://commons.wikimedia.org/wiki/File:%D0%A7%D0%B5%D1%88%D1%83%D0%B9%D0%BA%D0%B8_%D0%BA%D1%80%D1%8B%D0%BB%D0%B0_%D0%B1%D0%B0%D0%B1%D0%BE%D1%87%D0%BA%D0%B8.jpg',
49
		],
50
		'bernie-cetonia-aurata-take-off-composition.jpg' => [
51
			'attribution' => 'Cetonia aurata take off composition (Bernie, Public Domain)',
52
			'attribution_url' => 'https://commons.wikimedia.org/wiki/File:Cetonia_aurata_take_off_composition_05172009.jpg',
53
			'theming' => self::THEMING_MODE_DARK,
54
		],
55
		'dejan-krsmanovic-ribbed-red-metal.jpg' => [
56
			'attribution' => 'Ribbed red metal (Dejan Krsmanovic, CC BY)',
57
			'attribution_url' => 'https://www.flickr.com/photos/dejankrsmanovic/42971456774/',
58
		],
59
		'eduardo-neves-pedra-azul.jpg' => [
60
			'attribution' => 'Pedra azul milky way (Eduardo Neves, CC BY-SA)',
61
			'attribution_url' => 'https://commons.wikimedia.org/wiki/File:Pedra_Azul_Milky_Way.jpg',
62
		],
63
		'european-space-agency-barents-bloom.jpg' => [
64
			'attribution' => 'Barents bloom (European Space Agency, CC BY-SA)',
65
			'attribution_url' => 'https://www.esa.int/ESA_Multimedia/Images/2016/08/Barents_bloom',
66
		],
67
		'hannes-fritz-flippity-floppity.jpg' => [
68
			'attribution' => 'Flippity floppity (Hannes Fritz, CC BY-SA)',
69
			'attribution_url' => 'http://hannes.photos/flippity-floppity',
70
		],
71
		'hannes-fritz-roulette.jpg' => [
72
			'attribution' => 'Roulette (Hannes Fritz, CC BY-SA)',
73
			'attribution_url' => 'http://hannes.photos/roulette',
74
		],
75
		'hannes-fritz-sea-spray.jpg' => [
76
			'attribution' => 'Sea spray (Hannes Fritz, CC BY-SA)',
77
			'attribution_url' => 'http://hannes.photos/sea-spray',
78
		],
79
		'kamil-porembinski-clouds.jpg' => [
80
			'attribution' => 'Clouds (Kamil Porembiński, CC BY-SA)',
81
			'attribution_url' => 'https://www.flickr.com/photos/paszczak000/8715851521/',
82
		],
83
		'bernard-spragg-new-zealand-fern.jpg' => [
84
			'attribution' => 'New zealand fern (Bernard Spragg, CC0)',
85
			'attribution_url' => 'https://commons.wikimedia.org/wiki/File:NZ_Fern.(Blechnum_chambersii)_(11263534936).jpg',
86
		],
87
		'rawpixel-pink-tapioca-bubbles.jpg' => [
88
			'attribution' => 'Pink tapioca bubbles (Rawpixel, CC BY)',
89
			'attribution_url' => 'https://www.flickr.com/photos/byrawpixel/27665140298/in/photostream/',
90
			'theming' => self::THEMING_MODE_DARK,
91
		],
92
		'nasa-waxing-crescent-moon.jpg' => [
93
			'attribution' => 'Waxing crescent moon (NASA, Public Domain)',
94
			'attribution_url' => 'https://www.nasa.gov/image-feature/a-waxing-crescent-moon',
95
		],
96
		'tommy-chau-already.jpg' => [
97
			'attribution' => 'Cityscape (Tommy Chau, CC BY)',
98
			'attribution_url' => 'https://www.flickr.com/photos/90975693@N05/16910999368',
99
		],
100
		'tommy-chau-lion-rock-hill.jpg' => [
101
			'attribution' => 'Lion rock hill (Tommy Chau, CC BY)',
102
			'attribution_url' => 'https://www.flickr.com/photos/90975693@N05/17136440246',
103
			'theming' => self::THEMING_MODE_DARK,
104
		],
105
		'lali-masriera-yellow-bricks.jpg' => [
106
			'attribution' => 'Yellow bricks (Lali Masriera, CC BY)',
107
			'attribution_url' => 'https://www.flickr.com/photos/visualpanic/3982464447',
108
			'theming' => self::THEMING_MODE_DARK,
109
		]
110
	];
111
	/**
112
	 * @var IRootFolder
113
	 */
114
	private $rootFolder;
115
	/**
116
	 * @var IAppData
117
	 */
118
	private $appData;
119
	/**
120
	 * @var IConfig
121
	 */
122
	private $config;
123
	private $userId;
124
125
	public function __construct(IRootFolder $rootFolder, IAppData $appData, IConfig $config, $userId) {
126
		if ($userId === null) {
127
			return;
128
		}
129
		$this->rootFolder = $rootFolder;
130
		$this->appData = $appData;
131
		$this->config = $config;
132
		$this->userId = $userId;
133
	}
134
135
	public function setDefaultBackground(): void {
136
		$this->config->deleteUserValue($this->userId, 'dashboard', 'background');
137
	}
138
139
	/**
140
	 * @param $path
141
	 * @throws NotFoundException
142
	 * @throws NotPermittedException
143
	 * @throws LockedException
144
	 * @throws PreConditionNotMetException
145
	 * @throws NoUserException
146
	 */
147
	public function setFileBackground($path): void {
148
		$this->config->setUserValue($this->userId, 'dashboard', 'background', 'custom');
149
		$userFolder = $this->rootFolder->getUserFolder($this->userId);
150
		/** @var File $file */
151
		$file = $userFolder->get($path);
152
		$this->getAppDataFolder()->newFile('background.jpg', $file->fopen('r'));
153
	}
154
155
	public function setShippedBackground($fileName): void {
156
		if (!array_key_exists($fileName, self::SHIPPED_BACKGROUNDS)) {
157
			throw new InvalidArgumentException('The given file name is invalid');
158
		}
159
		$this->config->setUserValue($this->userId, 'dashboard', 'background', $fileName);
160
	}
161
162
	public function setColorBackground(string $color): void {
163
		if (!preg_match('/^#([0-9a-f]{3}|[0-9a-f]{6})$/i', $color)) {
164
			throw new InvalidArgumentException('The given color is invalid');
165
		}
166
		$this->config->setUserValue($this->userId, 'dashboard', 'background', $color);
167
	}
168
169
	public function getBackground(): ?ISimpleFile {
170
		$background = $this->config->getUserValue($this->userId, 'dashboard', 'background', 'default');
171
		if ($background === 'custom') {
172
			try {
173
				return $this->getAppDataFolder()->getFile('background.jpg');
174
			} catch (NotFoundException | NotPermittedException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
175
			}
176
		}
177
		return null;
178
	}
179
180
	/**
181
	 * @return ISimpleFolder
182
	 * @throws NotPermittedException
183
	 */
184
	private function getAppDataFolder(): ISimpleFolder {
185
		try {
186
			return $this->appData->getFolder($this->userId);
187
		} catch (NotFoundException $e) {
188
			return $this->appData->newFolder($this->userId);
189
		}
190
	}
191
}
192