CssController.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Core\Controller;
  8. use OC\Files\AppData\Factory;
  9. use OCP\AppFramework\Controller;
  10. use OCP\AppFramework\Http;
  11. use OCP\AppFramework\Http\Attribute\FrontpageRoute;
  12. use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
  13. use OCP\AppFramework\Http\Attribute\OpenAPI;
  14. use OCP\AppFramework\Http\Attribute\PublicPage;
  15. use OCP\AppFramework\Http\FileDisplayResponse;
  16. use OCP\AppFramework\Http\NotFoundResponse;
  17. use OCP\AppFramework\Http\Response;
  18. use OCP\AppFramework\Utility\ITimeFactory;
  19. use OCP\Files\IAppData;
  20. use OCP\Files\NotFoundException;
  21. use OCP\Files\SimpleFS\ISimpleFile;
  22. use OCP\Files\SimpleFS\ISimpleFolder;
  23. use OCP\IRequest;
  24. #[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
  25. class CssController extends Controller {
  26. protected IAppData $appData;
  27. public function __construct(
  28. string $appName,
  29. IRequest $request,
  30. Factory $appDataFactory,
  31. protected ITimeFactory $timeFactory,
  32. ) {
  33. parent::__construct($appName, $request);
  34. $this->appData = $appDataFactory->get('css');
  35. }
  36. /**
  37. * @NoSameSiteCookieRequired
  38. *
  39. * @param string $fileName css filename with extension
  40. * @param string $appName css folder name
  41. * @return FileDisplayResponse|NotFoundResponse
  42. */
  43. #[PublicPage]
  44. #[NoCSRFRequired]
  45. #[FrontpageRoute(verb: 'GET', url: '/css/{appName}/{fileName}')]
  46. public function getCss(string $fileName, string $appName): Response {
  47. try {
  48. $folder = $this->appData->getFolder($appName);
  49. $gzip = false;
  50. $file = $this->getFile($folder, $fileName, $gzip);
  51. } catch (NotFoundException $e) {
  52. return new NotFoundResponse();
  53. }
  54. $response = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => 'text/css']);
  55. if ($gzip) {
  56. $response->addHeader('Content-Encoding', 'gzip');
  57. }
  58. $ttl = 31536000;
  59. $response->addHeader('Cache-Control', 'max-age=' . $ttl . ', immutable');
  60. $expires = new \DateTime();
  61. $expires->setTimestamp($this->timeFactory->getTime());
  62. $expires->add(new \DateInterval('PT' . $ttl . 'S'));
  63. $response->addHeader('Expires', $expires->format(\DateTime::RFC1123));
  64. return $response;
  65. }
  66. /**
  67. * @param ISimpleFolder $folder
  68. * @param string $fileName
  69. * @param bool $gzip is set to true if we use the gzip file
  70. * @return ISimpleFile
  71. * @throws NotFoundException
  72. */
  73. private function getFile(ISimpleFolder $folder, string $fileName, bool &$gzip): ISimpleFile {
  74. $encoding = $this->request->getHeader('Accept-Encoding');
  75. if (str_contains($encoding, 'gzip')) {
  76. try {
  77. $gzip = true;
  78. return $folder->getFile($fileName . '.gzip'); # Safari doesn't like .gz
  79. } catch (NotFoundException $e) {
  80. // continue
  81. }
  82. }
  83. $gzip = false;
  84. return $folder->getFile($fileName);
  85. }
  86. }