SvgController.php 4.0 KB

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