You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IconController.php 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. try {
  118. $iconFile = $this->imageManager->getCachedImage('favIcon-' . $app);
  119. } catch (NotFoundException $exception) {
  120. $icon = $this->iconBuilder->getFavicon($app);
  121. if ($icon === false || $icon === '') {
  122. return new NotFoundResponse();
  123. }
  124. $iconFile = $this->imageManager->setCachedImage('favIcon-' . $app, $icon);
  125. }
  126. $response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/x-icon']);
  127. }
  128. if ($response === null) {
  129. $fallbackLogo = \OC::$SERVERROOT . '/core/img/favicon.png';
  130. $response = new DataDisplayResponse($this->fileAccessHelper->file_get_contents($fallbackLogo), Http::STATUS_OK, ['Content-Type' => 'image/x-icon']);
  131. }
  132. $response->cacheFor(86400);
  133. return $response;
  134. }
  135. /**
  136. * Return a 512x512 icon for touch devices
  137. *
  138. * @PublicPage
  139. * @NoCSRFRequired
  140. *
  141. * @param $app string app name
  142. * @return DataDisplayResponse|FileDisplayResponse|NotFoundResponse
  143. * @throws \Exception
  144. */
  145. public function getTouchIcon(string $app = 'core'): Response {
  146. $response = null;
  147. try {
  148. $iconFile = $this->imageManager->getImage('favicon');
  149. $response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/x-icon']);
  150. } catch (NotFoundException $e) {
  151. }
  152. if ($this->imageManager->shouldReplaceIcons()) {
  153. try {
  154. $iconFile = $this->imageManager->getCachedImage('touchIcon-' . $app);
  155. } catch (NotFoundException $exception) {
  156. $icon = $this->iconBuilder->getTouchIcon($app);
  157. if ($icon === false || $icon === '') {
  158. return new NotFoundResponse();
  159. }
  160. $iconFile = $this->imageManager->setCachedImage('touchIcon-' . $app, $icon);
  161. }
  162. $response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/png']);
  163. }
  164. if ($response === null) {
  165. $fallbackLogo = \OC::$SERVERROOT . '/core/img/favicon-touch.png';
  166. $response = new DataDisplayResponse($this->fileAccessHelper->file_get_contents($fallbackLogo), Http::STATUS_OK, ['Content-Type' => 'image/png']);
  167. }
  168. $response->cacheFor(86400);
  169. return $response;
  170. }
  171. }