SvgController.php 4.1 KB

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