1
0

IconController.php 5.9 KB

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