CssController.php 2.7 KB

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