1
0

IconController.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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\App\IAppManager;
  35. use OCP\AppFramework\Controller;
  36. use OCP\AppFramework\Http;
  37. use OCP\AppFramework\Http\DataDisplayResponse;
  38. use OCP\AppFramework\Http\FileDisplayResponse;
  39. use OCP\AppFramework\Http\NotFoundResponse;
  40. use OCP\AppFramework\Http\Response;
  41. use OCP\Files\NotFoundException;
  42. use OCP\IRequest;
  43. class IconController extends Controller {
  44. /** @var ThemingDefaults */
  45. private $themingDefaults;
  46. /** @var IconBuilder */
  47. private $iconBuilder;
  48. /** @var ImageManager */
  49. private $imageManager;
  50. /** @var FileAccessHelper */
  51. private $fileAccessHelper;
  52. /** @var IAppManager */
  53. private $appManager;
  54. public function __construct(
  55. $appName,
  56. IRequest $request,
  57. ThemingDefaults $themingDefaults,
  58. IconBuilder $iconBuilder,
  59. ImageManager $imageManager,
  60. FileAccessHelper $fileAccessHelper,
  61. IAppManager $appManager
  62. ) {
  63. parent::__construct($appName, $request);
  64. $this->themingDefaults = $themingDefaults;
  65. $this->iconBuilder = $iconBuilder;
  66. $this->imageManager = $imageManager;
  67. $this->fileAccessHelper = $fileAccessHelper;
  68. $this->appManager = $appManager;
  69. }
  70. /**
  71. * @PublicPage
  72. * @NoCSRFRequired
  73. *
  74. * Get a themed icon
  75. *
  76. * @param string $app ID of the app
  77. * @param string $image image file name (svg required)
  78. * @return FileDisplayResponse<Http::STATUS_OK, array{Content-Type: 'image/svg+xml'}>|NotFoundResponse<Http::STATUS_NOT_FOUND, array{}>
  79. * @throws \Exception
  80. *
  81. * 200: Themed icon returned
  82. * 404: Themed icon not found
  83. */
  84. public function getThemedIcon(string $app, string $image): Response {
  85. if ($app !== 'core' && !$this->appManager->isEnabledForUser($app)) {
  86. $app = 'core';
  87. $image = 'favicon.png';
  88. }
  89. $color = $this->themingDefaults->getColorPrimary();
  90. try {
  91. $iconFileName = $this->imageManager->getCachedImage('icon-' . $app . '-' . $color . str_replace('/', '_', $image));
  92. } catch (NotFoundException $exception) {
  93. $icon = $this->iconBuilder->colorSvg($app, $image);
  94. if ($icon === false || $icon === '') {
  95. return new NotFoundResponse();
  96. }
  97. $iconFileName = $this->imageManager->setCachedImage('icon-' . $app . '-' . $color . str_replace('/', '_', $image), $icon);
  98. }
  99. $response = new FileDisplayResponse($iconFileName, Http::STATUS_OK, ['Content-Type' => 'image/svg+xml']);
  100. $response->cacheFor(86400, false, true);
  101. return $response;
  102. }
  103. /**
  104. * Return a 32x32 favicon as png
  105. *
  106. * @PublicPage
  107. * @NoCSRFRequired
  108. *
  109. * @param string $app ID of the app
  110. * @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{}>
  111. * @throws \Exception
  112. *
  113. * 200: Favicon returned
  114. * 404: Favicon not found
  115. */
  116. public function getFavicon(string $app = 'core'): Response {
  117. if ($app !== 'core' && !$this->appManager->isEnabledForUser($app)) {
  118. $app = 'core';
  119. }
  120. $response = null;
  121. $iconFile = null;
  122. try {
  123. $iconFile = $this->imageManager->getImage('favicon', false);
  124. $response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/x-icon']);
  125. } catch (NotFoundException $e) {
  126. }
  127. if ($iconFile === null && $this->imageManager->shouldReplaceIcons()) {
  128. $color = $this->themingDefaults->getColorPrimary();
  129. try {
  130. $iconFile = $this->imageManager->getCachedImage('favIcon-' . $app . $color);
  131. } catch (NotFoundException $exception) {
  132. $icon = $this->iconBuilder->getFavicon($app);
  133. if ($icon === false || $icon === '') {
  134. return new NotFoundResponse();
  135. }
  136. $iconFile = $this->imageManager->setCachedImage('favIcon-' . $app . $color, $icon);
  137. }
  138. $response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/x-icon']);
  139. }
  140. if ($response === null) {
  141. $fallbackLogo = \OC::$SERVERROOT . '/core/img/favicon.png';
  142. $response = new DataDisplayResponse($this->fileAccessHelper->file_get_contents($fallbackLogo), Http::STATUS_OK, ['Content-Type' => 'image/x-icon']);
  143. }
  144. $response->cacheFor(86400);
  145. return $response;
  146. }
  147. /**
  148. * Return a 512x512 icon for touch devices
  149. *
  150. * @PublicPage
  151. * @NoCSRFRequired
  152. *
  153. * @param string $app ID of the app
  154. * @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{}>
  155. * @throws \Exception
  156. *
  157. * 200: Touch icon returned
  158. * 404: Touch icon not found
  159. */
  160. public function getTouchIcon(string $app = 'core'): Response {
  161. if ($app !== 'core' && !$this->appManager->isEnabledForUser($app)) {
  162. $app = 'core';
  163. }
  164. $response = null;
  165. try {
  166. $iconFile = $this->imageManager->getImage('favicon');
  167. $response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/x-icon']);
  168. } catch (NotFoundException $e) {
  169. }
  170. if ($this->imageManager->shouldReplaceIcons()) {
  171. $color = $this->themingDefaults->getColorPrimary();
  172. try {
  173. $iconFile = $this->imageManager->getCachedImage('touchIcon-' . $app . $color);
  174. } catch (NotFoundException $exception) {
  175. $icon = $this->iconBuilder->getTouchIcon($app);
  176. if ($icon === false || $icon === '') {
  177. return new NotFoundResponse();
  178. }
  179. $iconFile = $this->imageManager->setCachedImage('touchIcon-' . $app . $color, $icon);
  180. }
  181. $response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/png']);
  182. }
  183. if ($response === null) {
  184. $fallbackLogo = \OC::$SERVERROOT . '/core/img/favicon-touch.png';
  185. $response = new DataDisplayResponse($this->fileAccessHelper->file_get_contents($fallbackLogo), Http::STATUS_OK, ['Content-Type' => 'image/png']);
  186. }
  187. $response->cacheFor(86400);
  188. return $response;
  189. }
  190. }