1
0

CssController.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, John Molakvoæ (skjnldsv@protonmail.com)
  5. *
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. *
  11. * @license GNU AGPL version 3 or any later version
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License as
  15. * published by the Free Software Foundation, either version 3 of the
  16. * License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. */
  27. namespace OC\Core\Controller;
  28. use OC\Files\AppData\Factory;
  29. use OCP\AppFramework\Controller;
  30. use OCP\AppFramework\Http;
  31. use OCP\AppFramework\Http\NotFoundResponse;
  32. use OCP\AppFramework\Http\FileDisplayResponse;
  33. use OCP\AppFramework\Http\Response;
  34. use OCP\AppFramework\Utility\ITimeFactory;
  35. use OCP\Files\IAppData;
  36. use OCP\Files\NotFoundException;
  37. use OCP\Files\SimpleFS\ISimpleFile;
  38. use OCP\Files\SimpleFS\ISimpleFolder;
  39. use OCP\IConfig;
  40. use OCP\IRequest;
  41. class CssController extends Controller {
  42. /** @var IAppData */
  43. protected $appData;
  44. /** @var ITimeFactory */
  45. protected $timeFactory;
  46. public function __construct(string $appName,
  47. IRequest $request,
  48. Factory $appDataFactory,
  49. ITimeFactory $timeFactory) {
  50. parent::__construct($appName, $request);
  51. $this->appData = $appDataFactory->get('css');
  52. $this->timeFactory = $timeFactory;
  53. }
  54. /**
  55. * @PublicPage
  56. * @NoCSRFRequired
  57. * @NoSameSiteCookieRequired
  58. *
  59. * @param string $fileName css filename with extension
  60. * @param string $appName css folder name
  61. * @return FileDisplayResponse|NotFoundResponse
  62. */
  63. public function getCss(string $fileName, string $appName): Response {
  64. try {
  65. $folder = $this->appData->getFolder($appName);
  66. $gzip = false;
  67. $file = $this->getFile($folder, $fileName, $gzip);
  68. } catch(NotFoundException $e) {
  69. return new NotFoundResponse();
  70. }
  71. $response = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => 'text/css']);
  72. if ($gzip) {
  73. $response->addHeader('Content-Encoding', 'gzip');
  74. }
  75. $ttl = 31536000;
  76. $response->addHeader('Cache-Control', 'max-age='.$ttl.', immutable');
  77. $expires = new \DateTime();
  78. $expires->setTimestamp($this->timeFactory->getTime());
  79. $expires->add(new \DateInterval('PT'.$ttl.'S'));
  80. $response->addHeader('Expires', $expires->format(\DateTime::RFC1123));
  81. $response->addHeader('Pragma', 'cache');
  82. return $response;
  83. }
  84. /**
  85. * @param ISimpleFolder $folder
  86. * @param string $fileName
  87. * @param bool $gzip is set to true if we use the gzip file
  88. * @return ISimpleFile
  89. * @throws NotFoundException
  90. */
  91. private function getFile(ISimpleFolder $folder, string $fileName, bool &$gzip): ISimpleFile {
  92. $encoding = $this->request->getHeader('Accept-Encoding');
  93. if (strpos($encoding, 'gzip') !== false) {
  94. try {
  95. $gzip = true;
  96. return $folder->getFile($fileName . '.gzip'); # Safari doesn't like .gz
  97. } catch (NotFoundException $e) {
  98. // continue
  99. }
  100. }
  101. $gzip = false;
  102. return $folder->getFile($fileName);
  103. }
  104. }