SvgController.php 4.1 KB

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