ImageManager.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Julius Härtl <jus@bitgrid.net>
  4. *
  5. * @author Julius Haertl <jus@bitgrid.net>
  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. namespace OCA\Theming;
  24. use OCP\Files\SimpleFS\ISimpleFile;
  25. use OCP\Files\SimpleFS\ISimpleFolder;
  26. use OCP\ICacheFactory;
  27. use OCP\IConfig;
  28. use OCP\Files\IAppData;
  29. use OCP\Files\NotFoundException;
  30. use OCP\Files\NotPermittedException;
  31. use OCP\ILogger;
  32. use OCP\IURLGenerator;
  33. class ImageManager {
  34. /** @var IConfig */
  35. private $config;
  36. /** @var IAppData */
  37. private $appData;
  38. /** @var IURLGenerator */
  39. private $urlGenerator;
  40. /** @var array */
  41. private $supportedImageKeys = ['background', 'logo', 'logoheader', 'favicon'];
  42. /** @var ICacheFactory */
  43. private $cacheFactory;
  44. /** @var ILogger */
  45. private $logger;
  46. /**
  47. * ImageManager constructor.
  48. *
  49. * @param IConfig $config
  50. * @param IAppData $appData
  51. * @param IURLGenerator $urlGenerator
  52. * @param ICacheFactory $cacheFactory
  53. * @param ILogger $logger
  54. */
  55. public function __construct(IConfig $config,
  56. IAppData $appData,
  57. IURLGenerator $urlGenerator,
  58. ICacheFactory $cacheFactory,
  59. ILogger $logger
  60. ) {
  61. $this->config = $config;
  62. $this->appData = $appData;
  63. $this->urlGenerator = $urlGenerator;
  64. $this->cacheFactory = $cacheFactory;
  65. $this->logger = $logger;
  66. }
  67. public function getImageUrl(string $key, bool $useSvg = true): string {
  68. $cacheBusterCounter = $this->config->getAppValue('theming', 'cachebuster', '0');
  69. try {
  70. $image = $this->getImage($key, $useSvg);
  71. return $this->urlGenerator->linkToRoute('theming.Theming.getImage', [ 'key' => $key ]) . '?v=' . $cacheBusterCounter;
  72. } catch (NotFoundException $e) {
  73. }
  74. switch ($key) {
  75. case 'logo':
  76. case 'logoheader':
  77. case 'favicon':
  78. return $this->urlGenerator->imagePath('core', 'logo/logo.png') . '?v=' . $cacheBusterCounter;
  79. case 'background':
  80. return $this->urlGenerator->imagePath('core', 'background.png') . '?v=' . $cacheBusterCounter;
  81. }
  82. }
  83. public function getImageUrlAbsolute(string $key, bool $useSvg = true): string {
  84. return $this->urlGenerator->getAbsoluteURL($this->getImageUrl($key, $useSvg));
  85. }
  86. /**
  87. * @param string $key
  88. * @param bool $useSvg
  89. * @return ISimpleFile
  90. * @throws NotFoundException
  91. * @throws NotPermittedException
  92. */
  93. public function getImage(string $key, bool $useSvg = true): ISimpleFile {
  94. $pngFile = null;
  95. $logo = $this->config->getAppValue('theming', $key . 'Mime', false);
  96. $folder = $this->appData->getFolder('images');
  97. if ($logo === false || !$folder->fileExists($key)) {
  98. throw new NotFoundException();
  99. }
  100. if (!$useSvg && $this->shouldReplaceIcons()) {
  101. if (!$folder->fileExists($key . '.png')) {
  102. try {
  103. $finalIconFile = new \Imagick();
  104. $finalIconFile->setBackgroundColor('none');
  105. $finalIconFile->readImageBlob($folder->getFile($key)->getContent());
  106. $finalIconFile->setImageFormat('png32');
  107. $pngFile = $folder->newFile($key . '.png');
  108. $pngFile->putContent($finalIconFile->getImageBlob());
  109. } catch (\ImagickException $e) {
  110. $this->logger->info('The image was requested to be no SVG file, but converting it to PNG failed: ' . $e->getMessage());
  111. $pngFile = null;
  112. }
  113. } else {
  114. $pngFile = $folder->getFile($key . '.png');
  115. }
  116. }
  117. if ($pngFile !== null) {
  118. return $pngFile;
  119. }
  120. return $folder->getFile($key);
  121. }
  122. public function getCustomImages(): array {
  123. $images = [];
  124. foreach ($this->supportedImageKeys as $key) {
  125. $images[$key] = [
  126. 'mime' => $this->config->getAppValue('theming', $key . 'Mime', ''),
  127. 'url' => $this->getImageUrl($key),
  128. ];
  129. }
  130. return $images;
  131. }
  132. /**
  133. * Get folder for current theming files
  134. *
  135. * @return ISimpleFolder
  136. * @throws NotPermittedException
  137. */
  138. public function getCacheFolder(): ISimpleFolder {
  139. $cacheBusterValue = $this->config->getAppValue('theming', 'cachebuster', '0');
  140. try {
  141. $folder = $this->appData->getFolder($cacheBusterValue);
  142. } catch (NotFoundException $e) {
  143. $folder = $this->appData->newFolder($cacheBusterValue);
  144. $this->cleanup();
  145. }
  146. return $folder;
  147. }
  148. /**
  149. * Get a file from AppData
  150. *
  151. * @param string $filename
  152. * @throws NotFoundException
  153. * @return \OCP\Files\SimpleFS\ISimpleFile
  154. * @throws NotPermittedException
  155. */
  156. public function getCachedImage(string $filename): ISimpleFile {
  157. $currentFolder = $this->getCacheFolder();
  158. return $currentFolder->getFile($filename);
  159. }
  160. /**
  161. * Store a file for theming in AppData
  162. *
  163. * @param string $filename
  164. * @param string $data
  165. * @return \OCP\Files\SimpleFS\ISimpleFile
  166. * @throws NotFoundException
  167. * @throws NotPermittedException
  168. */
  169. public function setCachedImage(string $filename, string $data): ISimpleFile {
  170. $currentFolder = $this->getCacheFolder();
  171. if ($currentFolder->fileExists($filename)) {
  172. $file = $currentFolder->getFile($filename);
  173. } else {
  174. $file = $currentFolder->newFile($filename);
  175. }
  176. $file->putContent($data);
  177. return $file;
  178. }
  179. public function delete(string $key) {
  180. /* ignore exceptions, since we don't want to fail hard if something goes wrong during cleanup */
  181. try {
  182. $file = $this->appData->getFolder('images')->getFile($key);
  183. $file->delete();
  184. } catch (NotFoundException $e) {
  185. } catch (NotPermittedException $e) {
  186. }
  187. try {
  188. $file = $this->appData->getFolder('images')->getFile($key . '.png');
  189. $file->delete();
  190. } catch (NotFoundException $e) {
  191. } catch (NotPermittedException $e) {
  192. }
  193. }
  194. /**
  195. * remove cached files that are not required any longer
  196. *
  197. * @throws NotPermittedException
  198. * @throws NotFoundException
  199. */
  200. public function cleanup() {
  201. $currentFolder = $this->getCacheFolder();
  202. $folders = $this->appData->getDirectoryListing();
  203. foreach ($folders as $folder) {
  204. if ($folder->getName() !== 'images' && $folder->getName() !== $currentFolder->getName()) {
  205. $folder->delete();
  206. }
  207. }
  208. }
  209. /**
  210. * Check if Imagemagick is enabled and if SVG is supported
  211. * otherwise we can't render custom icons
  212. *
  213. * @return bool
  214. */
  215. public function shouldReplaceIcons() {
  216. $cache = $this->cacheFactory->createDistributed('theming-' . $this->urlGenerator->getBaseUrl());
  217. if($value = $cache->get('shouldReplaceIcons')) {
  218. return (bool)$value;
  219. }
  220. $value = false;
  221. if(extension_loaded('imagick')) {
  222. if (count(\Imagick::queryFormats('SVG')) >= 1) {
  223. $value = true;
  224. }
  225. }
  226. $cache->set('shouldReplaceIcons', $value);
  227. return $value;
  228. }
  229. }