2017-03-22 16:47:07 +03:00
|
|
|
<?php
|
2018-01-17 00:45:31 +03:00
|
|
|
declare(strict_types=1);
|
2017-03-22 16:47:07 +03:00
|
|
|
/**
|
|
|
|
* @copyright 2017, Roeland Jago Douma <roeland@famdouma.nl>
|
|
|
|
*
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2017-03-22 16:47:07 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
|
|
|
*
|
|
|
|
* @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/>.
|
|
|
|
*
|
|
|
|
*/
|
2019-11-22 22:52:10 +03:00
|
|
|
|
2017-03-22 16:47:07 +03:00
|
|
|
namespace OC\Core\Controller;
|
|
|
|
|
2017-05-10 10:41:33 +03:00
|
|
|
use OC\Files\AppData\Factory;
|
2017-03-22 16:47:07 +03:00
|
|
|
use OCP\AppFramework\Controller;
|
|
|
|
use OCP\AppFramework\Http;
|
|
|
|
use OCP\AppFramework\Http\FileDisplayResponse;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OCP\AppFramework\Http\NotFoundResponse;
|
2018-01-17 00:45:31 +03:00
|
|
|
use OCP\AppFramework\Http\Response;
|
2017-03-22 16:47:07 +03:00
|
|
|
use OCP\AppFramework\Utility\ITimeFactory;
|
|
|
|
use OCP\Files\IAppData;
|
|
|
|
use OCP\Files\NotFoundException;
|
2017-03-26 17:30:08 +03:00
|
|
|
use OCP\Files\SimpleFS\ISimpleFile;
|
|
|
|
use OCP\Files\SimpleFS\ISimpleFolder;
|
2017-03-22 16:47:07 +03:00
|
|
|
use OCP\IRequest;
|
|
|
|
|
|
|
|
class JsController extends Controller {
|
|
|
|
|
|
|
|
/** @var IAppData */
|
|
|
|
protected $appData;
|
|
|
|
|
|
|
|
/** @var ITimeFactory */
|
|
|
|
protected $timeFactory;
|
|
|
|
|
2017-05-10 10:41:33 +03:00
|
|
|
public function __construct($appName, IRequest $request, Factory $appDataFactory, ITimeFactory $timeFactory) {
|
2017-03-22 16:47:07 +03:00
|
|
|
parent::__construct($appName, $request);
|
|
|
|
|
2017-05-10 10:41:33 +03:00
|
|
|
$this->appData = $appDataFactory->get('js');
|
2017-03-22 16:47:07 +03:00
|
|
|
$this->timeFactory = $timeFactory;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @PublicPage
|
|
|
|
* @NoCSRFRequired
|
2018-10-17 10:24:21 +03:00
|
|
|
* @NoSameSiteCookieRequired
|
2017-03-22 16:47:07 +03:00
|
|
|
*
|
2018-06-25 12:17:00 +03:00
|
|
|
* @param string $fileName js filename with extension
|
|
|
|
* @param string $appName js folder name
|
2017-03-22 16:47:07 +03:00
|
|
|
* @return FileDisplayResponse|NotFoundResponse
|
|
|
|
*/
|
2018-01-17 00:45:31 +03:00
|
|
|
public function getJs(string $fileName, string $appName): Response {
|
2017-03-22 16:47:07 +03:00
|
|
|
try {
|
|
|
|
$folder = $this->appData->getFolder($appName);
|
2017-03-26 17:30:08 +03:00
|
|
|
$gzip = false;
|
|
|
|
$file = $this->getFile($folder, $fileName, $gzip);
|
2017-03-22 16:47:07 +03:00
|
|
|
} catch(NotFoundException $e) {
|
|
|
|
return new NotFoundResponse();
|
|
|
|
}
|
|
|
|
|
2017-03-26 17:30:08 +03:00
|
|
|
$response = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => 'application/javascript']);
|
|
|
|
if ($gzip) {
|
|
|
|
$response->addHeader('Content-Encoding', 'gzip');
|
|
|
|
}
|
2018-01-17 00:45:31 +03:00
|
|
|
|
|
|
|
$ttl = 31536000;
|
|
|
|
$response->addHeader('Cache-Control', 'max-age='.$ttl.', immutable');
|
|
|
|
|
2017-03-22 16:47:07 +03:00
|
|
|
$expires = new \DateTime();
|
|
|
|
$expires->setTimestamp($this->timeFactory->getTime());
|
2018-01-17 00:45:31 +03:00
|
|
|
$expires->add(new \DateInterval('PT'.$ttl.'S'));
|
2017-03-22 16:47:07 +03:00
|
|
|
$response->addHeader('Expires', $expires->format(\DateTime::RFC1123));
|
|
|
|
$response->addHeader('Pragma', 'cache');
|
|
|
|
return $response;
|
|
|
|
}
|
2017-03-26 17:30:08 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ISimpleFolder $folder
|
|
|
|
* @param string $fileName
|
|
|
|
* @param bool $gzip is set to true if we use the gzip file
|
|
|
|
* @return ISimpleFile
|
2018-01-17 00:45:31 +03:00
|
|
|
*
|
|
|
|
* @throws NotFoundException
|
2017-03-26 17:30:08 +03:00
|
|
|
*/
|
2018-01-17 00:45:31 +03:00
|
|
|
private function getFile(ISimpleFolder $folder, string $fileName, bool &$gzip): ISimpleFile {
|
2017-03-26 17:30:08 +03:00
|
|
|
$encoding = $this->request->getHeader('Accept-Encoding');
|
|
|
|
|
2018-01-12 16:15:12 +03:00
|
|
|
if (strpos($encoding, 'gzip') !== false) {
|
2017-03-26 17:30:08 +03:00
|
|
|
try {
|
|
|
|
$gzip = true;
|
2017-03-29 09:11:51 +03:00
|
|
|
return $folder->getFile($fileName . '.gzip'); # Safari doesn't like .gz
|
2017-03-26 17:30:08 +03:00
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
// continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$gzip = false;
|
|
|
|
return $folder->getFile($fileName);
|
|
|
|
}
|
2017-03-22 16:47:07 +03:00
|
|
|
}
|