CssController.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, John Molakvoæ (skjnldsv@protonmail.com)
  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\IConfig;
  41. use OCP\IRequest;
  42. class CssController extends Controller {
  43. /** @var IAppData */
  44. protected $appData;
  45. /** @var ITimeFactory */
  46. protected $timeFactory;
  47. public function __construct(string $appName,
  48. IRequest $request,
  49. Factory $appDataFactory,
  50. ITimeFactory $timeFactory) {
  51. parent::__construct($appName, $request);
  52. $this->appData = $appDataFactory->get('css');
  53. $this->timeFactory = $timeFactory;
  54. }
  55. /**
  56. * @PublicPage
  57. * @NoCSRFRequired
  58. * @NoSameSiteCookieRequired
  59. *
  60. * @param string $fileName css filename with extension
  61. * @param string $appName css folder name
  62. * @return FileDisplayResponse|NotFoundResponse
  63. */
  64. public function getCss(string $fileName, string $appName): Response {
  65. try {
  66. $folder = $this->appData->getFolder($appName);
  67. $gzip = false;
  68. $file = $this->getFile($folder, $fileName, $gzip);
  69. } catch(NotFoundException $e) {
  70. return new NotFoundResponse();
  71. }
  72. $response = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => 'text/css']);
  73. if ($gzip) {
  74. $response->addHeader('Content-Encoding', 'gzip');
  75. }
  76. $ttl = 31536000;
  77. $response->addHeader('Cache-Control', 'max-age='.$ttl.', immutable');
  78. $expires = new \DateTime();
  79. $expires->setTimestamp($this->timeFactory->getTime());
  80. $expires->add(new \DateInterval('PT'.$ttl.'S'));
  81. $response->addHeader('Expires', $expires->format(\DateTime::RFC1123));
  82. $response->addHeader('Pragma', 'cache');
  83. return $response;
  84. }
  85. /**
  86. * @param ISimpleFolder $folder
  87. * @param string $fileName
  88. * @param bool $gzip is set to true if we use the gzip file
  89. * @return ISimpleFile
  90. * @throws NotFoundException
  91. */
  92. private function getFile(ISimpleFolder $folder, string $fileName, bool &$gzip): ISimpleFile {
  93. $encoding = $this->request->getHeader('Accept-Encoding');
  94. if (strpos($encoding, 'gzip') !== false) {
  95. try {
  96. $gzip = true;
  97. return $folder->getFile($fileName . '.gzip'); # Safari doesn't like .gz
  98. } catch (NotFoundException $e) {
  99. // continue
  100. }
  101. }
  102. $gzip = false;
  103. return $folder->getFile($fileName);
  104. }
  105. }