JsController.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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\OpenAPI;
  35. use OCP\AppFramework\Http\FileDisplayResponse;
  36. use OCP\AppFramework\Http\NotFoundResponse;
  37. use OCP\AppFramework\Http\Response;
  38. use OCP\AppFramework\Utility\ITimeFactory;
  39. use OCP\Files\IAppData;
  40. use OCP\Files\NotFoundException;
  41. use OCP\Files\SimpleFS\ISimpleFile;
  42. use OCP\Files\SimpleFS\ISimpleFolder;
  43. use OCP\IRequest;
  44. #[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
  45. class JsController extends Controller {
  46. protected IAppData $appData;
  47. public function __construct(
  48. string $appName,
  49. IRequest $request,
  50. Factory $appDataFactory,
  51. protected ITimeFactory $timeFactory,
  52. ) {
  53. parent::__construct($appName, $request);
  54. $this->appData = $appDataFactory->get('js');
  55. }
  56. /**
  57. * @PublicPage
  58. * @NoCSRFRequired
  59. * @NoSameSiteCookieRequired
  60. *
  61. * @param string $fileName js filename with extension
  62. * @param string $appName js folder name
  63. * @return FileDisplayResponse|NotFoundResponse
  64. */
  65. public function getJs(string $fileName, string $appName): Response {
  66. try {
  67. $folder = $this->appData->getFolder($appName);
  68. $gzip = false;
  69. $file = $this->getFile($folder, $fileName, $gzip);
  70. } catch (NotFoundException $e) {
  71. return new NotFoundResponse();
  72. }
  73. $response = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => 'application/javascript']);
  74. if ($gzip) {
  75. $response->addHeader('Content-Encoding', 'gzip');
  76. }
  77. $ttl = 31536000;
  78. $response->addHeader('Cache-Control', 'max-age='.$ttl.', immutable');
  79. $expires = new \DateTime();
  80. $expires->setTimestamp($this->timeFactory->getTime());
  81. $expires->add(new \DateInterval('PT'.$ttl.'S'));
  82. $response->addHeader('Expires', $expires->format(\DateTime::RFC1123));
  83. return $response;
  84. }
  85. /**
  86. * @param ISimpleFolder $folder
  87. * @param string $fileName
  88. * @param bool $gzip is set to true if we use the gzip file
  89. * @return ISimpleFile
  90. *
  91. * @throws NotFoundException
  92. */
  93. private function getFile(ISimpleFolder $folder, string $fileName, bool &$gzip): ISimpleFile {
  94. $encoding = $this->request->getHeader('Accept-Encoding');
  95. if (str_contains($encoding, 'gzip')) {
  96. try {
  97. $gzip = true;
  98. return $folder->getFile($fileName . '.gzip'); # Safari doesn't like .gz
  99. } catch (NotFoundException $e) {
  100. // continue
  101. }
  102. }
  103. $gzip = false;
  104. return $folder->getFile($fileName);
  105. }
  106. }