SvgController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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) <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\Template\IconsCacher;
  32. use OCP\App\AppPathNotFoundException;
  33. use OCP\App\IAppManager;
  34. use OCP\AppFramework\Controller;
  35. use OCP\AppFramework\Http;
  36. use OCP\AppFramework\Http\DataDisplayResponse;
  37. use OCP\AppFramework\Http\NotFoundResponse;
  38. use OCP\AppFramework\Utility\ITimeFactory;
  39. use OCP\IRequest;
  40. class SvgController extends Controller {
  41. /** @var string */
  42. protected $serverRoot;
  43. /** @var ITimeFactory */
  44. protected $timeFactory;
  45. /** @var IAppManager */
  46. protected $appManager;
  47. /** @var IconsCacher */
  48. private $iconsCacher;
  49. public function __construct(string $appName,
  50. IRequest $request,
  51. ITimeFactory $timeFactory,
  52. IAppManager $appManager,
  53. IconsCacher $iconsCacher) {
  54. parent::__construct($appName, $request);
  55. $this->serverRoot = \OC::$SERVERROOT;
  56. $this->timeFactory = $timeFactory;
  57. $this->appManager = $appManager;
  58. $this->iconsCacher = $iconsCacher;
  59. }
  60. /**
  61. * @PublicPage
  62. * @NoCSRFRequired
  63. * @NoSameSiteCookieRequired
  64. *
  65. * Generate svg from filename with the requested color
  66. *
  67. * @param string $folder
  68. * @param string $fileName
  69. * @param string $color
  70. * @return DataDisplayResponse|NotFoundResponse
  71. */
  72. public function getSvgFromCore(string $folder, string $fileName, string $color = 'ffffff') {
  73. $path = $this->serverRoot . "/core/img/$folder/$fileName.svg";
  74. return $this->getSvg($path, $color, $fileName);
  75. }
  76. /**
  77. * @PublicPage
  78. * @NoCSRFRequired
  79. * @NoSameSiteCookieRequired
  80. *
  81. * Generate svg from filename with the requested color
  82. *
  83. * @param string $app
  84. * @param string $fileName
  85. * @param string $color
  86. * @return DataDisplayResponse|NotFoundResponse
  87. */
  88. public function getSvgFromApp(string $app, string $fileName, string $color = 'ffffff') {
  89. try {
  90. $appPath = $this->appManager->getAppPath($app);
  91. } catch (AppPathNotFoundException $e) {
  92. return new NotFoundResponse();
  93. }
  94. $path = $appPath . "/img/$fileName.svg";
  95. return $this->getSvg($path, $color, $fileName);
  96. }
  97. /**
  98. * Generate svg from filename with the requested color
  99. *
  100. * @param string $path
  101. * @param string $color
  102. * @param string $fileName
  103. * @return DataDisplayResponse|NotFoundResponse
  104. */
  105. private function getSvg(string $path, string $color, string $fileName) {
  106. if (!file_exists($path)) {
  107. return new NotFoundResponse();
  108. }
  109. $svg = file_get_contents($path);
  110. if ($svg === null) {
  111. return new NotFoundResponse();
  112. }
  113. $svg = $this->iconsCacher->colorizeSvg($svg, $color);
  114. $response = new DataDisplayResponse($svg, Http::STATUS_OK, ['Content-Type' => 'image/svg+xml']);
  115. // Set cache control
  116. $ttl = 31536000;
  117. $response->cacheFor($ttl);
  118. $response->addHeader('Content-Disposition', 'inline; filename="' . $fileName . '.svg"');
  119. $expires = new \DateTime();
  120. $expires->setTimestamp($this->timeFactory->getTime());
  121. $expires->add(new \DateInterval('PT' . $ttl . 'S'));
  122. $response->addHeader('Expires', $expires->format(\DateTime::RFC1123));
  123. $response->addHeader('Pragma', 'cache');
  124. return $response;
  125. }
  126. }