2016-11-09 12:39:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2016, John Molakvoæ (skjnldsv@protonmail.com)
|
|
|
|
*
|
2017-11-06 14:56:42 +00:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
|
|
|
* @author John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
|
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
|
|
|
*
|
2016-11-09 12:39:08 +00:00
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OC\Core\Controller;
|
|
|
|
|
2017-05-10 07:42:40 +00:00
|
|
|
use OC\Files\AppData\Factory;
|
2016-11-09 12:39:08 +00:00
|
|
|
use OCP\AppFramework\Controller;
|
|
|
|
use OCP\AppFramework\Http;
|
|
|
|
use OCP\AppFramework\Http\NotFoundResponse;
|
|
|
|
use OCP\AppFramework\Http\FileDisplayResponse;
|
2017-01-02 15:32:16 +00:00
|
|
|
use OCP\AppFramework\Utility\ITimeFactory;
|
2016-11-30 15:14:00 +00:00
|
|
|
use OCP\Files\IAppData;
|
2016-11-22 11:08:53 +00:00
|
|
|
use OCP\Files\NotFoundException;
|
2017-03-26 14:30:08 +00:00
|
|
|
use OCP\Files\SimpleFS\ISimpleFile;
|
|
|
|
use OCP\Files\SimpleFS\ISimpleFolder;
|
2016-11-09 12:39:08 +00:00
|
|
|
use OCP\IRequest;
|
|
|
|
|
|
|
|
class CssController extends Controller {
|
|
|
|
|
2016-11-30 15:14:00 +00:00
|
|
|
/** @var IAppData */
|
|
|
|
protected $appData;
|
2016-11-09 12:39:08 +00:00
|
|
|
|
2017-01-02 15:32:16 +00:00
|
|
|
/** @var ITimeFactory */
|
2016-11-09 12:39:08 +00:00
|
|
|
protected $timeFactory;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $appName
|
|
|
|
* @param IRequest $request
|
2017-05-10 07:42:40 +00:00
|
|
|
* @param Factory $appDataFactory
|
2017-01-02 15:32:16 +00:00
|
|
|
* @param ITimeFactory $timeFactory
|
2016-11-09 12:39:08 +00:00
|
|
|
*/
|
2017-05-10 07:42:40 +00:00
|
|
|
public function __construct($appName, IRequest $request, Factory $appDataFactory, ITimeFactory $timeFactory) {
|
2016-11-09 12:39:08 +00:00
|
|
|
parent::__construct($appName, $request);
|
|
|
|
|
2017-05-10 07:42:40 +00:00
|
|
|
$this->appData = $appDataFactory->get('css');
|
2016-11-09 12:39:08 +00:00
|
|
|
$this->timeFactory = $timeFactory;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-10 15:16:33 +00:00
|
|
|
* @PublicPage
|
2016-11-09 12:39:08 +00:00
|
|
|
* @NoCSRFRequired
|
|
|
|
*
|
|
|
|
* @param string $fileName css filename with extension
|
2016-11-10 15:36:58 +00:00
|
|
|
* @param string $appName css folder name
|
2016-11-22 10:42:46 +00:00
|
|
|
* @return FileDisplayResponse|NotFoundResponse
|
2016-11-09 12:39:08 +00:00
|
|
|
*/
|
2016-11-10 15:36:58 +00:00
|
|
|
public function getCss($fileName, $appName) {
|
2016-11-09 12:39:08 +00:00
|
|
|
try {
|
2016-11-30 15:14:00 +00:00
|
|
|
$folder = $this->appData->getFolder($appName);
|
2017-03-26 14:30:08 +00:00
|
|
|
$gzip = false;
|
|
|
|
$file = $this->getFile($folder, $fileName, $gzip);
|
2016-11-09 12:39:08 +00:00
|
|
|
} catch(NotFoundException $e) {
|
|
|
|
return new NotFoundResponse();
|
|
|
|
}
|
|
|
|
|
2017-03-26 14:30:08 +00:00
|
|
|
$response = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => 'text/css']);
|
|
|
|
if ($gzip) {
|
|
|
|
$response->addHeader('Content-Encoding', 'gzip');
|
|
|
|
}
|
2017-01-02 15:32:16 +00:00
|
|
|
$response->cacheFor(86400);
|
|
|
|
$expires = new \DateTime();
|
|
|
|
$expires->setTimestamp($this->timeFactory->getTime());
|
|
|
|
$expires->add(new \DateInterval('PT24H'));
|
|
|
|
$response->addHeader('Expires', $expires->format(\DateTime::RFC1123));
|
|
|
|
$response->addHeader('Pragma', 'cache');
|
|
|
|
return $response;
|
2016-11-09 12:39:08 +00:00
|
|
|
}
|
2017-03-26 14:30:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ISimpleFolder $folder
|
|
|
|
* @param string $fileName
|
|
|
|
* @param bool $gzip is set to true if we use the gzip file
|
|
|
|
* @return ISimpleFile
|
|
|
|
*/
|
|
|
|
private function getFile(ISimpleFolder $folder, $fileName, &$gzip) {
|
|
|
|
$encoding = $this->request->getHeader('Accept-Encoding');
|
|
|
|
|
|
|
|
if ($encoding !== null && strpos($encoding, 'gzip') !== false) {
|
|
|
|
try {
|
|
|
|
$gzip = true;
|
2017-03-29 06:11:51 +00:00
|
|
|
return $folder->getFile($fileName . '.gzip'); # Safari doesn't like .gz
|
2017-03-26 14:30:08 +00:00
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
// continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$gzip = false;
|
|
|
|
return $folder->getFile($fileName);
|
|
|
|
}
|
2016-11-09 12:39:08 +00:00
|
|
|
}
|