JsController.php 2.7 KB

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