1
0

JsController.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * @copyright 2017, Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Roeland Jago Douma <roeland@famdouma.nl>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OC\Core\Controller;
  24. use OC\Files\AppData\Factory;
  25. use OCP\AppFramework\Controller;
  26. use OCP\AppFramework\Http;
  27. use OCP\AppFramework\Http\NotFoundResponse;
  28. use OCP\AppFramework\Http\FileDisplayResponse;
  29. use OCP\AppFramework\Utility\ITimeFactory;
  30. use OCP\Files\IAppData;
  31. use OCP\Files\NotFoundException;
  32. use OCP\Files\SimpleFS\ISimpleFile;
  33. use OCP\Files\SimpleFS\ISimpleFolder;
  34. use OCP\IRequest;
  35. class JsController extends Controller {
  36. /** @var IAppData */
  37. protected $appData;
  38. /** @var ITimeFactory */
  39. protected $timeFactory;
  40. /**
  41. * @param string $appName
  42. * @param IRequest $request
  43. * @param Factory $appDataFactory
  44. * @param ITimeFactory $timeFactory
  45. */
  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. *
  55. * @param string $fileName css filename with extension
  56. * @param string $appName css folder name
  57. * @return FileDisplayResponse|NotFoundResponse
  58. */
  59. public function getJs($fileName, $appName) {
  60. try {
  61. $folder = $this->appData->getFolder($appName);
  62. $gzip = false;
  63. $file = $this->getFile($folder, $fileName, $gzip);
  64. } catch(NotFoundException $e) {
  65. return new NotFoundResponse();
  66. }
  67. $response = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => 'application/javascript']);
  68. if ($gzip) {
  69. $response->addHeader('Content-Encoding', 'gzip');
  70. }
  71. $response->cacheFor(86400);
  72. $expires = new \DateTime();
  73. $expires->setTimestamp($this->timeFactory->getTime());
  74. $expires->add(new \DateInterval('PT24H'));
  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. private function getFile(ISimpleFolder $folder, $fileName, &$gzip) {
  86. $encoding = $this->request->getHeader('Accept-Encoding');
  87. if ($encoding !== null && strpos($encoding, 'gzip') !== false) {
  88. try {
  89. $gzip = true;
  90. return $folder->getFile($fileName . '.gzip'); # Safari doesn't like .gz
  91. } catch (NotFoundException $e) {
  92. // continue
  93. }
  94. }
  95. $gzip = false;
  96. return $folder->getFile($fileName);
  97. }
  98. }