IconController.php 5.8 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. try {
  85. $iconFile = $this->imageManager->getCachedImage('icon-' . $app . '-' . str_replace('/', '_',$image));
  86. } catch (NotFoundException $exception) {
  87. $icon = $this->iconBuilder->colorSvg($app, $image);
  88. if ($icon === false || $icon === '') {
  89. return new NotFoundResponse();
  90. }
  91. $iconFile = $this->imageManager->setCachedImage('icon-' . $app . '-' . str_replace('/', '_',$image), $icon);
  92. }
  93. if ($iconFile !== false) {
  94. $response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/svg+xml']);
  95. $response->cacheFor(86400);
  96. return $response;
  97. }
  98. return new NotFoundResponse();
  99. }
  100. /**
  101. * Return a 32x32 favicon as png
  102. *
  103. * @PublicPage
  104. * @NoCSRFRequired
  105. *
  106. * @param $app string app name
  107. * @return FileDisplayResponse|DataDisplayResponse
  108. * @throws \Exception
  109. */
  110. public function getFavicon(string $app = 'core'): Response {
  111. $response = null;
  112. $iconFile = null;
  113. try {
  114. $iconFile = $this->imageManager->getImage('favicon', false);
  115. $response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/x-icon']);
  116. } catch (NotFoundException $e) {
  117. }
  118. if ($iconFile === null && $this->imageManager->shouldReplaceIcons()) {
  119. try {
  120. $iconFile = $this->imageManager->getCachedImage('favIcon-' . $app);
  121. } catch (NotFoundException $exception) {
  122. $icon = $this->iconBuilder->getFavicon($app);
  123. $iconFile = $this->imageManager->setCachedImage('favIcon-' . $app, $icon);
  124. }
  125. if ($iconFile !== false) {
  126. $response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/x-icon']);
  127. }
  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 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. try {
  155. $iconFile = $this->imageManager->getCachedImage('touchIcon-' . $app);
  156. } catch (NotFoundException $exception) {
  157. $icon = $this->iconBuilder->getTouchIcon($app);
  158. $iconFile = $this->imageManager->setCachedImage('touchIcon-' . $app, $icon);
  159. }
  160. if ($iconFile !== false) {
  161. $response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/png']);
  162. }
  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. }