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.

CssController.php 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, John Molakvoæ (skjnldsv@protonmail.com)
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author John Molakvoæ (skjnldsv) <skjnldsv@protonmail.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\Utility\ITimeFactory;
  33. use OCP\Files\IAppData;
  34. use OCP\Files\NotFoundException;
  35. use OCP\Files\SimpleFS\ISimpleFile;
  36. use OCP\Files\SimpleFS\ISimpleFolder;
  37. use OCP\IRequest;
  38. class CssController extends Controller {
  39. /** @var IAppData */
  40. protected $appData;
  41. /** @var ITimeFactory */
  42. protected $timeFactory;
  43. /**
  44. * @param string $appName
  45. * @param IRequest $request
  46. * @param Factory $appDataFactory
  47. * @param ITimeFactory $timeFactory
  48. */
  49. public function __construct($appName, IRequest $request, Factory $appDataFactory, ITimeFactory $timeFactory) {
  50. parent::__construct($appName, $request);
  51. $this->appData = $appDataFactory->get('css');
  52. $this->timeFactory = $timeFactory;
  53. }
  54. /**
  55. * @PublicPage
  56. * @NoCSRFRequired
  57. *
  58. * @param string $fileName css filename with extension
  59. * @param string $appName css folder name
  60. * @return FileDisplayResponse|NotFoundResponse
  61. */
  62. public function getCss($fileName, $appName) {
  63. try {
  64. $folder = $this->appData->getFolder($appName);
  65. $gzip = false;
  66. $file = $this->getFile($folder, $fileName, $gzip);
  67. } catch(NotFoundException $e) {
  68. return new NotFoundResponse();
  69. }
  70. $response = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => 'text/css']);
  71. if ($gzip) {
  72. $response->addHeader('Content-Encoding', 'gzip');
  73. }
  74. $response->cacheFor(86400);
  75. $expires = new \DateTime();
  76. $expires->setTimestamp($this->timeFactory->getTime());
  77. $expires->add(new \DateInterval('PT24H'));
  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. private function getFile(ISimpleFolder $folder, $fileName, &$gzip) {
  89. $encoding = $this->request->getHeader('Accept-Encoding');
  90. if (strpos($encoding, 'gzip') !== false) {
  91. try {
  92. $gzip = true;
  93. return $folder->getFile($fileName . '.gzip'); # Safari doesn't like .gz
  94. } catch (NotFoundException $e) {
  95. // continue
  96. }
  97. }
  98. $gzip = false;
  99. return $folder->getFile($fileName);
  100. }
  101. }