You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JsController.php 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. *
  13. * @license GNU AGPL version 3 or any later version
  14. *
  15. * This program is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License as
  17. * published by the Free Software Foundation, either version 3 of the
  18. * License, or (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. *
  28. */
  29. namespace OC\Core\Controller;
  30. use OC\Files\AppData\Factory;
  31. use OCP\AppFramework\Controller;
  32. use OCP\AppFramework\Http;
  33. use OCP\AppFramework\Http\FileDisplayResponse;
  34. use OCP\AppFramework\Http\NotFoundResponse;
  35. use OCP\AppFramework\Http\Response;
  36. use OCP\AppFramework\Utility\ITimeFactory;
  37. use OCP\Files\IAppData;
  38. use OCP\Files\NotFoundException;
  39. use OCP\Files\SimpleFS\ISimpleFile;
  40. use OCP\Files\SimpleFS\ISimpleFolder;
  41. use OCP\IRequest;
  42. class JsController extends Controller {
  43. /** @var IAppData */
  44. protected $appData;
  45. /** @var ITimeFactory */
  46. protected $timeFactory;
  47. public function __construct($appName, IRequest $request, Factory $appDataFactory, ITimeFactory $timeFactory) {
  48. parent::__construct($appName, $request);
  49. $this->appData = $appDataFactory->get('js');
  50. $this->timeFactory = $timeFactory;
  51. }
  52. /**
  53. * @PublicPage
  54. * @NoCSRFRequired
  55. * @NoSameSiteCookieRequired
  56. *
  57. * @param string $fileName js filename with extension
  58. * @param string $appName js folder name
  59. * @return FileDisplayResponse|NotFoundResponse
  60. */
  61. public function getJs(string $fileName, string $appName): Response {
  62. try {
  63. $folder = $this->appData->getFolder($appName);
  64. $gzip = false;
  65. $file = $this->getFile($folder, $fileName, $gzip);
  66. } catch (NotFoundException $e) {
  67. return new NotFoundResponse();
  68. }
  69. $response = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => 'application/javascript']);
  70. if ($gzip) {
  71. $response->addHeader('Content-Encoding', 'gzip');
  72. }
  73. $ttl = 31536000;
  74. $response->addHeader('Cache-Control', 'max-age='.$ttl.', immutable');
  75. $expires = new \DateTime();
  76. $expires->setTimestamp($this->timeFactory->getTime());
  77. $expires->add(new \DateInterval('PT'.$ttl.'S'));
  78. $response->addHeader('Expires', $expires->format(\DateTime::RFC1123));
  79. $response->addHeader('Pragma', 'cache');
  80. return $response;
  81. }
  82. /**
  83. * @param ISimpleFolder $folder
  84. * @param string $fileName
  85. * @param bool $gzip is set to true if we use the gzip file
  86. * @return ISimpleFile
  87. *
  88. * @throws NotFoundException
  89. */
  90. private function getFile(ISimpleFolder $folder, string $fileName, bool &$gzip): ISimpleFile {
  91. $encoding = $this->request->getHeader('Accept-Encoding');
  92. if (strpos($encoding, 'gzip') !== false) {
  93. try {
  94. $gzip = true;
  95. return $folder->getFile($fileName . '.gzip'); # Safari doesn't like .gz
  96. } catch (NotFoundException $e) {
  97. // continue
  98. }
  99. }
  100. $gzip = false;
  101. return $folder->getFile($fileName);
  102. }
  103. }