IconController.php 6.1 KB

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