CssController.php 3.2 KB

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