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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * @copyright 2017, Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OC\Core\Controller;
  26. use OC\Files\AppData\Factory;
  27. use OCP\AppFramework\Controller;
  28. use OCP\AppFramework\Http;
  29. use OCP\AppFramework\Http\NotFoundResponse;
  30. use OCP\AppFramework\Http\FileDisplayResponse;
  31. use OCP\AppFramework\Utility\ITimeFactory;
  32. use OCP\Files\IAppData;
  33. use OCP\Files\NotFoundException;
  34. use OCP\Files\SimpleFS\ISimpleFile;
  35. use OCP\Files\SimpleFS\ISimpleFolder;
  36. use OCP\IRequest;
  37. class JsController extends Controller {
  38. /** @var IAppData */
  39. protected $appData;
  40. /** @var ITimeFactory */
  41. protected $timeFactory;
  42. /**
  43. * @param string $appName
  44. * @param IRequest $request
  45. * @param Factory $appDataFactory
  46. * @param ITimeFactory $timeFactory
  47. */
  48. public function __construct($appName, IRequest $request, Factory $appDataFactory, ITimeFactory $timeFactory) {
  49. parent::__construct($appName, $request);
  50. $this->appData = $appDataFactory->get('js');
  51. $this->timeFactory = $timeFactory;
  52. }
  53. /**
  54. * @PublicPage
  55. * @NoCSRFRequired
  56. *
  57. * @param string $fileName css filename with extension
  58. * @param string $appName css folder name
  59. * @return FileDisplayResponse|NotFoundResponse
  60. */
  61. public function getJs($fileName, $appName) {
  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. $response->cacheFor(86400);
  74. $expires = new \DateTime();
  75. $expires->setTimestamp($this->timeFactory->getTime());
  76. $expires->add(new \DateInterval('PT24H'));
  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. private function getFile(ISimpleFolder $folder, $fileName, &$gzip) {
  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. }