IconController.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Julius Haertl <jus@bitgrid.net>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Julius Haertl <jus@bitgrid.net>
  8. * @author Julius Härtl <jus@bitgrid.net>
  9. * @author Michael Weimann <mail@michael-weimann.eu>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Kate Döen <kate.doeen@nextcloud.com>
  12. *
  13. * @license GNU AGPL version 3 or any later version
  14. *
  15. * This program is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License as
  17. * published by the Free Software Foundation, either version 3 of the
  18. * License, or (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. *
  28. */
  29. namespace OCA\Theming\Controller;
  30. use OC\IntegrityCheck\Helpers\FileAccessHelper;
  31. use OCA\Theming\IconBuilder;
  32. use OCA\Theming\ImageManager;
  33. use OCA\Theming\ThemingDefaults;
  34. use OCP\AppFramework\Controller;
  35. use OCP\AppFramework\Http;
  36. use OCP\AppFramework\Http\DataDisplayResponse;
  37. use OCP\AppFramework\Http\FileDisplayResponse;
  38. use OCP\AppFramework\Http\NotFoundResponse;
  39. use OCP\AppFramework\Http\Response;
  40. use OCP\Files\NotFoundException;
  41. use OCP\IRequest;
  42. class IconController extends Controller {
  43. /** @var ThemingDefaults */
  44. private $themingDefaults;
  45. /** @var IconBuilder */
  46. private $iconBuilder;
  47. /** @var ImageManager */
  48. private $imageManager;
  49. /** @var FileAccessHelper */
  50. private $fileAccessHelper;
  51. /**
  52. * IconController constructor.
  53. *
  54. * @param string $appName
  55. * @param IRequest $request
  56. * @param ThemingDefaults $themingDefaults
  57. * @param IconBuilder $iconBuilder
  58. * @param ImageManager $imageManager
  59. * @param FileAccessHelper $fileAccessHelper
  60. */
  61. public function __construct(
  62. $appName,
  63. IRequest $request,
  64. ThemingDefaults $themingDefaults,
  65. IconBuilder $iconBuilder,
  66. ImageManager $imageManager,
  67. FileAccessHelper $fileAccessHelper
  68. ) {
  69. parent::__construct($appName, $request);
  70. $this->themingDefaults = $themingDefaults;
  71. $this->iconBuilder = $iconBuilder;
  72. $this->imageManager = $imageManager;
  73. $this->fileAccessHelper = $fileAccessHelper;
  74. }
  75. /**
  76. * @PublicPage
  77. * @NoCSRFRequired
  78. *
  79. * Get a themed icon
  80. *
  81. * @param string $app ID of the app
  82. * @param string $image image file name (svg required)
  83. * @return FileDisplayResponse<Http::STATUS_OK, array{Content-Type: 'image/svg+xml'}>|NotFoundResponse<Http::STATUS_NOT_FOUND, array{}>
  84. * @throws \Exception
  85. *
  86. * 200: Themed icon returned
  87. * 404: Themed icon not found
  88. */
  89. public function getThemedIcon(string $app, string $image): Response {
  90. $color = $this->themingDefaults->getColorPrimary();
  91. try {
  92. $iconFileName = $this->imageManager->getCachedImage('icon-' . $app . '-' . $color . str_replace('/', '_', $image));
  93. } catch (NotFoundException $exception) {
  94. $icon = $this->iconBuilder->colorSvg($app, $image);
  95. if ($icon === false || $icon === '') {
  96. return new NotFoundResponse();
  97. }
  98. $iconFileName = $this->imageManager->setCachedImage('icon-' . $app . '-' . $color . str_replace('/', '_', $image), $icon);
  99. }
  100. $response = new FileDisplayResponse($iconFileName, Http::STATUS_OK, ['Content-Type' => 'image/svg+xml']);
  101. $response->cacheFor(86400, false, true);
  102. return $response;
  103. }
  104. /**
  105. * Return a 32x32 favicon as png
  106. *
  107. * @PublicPage
  108. * @NoCSRFRequired
  109. *
  110. * @param string $app ID of the app
  111. * @return DataDisplayResponse<Http::STATUS_OK, array{Content-Type: 'image/x-icon'}>|FileDisplayResponse<Http::STATUS_OK, array{Content-Type: 'image/x-icon'}>|NotFoundResponse<Http::STATUS_NOT_FOUND, array{}>
  112. * @throws \Exception
  113. *
  114. * 200: Favicon returned
  115. * 404: Favicon not found
  116. */
  117. public function getFavicon(string $app = 'core'): Response {
  118. $response = null;
  119. $iconFile = null;
  120. try {
  121. $iconFile = $this->imageManager->getImage('favicon', false);
  122. $response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/x-icon']);
  123. } catch (NotFoundException $e) {
  124. }
  125. if ($iconFile === null && $this->imageManager->shouldReplaceIcons()) {
  126. $color = $this->themingDefaults->getColorPrimary();
  127. try {
  128. $iconFile = $this->imageManager->getCachedImage('favIcon-' . $app . $color);
  129. } catch (NotFoundException $exception) {
  130. $icon = $this->iconBuilder->getFavicon($app);
  131. if ($icon === false || $icon === '') {
  132. return new NotFoundResponse();
  133. }
  134. $iconFile = $this->imageManager->setCachedImage('favIcon-' . $app . $color, $icon);
  135. }
  136. $response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/x-icon']);
  137. }
  138. if ($response === null) {
  139. $fallbackLogo = \OC::$SERVERROOT . '/core/img/favicon.png';
  140. $response = new DataDisplayResponse($this->fileAccessHelper->file_get_contents($fallbackLogo), Http::STATUS_OK, ['Content-Type' => 'image/x-icon']);
  141. }
  142. $response->cacheFor(86400);
  143. return $response;
  144. }
  145. /**
  146. * Return a 512x512 icon for touch devices
  147. *
  148. * @PublicPage
  149. * @NoCSRFRequired
  150. *
  151. * @param string $app ID of the app
  152. * @return DataDisplayResponse<Http::STATUS_OK, array{Content-Type: 'image/png'}>|FileDisplayResponse<Http::STATUS_OK, array{Content-Type: 'image/x-icon'|'image/png'}>|NotFoundResponse<Http::STATUS_NOT_FOUND, array{}>
  153. * @throws \Exception
  154. *
  155. * 200: Touch icon returned
  156. * 404: Touch icon not found
  157. */
  158. public function getTouchIcon(string $app = 'core'): Response {
  159. $response = null;
  160. try {
  161. $iconFile = $this->imageManager->getImage('favicon');
  162. $response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/x-icon']);
  163. } catch (NotFoundException $e) {
  164. }
  165. if ($this->imageManager->shouldReplaceIcons()) {
  166. $color = $this->themingDefaults->getColorPrimary();
  167. try {
  168. $iconFile = $this->imageManager->getCachedImage('touchIcon-' . $app . $color);
  169. } catch (NotFoundException $exception) {
  170. $icon = $this->iconBuilder->getTouchIcon($app);
  171. if ($icon === false || $icon === '') {
  172. return new NotFoundResponse();
  173. }
  174. $iconFile = $this->imageManager->setCachedImage('touchIcon-' . $app . $color, $icon);
  175. }
  176. $response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/png']);
  177. }
  178. if ($response === null) {
  179. $fallbackLogo = \OC::$SERVERROOT . '/core/img/favicon-touch.png';
  180. $response = new DataDisplayResponse($this->fileAccessHelper->file_get_contents($fallbackLogo), Http::STATUS_OK, ['Content-Type' => 'image/png']);
  181. }
  182. $response->cacheFor(86400);
  183. return $response;
  184. }
  185. }