ImageManager.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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\IConfig;
  27. use OCP\Files\IAppData;
  28. use OCP\Files\NotFoundException;
  29. use OCP\Files\NotPermittedException;
  30. use OCP\IURLGenerator;
  31. /**
  32. * @property IURLGenerator urlGenerator
  33. */
  34. class ImageManager {
  35. /** @var IConfig */
  36. private $config;
  37. /** @var IAppData */
  38. private $appData;
  39. /** @var array */
  40. private $supportedImageKeys = ['background', 'logo', 'logoheader', 'favicon'];
  41. /**
  42. * ImageManager constructor.
  43. *
  44. * @param IConfig $config
  45. * @param IAppData $appData
  46. * @param IURLGenerator $urlGenerator
  47. */
  48. public function __construct(IConfig $config,
  49. IAppData $appData,
  50. IURLGenerator $urlGenerator
  51. ) {
  52. $this->config = $config;
  53. $this->appData = $appData;
  54. $this->urlGenerator = $urlGenerator;
  55. }
  56. public function getImageUrl(string $key): string {
  57. $cacheBusterCounter = $this->config->getAppValue('theming', 'cachebuster', '0');
  58. try {
  59. $this->getImage($key);
  60. return $this->urlGenerator->linkToRoute('theming.Theming.getImage', [ 'key' => $key ]) . '?v=' . $cacheBusterCounter;
  61. } catch (NotFoundException $e) {
  62. }
  63. switch ($key) {
  64. case 'logo':
  65. case 'logoheader':
  66. case 'favicon':
  67. return $this->urlGenerator->imagePath('core', 'logo.png') . '?v=' . $cacheBusterCounter;
  68. case 'background':
  69. return $this->urlGenerator->imagePath('core', 'background.png') . '?v=' . $cacheBusterCounter;
  70. }
  71. }
  72. public function getImageUrlAbsolute(string $key): string {
  73. return $this->urlGenerator->getAbsoluteURL($this->getImageUrl($key));
  74. }
  75. /**
  76. * @param $key
  77. * @return ISimpleFile
  78. * @throws NotFoundException
  79. */
  80. public function getImage(string $key): ISimpleFile {
  81. $logo = $this->config->getAppValue('theming', $key . 'Mime', false);
  82. if ($logo === false) {
  83. throw new NotFoundException();
  84. }
  85. $folder = $this->appData->getFolder('images');
  86. return $folder->getFile($key);
  87. }
  88. public function getCustomImages(): array {
  89. $images = [];
  90. foreach ($this->supportedImageKeys as $key) {
  91. $images[$key] = [
  92. 'mime' => $this->config->getAppValue('theming', $key . 'Mime', ''),
  93. 'url' => $this->getImageUrl($key),
  94. ];
  95. }
  96. return $images;
  97. }
  98. /**
  99. * Get folder for current theming files
  100. *
  101. * @return ISimpleFolder
  102. * @throws NotPermittedException
  103. */
  104. public function getCacheFolder(): ISimpleFolder {
  105. $cacheBusterValue = $this->config->getAppValue('theming', 'cachebuster', '0');
  106. try {
  107. $folder = $this->appData->getFolder($cacheBusterValue);
  108. } catch (NotFoundException $e) {
  109. $folder = $this->appData->newFolder($cacheBusterValue);
  110. $this->cleanup();
  111. }
  112. return $folder;
  113. }
  114. /**
  115. * Get a file from AppData
  116. *
  117. * @param string $filename
  118. * @throws NotFoundException
  119. * @return \OCP\Files\SimpleFS\ISimpleFile
  120. * @throws NotPermittedException
  121. */
  122. public function getCachedImage(string $filename): ISimpleFile {
  123. $currentFolder = $this->getCacheFolder();
  124. return $currentFolder->getFile($filename);
  125. }
  126. /**
  127. * Store a file for theming in AppData
  128. *
  129. * @param string $filename
  130. * @param string $data
  131. * @return \OCP\Files\SimpleFS\ISimpleFile
  132. * @throws NotFoundException
  133. * @throws NotPermittedException
  134. */
  135. public function setCachedImage(string $filename, string $data): ISimpleFile {
  136. $currentFolder = $this->getCacheFolder();
  137. if ($currentFolder->fileExists($filename)) {
  138. $file = $currentFolder->getFile($filename);
  139. } else {
  140. $file = $currentFolder->newFile($filename);
  141. }
  142. $file->putContent($data);
  143. return $file;
  144. }
  145. public function delete(string $key) {
  146. try {
  147. $file = $this->appData->getFolder('images')->getFile($key);
  148. $file->delete();
  149. } catch (NotFoundException $e) {
  150. } catch (NotPermittedException $e) {
  151. }
  152. }
  153. /**
  154. * remove cached files that are not required any longer
  155. *
  156. * @throws NotPermittedException
  157. * @throws NotFoundException
  158. */
  159. public function cleanup() {
  160. $currentFolder = $this->getCacheFolder();
  161. $folders = $this->appData->getDirectoryListing();
  162. foreach ($folders as $folder) {
  163. if ($folder->getName() !== 'images' && $folder->getName() !== $currentFolder->getName()) {
  164. $folder->delete();
  165. }
  166. }
  167. }
  168. }