BackgroundService.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2020 Julius Härtl <jus@bitgrid.net>
  5. *
  6. * @author Jan C. Borchardt <hey@jancborchardt.net>
  7. * @author Julius Härtl <jus@bitgrid.net>
  8. * @author Christopher Ng <chrng8@gmail.com>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OCA\Theming\Service;
  27. use InvalidArgumentException;
  28. use OC\User\NoUserException;
  29. use OCA\Theming\AppInfo\Application;
  30. use OCP\Files\AppData\IAppDataFactory;
  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. class BackgroundService {
  42. // true when the background is bright and need dark icons
  43. public const THEMING_MODE_DARK = 'dark';
  44. public const DEFAULT_COLOR = '#0082c9';
  45. public const DEFAULT_ACCESSIBLE_COLOR = '#006aa3';
  46. public const SHIPPED_BACKGROUNDS = [
  47. 'anatoly-mikhaltsov-butterfly-wing-scale.jpg' => [
  48. 'attribution' => 'Butterfly wing scale (Anatoly Mikhaltsov, CC BY-SA)',
  49. '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',
  50. 'primary_color' => '#a53c17',
  51. ],
  52. 'bernie-cetonia-aurata-take-off-composition.jpg' => [
  53. 'attribution' => 'Cetonia aurata take off composition (Bernie, Public Domain)',
  54. 'attribution_url' => 'https://commons.wikimedia.org/wiki/File:Cetonia_aurata_take_off_composition_05172009.jpg',
  55. 'theming' => self::THEMING_MODE_DARK,
  56. 'primary_color' => '#56633d',
  57. ],
  58. 'dejan-krsmanovic-ribbed-red-metal.jpg' => [
  59. 'attribution' => 'Ribbed red metal (Dejan Krsmanovic, CC BY)',
  60. 'attribution_url' => 'https://www.flickr.com/photos/dejankrsmanovic/42971456774/',
  61. 'primary_color' => '#9c4236',
  62. ],
  63. 'eduardo-neves-pedra-azul.jpg' => [
  64. 'attribution' => 'Pedra azul milky way (Eduardo Neves, CC BY-SA)',
  65. 'attribution_url' => 'https://commons.wikimedia.org/wiki/File:Pedra_Azul_Milky_Way.jpg',
  66. 'primary_color' => '#4f6071',
  67. ],
  68. 'european-space-agency-barents-bloom.jpg' => [
  69. 'attribution' => 'Barents bloom (European Space Agency, CC BY-SA)',
  70. 'attribution_url' => 'https://www.esa.int/ESA_Multimedia/Images/2016/08/Barents_bloom',
  71. 'primary_color' => '#396475',
  72. ],
  73. 'hannes-fritz-flippity-floppity.jpg' => [
  74. 'attribution' => 'Flippity floppity (Hannes Fritz, CC BY-SA)',
  75. 'attribution_url' => 'http://hannes.photos/flippity-floppity',
  76. 'primary_color' => '#98415a',
  77. ],
  78. 'hannes-fritz-roulette.jpg' => [
  79. 'attribution' => 'Roulette (Hannes Fritz, CC BY-SA)',
  80. 'attribution_url' => 'http://hannes.photos/roulette',
  81. 'primary_color' => '#845334',
  82. ],
  83. 'hannes-fritz-sea-spray.jpg' => [
  84. 'attribution' => 'Sea spray (Hannes Fritz, CC BY-SA)',
  85. 'attribution_url' => 'http://hannes.photos/sea-spray',
  86. 'primary_color' => '#4f6071',
  87. ],
  88. 'kamil-porembinski-clouds.jpg' => [
  89. 'attribution' => 'Clouds (Kamil Porembiński, CC BY-SA)',
  90. 'attribution_url' => 'https://www.flickr.com/photos/paszczak000/8715851521/',
  91. 'primary_color' => self::DEFAULT_COLOR,
  92. ],
  93. 'bernard-spragg-new-zealand-fern.jpg' => [
  94. 'attribution' => 'New zealand fern (Bernard Spragg, CC0)',
  95. 'attribution_url' => 'https://commons.wikimedia.org/wiki/File:NZ_Fern.(Blechnum_chambersii)_(11263534936).jpg',
  96. 'primary_color' => '#316b26',
  97. ],
  98. 'rawpixel-pink-tapioca-bubbles.jpg' => [
  99. 'attribution' => 'Pink tapioca bubbles (Rawpixel, CC BY)',
  100. 'attribution_url' => 'https://www.flickr.com/photos/byrawpixel/27665140298/in/photostream/',
  101. 'theming' => self::THEMING_MODE_DARK,
  102. 'primary_color' => '#7b4e7e',
  103. ],
  104. 'nasa-waxing-crescent-moon.jpg' => [
  105. 'attribution' => 'Waxing crescent moon (NASA, Public Domain)',
  106. 'attribution_url' => 'https://www.nasa.gov/image-feature/a-waxing-crescent-moon',
  107. 'primary_color' => '#005ac1',
  108. ],
  109. 'tommy-chau-already.jpg' => [
  110. 'attribution' => 'Cityscape (Tommy Chau, CC BY)',
  111. 'attribution_url' => 'https://www.flickr.com/photos/90975693@N05/16910999368',
  112. 'primary_color' => '#6a2af4',
  113. ],
  114. 'tommy-chau-lion-rock-hill.jpg' => [
  115. 'attribution' => 'Lion rock hill (Tommy Chau, CC BY)',
  116. 'attribution_url' => 'https://www.flickr.com/photos/90975693@N05/17136440246',
  117. 'theming' => self::THEMING_MODE_DARK,
  118. 'primary_color' => '#7f4f70',
  119. ],
  120. 'lali-masriera-yellow-bricks.jpg' => [
  121. 'attribution' => 'Yellow bricks (Lali Masriera, CC BY)',
  122. 'attribution_url' => 'https://www.flickr.com/photos/visualpanic/3982464447',
  123. 'theming' => self::THEMING_MODE_DARK,
  124. 'primary_color' => '#7f5700',
  125. ],
  126. ];
  127. private IRootFolder $rootFolder;
  128. private IAppData $appData;
  129. private IConfig $config;
  130. private string $userId;
  131. private IAppDataFactory $appDataFactory;
  132. public function __construct(IRootFolder $rootFolder,
  133. IAppData $appData,
  134. IConfig $config,
  135. ?string $userId,
  136. IAppDataFactory $appDataFactory) {
  137. if ($userId === null) {
  138. return;
  139. }
  140. $this->rootFolder = $rootFolder;
  141. $this->config = $config;
  142. $this->userId = $userId;
  143. $this->appData = $appData;
  144. $this->appDataFactory = $appDataFactory;
  145. }
  146. public function setDefaultBackground(): void {
  147. $this->config->deleteUserValue($this->userId, Application::APP_ID, 'background');
  148. }
  149. /**
  150. * @param $path
  151. * @throws NotFoundException
  152. * @throws NotPermittedException
  153. * @throws LockedException
  154. * @throws PreConditionNotMetException
  155. * @throws NoUserException
  156. */
  157. public function setFileBackground($path): void {
  158. $this->config->setUserValue($this->userId, Application::APP_ID, 'background', 'custom');
  159. $userFolder = $this->rootFolder->getUserFolder($this->userId);
  160. /** @var File $file */
  161. $file = $userFolder->get($path);
  162. $image = new \OCP\Image();
  163. if ($image->loadFromFileHandle($file->fopen('r')) === false) {
  164. throw new InvalidArgumentException('Invalid image file');
  165. }
  166. $this->getAppDataFolder()->newFile('background.jpg', $file->fopen('r'));
  167. }
  168. public function setShippedBackground($fileName): void {
  169. if (!array_key_exists($fileName, self::SHIPPED_BACKGROUNDS)) {
  170. throw new InvalidArgumentException('The given file name is invalid');
  171. }
  172. $this->config->setUserValue($this->userId, Application::APP_ID, 'background', $fileName);
  173. }
  174. public function setColorBackground(string $color): void {
  175. if (!preg_match('/^#([0-9a-f]{3}|[0-9a-f]{6})$/i', $color)) {
  176. throw new InvalidArgumentException('The given color is invalid');
  177. }
  178. $this->config->setUserValue($this->userId, Application::APP_ID, 'background', $color);
  179. }
  180. public function getBackground(): ?ISimpleFile {
  181. $background = $this->config->getUserValue($this->userId, Application::APP_ID, 'background', 'default');
  182. if ($background === 'custom') {
  183. try {
  184. return $this->getAppDataFolder()->getFile('background.jpg');
  185. } catch (NotFoundException | NotPermittedException $e) {
  186. try {
  187. // Fallback can be removed in 26
  188. $dashboardFolder = $this->appDataFactory->get('dashboard');
  189. return $dashboardFolder->getFolder($this->userId)->getFile('background.jpg');
  190. } catch (\Throwable $t) {}
  191. }
  192. }
  193. return null;
  194. }
  195. /**
  196. * Storing the data in appdata/theming/users/USERID
  197. *
  198. * @return ISimpleFolder
  199. * @throws NotPermittedException
  200. */
  201. private function getAppDataFolder(): ISimpleFolder {
  202. try {
  203. $rootFolder = $this->appData->getFolder('users');
  204. } catch (NotFoundException $e) {
  205. $rootFolder = $this->appData->newFolder('users');
  206. }
  207. try {
  208. return $rootFolder->getFolder($this->userId);
  209. } catch (NotFoundException $e) {
  210. return $rootFolder->newFolder($this->userId);
  211. }
  212. }
  213. }