JsController.php 3.4 KB

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