1
0

JsController.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2017 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 JsController 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('js');
  35. }
  36. /**
  37. * @NoSameSiteCookieRequired
  38. *
  39. * @param string $fileName js filename with extension
  40. * @param string $appName js folder name
  41. * @return FileDisplayResponse|NotFoundResponse
  42. */
  43. #[PublicPage]
  44. #[NoCSRFRequired]
  45. #[FrontpageRoute(verb: 'GET', url: '/js/{appName}/{fileName}')]
  46. public function getJs(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' => 'application/javascript']);
  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. *
  72. * @throws NotFoundException
  73. */
  74. private function getFile(ISimpleFolder $folder, string $fileName, bool &$gzip): ISimpleFile {
  75. $encoding = $this->request->getHeader('Accept-Encoding');
  76. if (str_contains($encoding, 'gzip')) {
  77. try {
  78. $gzip = true;
  79. return $folder->getFile($fileName . '.gzip'); # Safari doesn't like .gz
  80. } catch (NotFoundException $e) {
  81. // continue
  82. }
  83. }
  84. $gzip = false;
  85. return $folder->getFile($fileName);
  86. }
  87. }