JsController.php 3.5 KB

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