CssController.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 OCP\AppFramework\Controller;
  23. use OCP\AppFramework\Http;
  24. use OCP\AppFramework\Http\NotFoundResponse;
  25. use OCP\AppFramework\Http\FileDisplayResponse;
  26. use OCP\AppFramework\Utility\ITimeFactory;
  27. use OCP\Files\IAppData;
  28. use OCP\Files\NotFoundException;
  29. use OCP\IRequest;
  30. class CssController extends Controller {
  31. /** @var IAppData */
  32. protected $appData;
  33. /** @var ITimeFactory */
  34. protected $timeFactory;
  35. /**
  36. * @param string $appName
  37. * @param IRequest $request
  38. * @param IAppData $appData
  39. * @param ITimeFactory $timeFactory
  40. */
  41. public function __construct($appName, IRequest $request, IAppData $appData, ITimeFactory $timeFactory) {
  42. parent::__construct($appName, $request);
  43. $this->appData = $appData;
  44. $this->timeFactory = $timeFactory;
  45. }
  46. /**
  47. * @PublicPage
  48. * @NoCSRFRequired
  49. *
  50. * @param string $fileName css filename with extension
  51. * @param string $appName css folder name
  52. * @return FileDisplayResponse|NotFoundResponse
  53. */
  54. public function getCss($fileName, $appName) {
  55. try {
  56. $folder = $this->appData->getFolder($appName);
  57. $cssFile = $folder->getFile($fileName);
  58. } catch(NotFoundException $e) {
  59. return new NotFoundResponse();
  60. }
  61. $response = new FileDisplayResponse($cssFile, Http::STATUS_OK, ['Content-Type' => 'text/css']);
  62. $response->cacheFor(86400);
  63. $expires = new \DateTime();
  64. $expires->setTimestamp($this->timeFactory->getTime());
  65. $expires->add(new \DateInterval('PT24H'));
  66. $response->addHeader('Expires', $expires->format(\DateTime::RFC1123));
  67. $response->addHeader('Pragma', 'cache');
  68. return $response;
  69. }
  70. }