IconController.php 5.7 KB

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