1
0

JsController.php 3.7 KB

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