BackgroundService.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 OCA\Theming\ThemingDefaults;
  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 BACKGROUND_SHIPPED = 'shipped';
  47. public const BACKGROUND_CUSTOM = 'custom';
  48. public const BACKGROUND_DEFAULT = 'default';
  49. public const BACKGROUND_DISABLED = 'disabled';
  50. public const DEFAULT_BACKGROUND_IMAGE = 'kamil-porembinski-clouds.jpg';
  51. public const SHIPPED_BACKGROUNDS = [
  52. 'hannah-maclean-soft-floral.jpg' => [
  53. 'attribution' => 'Soft floral (Hannah MacLean, CC0)',
  54. 'attribution_url' => 'https://stocksnap.io/photo/soft-floral-XOYWCCW5PA',
  55. 'theming' => self::THEMING_MODE_DARK,
  56. 'primary_color' => '#9f652f',
  57. ],
  58. 'ted-moravec-morning-fog.jpg' => [
  59. 'attribution' => 'Morning fog (Ted Moravec, Public Domain)',
  60. 'attribution_url' => 'https://flickr.com/photos/tmoravec/52392410261',
  61. 'theming' => self::THEMING_MODE_DARK,
  62. 'primary_color' => '#114c3b',
  63. ],
  64. 'stefanus-martanto-setyo-husodo-underwater-ocean.jpg' => [
  65. 'attribution' => 'Underwater ocean (Stefanus Martanto Setyo Husodo, CC0)',
  66. 'attribution_url' => 'https://stocksnap.io/photo/underwater-ocean-TJA9LBH4WS',
  67. 'primary_color' => '#04577e',
  68. ],
  69. 'zoltan-voros-rhythm-and-blues.jpg' => [
  70. 'attribution' => 'Rhythm and blues (Zoltán Vörös, CC BY)',
  71. 'attribution_url' => 'https://flickr.com/photos/v923z/51634409289/',
  72. 'primary_color' => '#1c243c',
  73. ],
  74. 'anatoly-mikhaltsov-butterfly-wing-scale.jpg' => [
  75. 'attribution' => 'Butterfly wing scale (Anatoly Mikhaltsov, CC BY-SA)',
  76. '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',
  77. 'primary_color' => '#a53c17',
  78. ],
  79. 'bernie-cetonia-aurata-take-off-composition.jpg' => [
  80. 'attribution' => 'Cetonia aurata take off composition (Bernie, Public Domain)',
  81. 'attribution_url' => 'https://commons.wikimedia.org/wiki/File:Cetonia_aurata_take_off_composition_05172009.jpg',
  82. 'theming' => self::THEMING_MODE_DARK,
  83. 'primary_color' => '#56633d',
  84. ],
  85. 'dejan-krsmanovic-ribbed-red-metal.jpg' => [
  86. 'attribution' => 'Ribbed red metal (Dejan Krsmanovic, CC BY)',
  87. 'attribution_url' => 'https://www.flickr.com/photos/dejankrsmanovic/42971456774/',
  88. 'primary_color' => '#9c4236',
  89. ],
  90. 'eduardo-neves-pedra-azul.jpg' => [
  91. 'attribution' => 'Pedra azul milky way (Eduardo Neves, CC BY-SA)',
  92. 'attribution_url' => 'https://commons.wikimedia.org/wiki/File:Pedra_Azul_Milky_Way.jpg',
  93. 'primary_color' => '#4f6071',
  94. ],
  95. 'european-space-agency-barents-bloom.jpg' => [
  96. 'attribution' => 'Barents bloom (European Space Agency, CC BY-SA)',
  97. 'attribution_url' => 'https://www.esa.int/ESA_Multimedia/Images/2016/08/Barents_bloom',
  98. 'primary_color' => '#396475',
  99. ],
  100. 'hannes-fritz-flippity-floppity.jpg' => [
  101. 'attribution' => 'Flippity floppity (Hannes Fritz, CC BY-SA)',
  102. 'attribution_url' => 'http://hannes.photos/flippity-floppity',
  103. 'primary_color' => '#98415a',
  104. ],
  105. 'hannes-fritz-roulette.jpg' => [
  106. 'attribution' => 'Roulette (Hannes Fritz, CC BY-SA)',
  107. 'attribution_url' => 'http://hannes.photos/roulette',
  108. 'primary_color' => '#845334',
  109. ],
  110. 'hannes-fritz-sea-spray.jpg' => [
  111. 'attribution' => 'Sea spray (Hannes Fritz, CC BY-SA)',
  112. 'attribution_url' => 'http://hannes.photos/sea-spray',
  113. 'primary_color' => '#4f6071',
  114. ],
  115. 'kamil-porembinski-clouds.jpg' => [
  116. 'attribution' => 'Clouds (Kamil Porembiński, CC BY-SA)',
  117. 'attribution_url' => 'https://www.flickr.com/photos/paszczak000/8715851521/',
  118. 'primary_color' => self::DEFAULT_COLOR,
  119. ],
  120. 'bernard-spragg-new-zealand-fern.jpg' => [
  121. 'attribution' => 'New zealand fern (Bernard Spragg, CC0)',
  122. 'attribution_url' => 'https://commons.wikimedia.org/wiki/File:NZ_Fern.(Blechnum_chambersii)_(11263534936).jpg',
  123. 'primary_color' => '#316b26',
  124. ],
  125. 'rawpixel-pink-tapioca-bubbles.jpg' => [
  126. 'attribution' => 'Pink tapioca bubbles (Rawpixel, CC BY)',
  127. 'attribution_url' => 'https://www.flickr.com/photos/byrawpixel/27665140298/in/photostream/',
  128. 'theming' => self::THEMING_MODE_DARK,
  129. 'primary_color' => '#7b4e7e',
  130. ],
  131. 'nasa-waxing-crescent-moon.jpg' => [
  132. 'attribution' => 'Waxing crescent moon (NASA, Public Domain)',
  133. 'attribution_url' => 'https://www.nasa.gov/image-feature/a-waxing-crescent-moon',
  134. 'primary_color' => '#005ac1',
  135. ],
  136. 'tommy-chau-already.jpg' => [
  137. 'attribution' => 'Cityscape (Tommy Chau, CC BY)',
  138. 'attribution_url' => 'https://www.flickr.com/photos/90975693@N05/16910999368',
  139. 'primary_color' => '#6a2af4',
  140. ],
  141. 'tommy-chau-lion-rock-hill.jpg' => [
  142. 'attribution' => 'Lion rock hill (Tommy Chau, CC BY)',
  143. 'attribution_url' => 'https://www.flickr.com/photos/90975693@N05/17136440246',
  144. 'theming' => self::THEMING_MODE_DARK,
  145. 'primary_color' => '#7f4f70',
  146. ],
  147. 'lali-masriera-yellow-bricks.jpg' => [
  148. 'attribution' => 'Yellow bricks (Lali Masriera, CC BY)',
  149. 'attribution_url' => 'https://www.flickr.com/photos/visualpanic/3982464447',
  150. 'theming' => self::THEMING_MODE_DARK,
  151. 'primary_color' => '#7f5700',
  152. ],
  153. ];
  154. private IRootFolder $rootFolder;
  155. private IAppData $appData;
  156. private IConfig $config;
  157. private string $userId;
  158. private ThemingDefaults $themingDefaults;
  159. public function __construct(IRootFolder $rootFolder,
  160. IAppData $appData,
  161. IConfig $config,
  162. ?string $userId,
  163. ThemingDefaults $themingDefaults) {
  164. if ($userId === null) {
  165. return;
  166. }
  167. $this->rootFolder = $rootFolder;
  168. $this->config = $config;
  169. $this->userId = $userId;
  170. $this->appData = $appData;
  171. $this->themingDefaults = $themingDefaults;
  172. }
  173. public function setDefaultBackground(): void {
  174. $this->config->deleteUserValue($this->userId, Application::APP_ID, 'background_image');
  175. $this->config->deleteUserValue($this->userId, Application::APP_ID, 'background_color');
  176. }
  177. /**
  178. * @param $path
  179. * @throws NotFoundException
  180. * @throws NotPermittedException
  181. * @throws LockedException
  182. * @throws PreConditionNotMetException
  183. * @throws NoUserException
  184. */
  185. public function setFileBackground($path): void {
  186. $this->config->setUserValue($this->userId, Application::APP_ID, 'background_image', self::BACKGROUND_CUSTOM);
  187. $userFolder = $this->rootFolder->getUserFolder($this->userId);
  188. /** @var File $file */
  189. $file = $userFolder->get($path);
  190. $image = new \OCP\Image();
  191. if ($image->loadFromFileHandle($file->fopen('r')) === false) {
  192. throw new InvalidArgumentException('Invalid image file');
  193. }
  194. $this->getAppDataFolder()->newFile('background.jpg', $file->fopen('r'));
  195. }
  196. public function setShippedBackground($fileName): void {
  197. if (!array_key_exists($fileName, self::SHIPPED_BACKGROUNDS)) {
  198. throw new InvalidArgumentException('The given file name is invalid');
  199. }
  200. $this->config->setUserValue($this->userId, Application::APP_ID, 'background_image', $fileName);
  201. $this->setColorBackground(self::SHIPPED_BACKGROUNDS[$fileName]['primary_color']);
  202. }
  203. public function setColorBackground(string $color): void {
  204. if (!preg_match('/^#([0-9a-f]{3}|[0-9a-f]{6})$/i', $color)) {
  205. throw new InvalidArgumentException('The given color is invalid');
  206. }
  207. $this->config->setUserValue($this->userId, Application::APP_ID, 'background_color', $color);
  208. }
  209. public function deleteBackgroundImage(): void {
  210. $this->config->setUserValue($this->userId, Application::APP_ID, 'background_image', self::BACKGROUND_DISABLED);
  211. }
  212. public function getBackground(): ?ISimpleFile {
  213. $background = $this->config->getUserValue($this->userId, Application::APP_ID, 'background_image', self::BACKGROUND_DEFAULT);
  214. if ($background === self::BACKGROUND_CUSTOM) {
  215. try {
  216. return $this->getAppDataFolder()->getFile('background.jpg');
  217. } catch (NotFoundException | NotPermittedException $e) {
  218. return null;
  219. }
  220. }
  221. return null;
  222. }
  223. /**
  224. * Storing the data in appdata/theming/users/USERID
  225. *
  226. * @return ISimpleFolder
  227. * @throws NotPermittedException
  228. */
  229. private function getAppDataFolder(): ISimpleFolder {
  230. try {
  231. $rootFolder = $this->appData->getFolder('users');
  232. } catch (NotFoundException $e) {
  233. $rootFolder = $this->appData->newFolder('users');
  234. }
  235. try {
  236. return $rootFolder->getFolder($this->userId);
  237. } catch (NotFoundException $e) {
  238. return $rootFolder->newFolder($this->userId);
  239. }
  240. }
  241. }