SvgController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2018, John Molakvoæ (skjnldsv@protonmail.com)
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author John Molakvoæ <skjnldsv@protonmail.com>
  10. * @author Julius Härtl <jus@bitgrid.net>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. * @author Thomas Citharel <nextcloud@tcit.fr>
  13. *
  14. * @license GNU AGPL version 3 or any later version
  15. *
  16. * This program is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License as
  18. * published by the Free Software Foundation, either version 3 of the
  19. * License, or (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. *
  29. */
  30. namespace OC\Core\Controller;
  31. use OC\Files\Filesystem;
  32. use OC\Template\IconsCacher;
  33. use OCP\App\AppPathNotFoundException;
  34. use OCP\App\IAppManager;
  35. use OCP\AppFramework\Controller;
  36. use OCP\AppFramework\Http;
  37. use OCP\AppFramework\Http\DataDisplayResponse;
  38. use OCP\AppFramework\Http\NotFoundResponse;
  39. use OCP\AppFramework\Utility\ITimeFactory;
  40. use OCP\IRequest;
  41. class SvgController extends Controller {
  42. /** @var string */
  43. protected $serverRoot;
  44. /** @var ITimeFactory */
  45. protected $timeFactory;
  46. /** @var IAppManager */
  47. protected $appManager;
  48. /** @var IconsCacher */
  49. private $iconsCacher;
  50. public function __construct(string $appName,
  51. IRequest $request,
  52. ITimeFactory $timeFactory,
  53. IAppManager $appManager,
  54. IconsCacher $iconsCacher) {
  55. parent::__construct($appName, $request);
  56. $this->serverRoot = \OC::$SERVERROOT;
  57. $this->timeFactory = $timeFactory;
  58. $this->appManager = $appManager;
  59. $this->iconsCacher = $iconsCacher;
  60. }
  61. /**
  62. * @PublicPage
  63. * @NoCSRFRequired
  64. * @NoSameSiteCookieRequired
  65. *
  66. * Generate svg from filename with the requested color
  67. *
  68. * @param string $folder
  69. * @param string $fileName
  70. * @param string $color
  71. * @return DataDisplayResponse|NotFoundResponse
  72. */
  73. public function getSvgFromCore(string $folder, string $fileName, string $color = 'ffffff') {
  74. $path = $this->serverRoot . "/core/img/$folder/$fileName.svg";
  75. return $this->getSvg($path, $color, $fileName);
  76. }
  77. /**
  78. * @PublicPage
  79. * @NoCSRFRequired
  80. * @NoSameSiteCookieRequired
  81. *
  82. * Generate svg from filename with the requested color
  83. *
  84. * @param string $app
  85. * @param string $fileName
  86. * @param string $color
  87. * @return DataDisplayResponse|NotFoundResponse
  88. */
  89. public function getSvgFromApp(string $app, string $fileName, string $color = 'ffffff') {
  90. try {
  91. $appPath = $this->appManager->getAppPath($app);
  92. } catch (AppPathNotFoundException $e) {
  93. return new NotFoundResponse();
  94. }
  95. $path = $appPath . "/img/$fileName.svg";
  96. return $this->getSvg($path, $color, $fileName);
  97. }
  98. /**
  99. * Generate svg from filename with the requested color
  100. *
  101. * @param string $path
  102. * @param string $color
  103. * @param string $fileName
  104. * @return DataDisplayResponse|NotFoundResponse
  105. */
  106. private function getSvg(string $path, string $color, string $fileName) {
  107. if (!Filesystem::isValidPath($path)) {
  108. return new NotFoundResponse();
  109. }
  110. if (!file_exists($path)) {
  111. return new NotFoundResponse();
  112. }
  113. $svg = file_get_contents($path);
  114. if ($svg === null) {
  115. return new NotFoundResponse();
  116. }
  117. $svg = $this->iconsCacher->colorizeSvg($svg, $color);
  118. $response = new DataDisplayResponse($svg, Http::STATUS_OK, ['Content-Type' => 'image/svg+xml']);
  119. // Set cache control
  120. $ttl = 31536000;
  121. $response->cacheFor($ttl);
  122. $response->addHeader('Content-Disposition', 'inline; filename="' . $fileName . '.svg"');
  123. $expires = new \DateTime();
  124. $expires->setTimestamp($this->timeFactory->getTime());
  125. $expires->add(new \DateInterval('PT' . $ttl . 'S'));
  126. $response->addHeader('Expires', $expires->format(\DateTime::RFC1123));
  127. $response->addHeader('Pragma', 'cache');
  128. return $response;
  129. }
  130. }