Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions core/Controller/CssController.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2016, John Molakvoæ ([email protected])
*
Expand Down Expand Up @@ -31,11 +32,13 @@
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\NotFoundResponse;
use OCP\AppFramework\Http\FileDisplayResponse;
use OCP\AppFramework\Http\Response;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Files\IAppData;
use OCP\Files\NotFoundException;
use OCP\Files\SimpleFS\ISimpleFile;
use OCP\Files\SimpleFS\ISimpleFolder;
use OCP\IConfig;
use OCP\IRequest;

class CssController extends Controller {
Expand All @@ -46,13 +49,10 @@ class CssController extends Controller {
/** @var ITimeFactory */
protected $timeFactory;

/**
* @param string $appName
* @param IRequest $request
* @param Factory $appDataFactory
* @param ITimeFactory $timeFactory
*/
public function __construct($appName, IRequest $request, Factory $appDataFactory, ITimeFactory $timeFactory) {
public function __construct(string $appName,
IRequest $request,
Factory $appDataFactory,
ITimeFactory $timeFactory) {
parent::__construct($appName, $request);

$this->appData = $appDataFactory->get('css');
Expand All @@ -67,7 +67,7 @@ public function __construct($appName, IRequest $request, Factory $appDataFactory
* @param string $appName css folder name
* @return FileDisplayResponse|NotFoundResponse
*/
public function getCss($fileName, $appName) {
public function getCss(string $fileName, string $appName): Response {
try {
$folder = $this->appData->getFolder($appName);
$gzip = false;
Expand All @@ -80,10 +80,13 @@ public function getCss($fileName, $appName) {
if ($gzip) {
$response->addHeader('Content-Encoding', 'gzip');
}
$response->cacheFor(86400);

$ttl = 31536000;
$response->addHeader('Cache-Control', 'max-age='.$ttl.', immutable');

$expires = new \DateTime();
$expires->setTimestamp($this->timeFactory->getTime());
$expires->add(new \DateInterval('PT24H'));
$expires->add(new \DateInterval('PT'.$ttl.'S'));
$response->addHeader('Expires', $expires->format(\DateTime::RFC1123));
$response->addHeader('Pragma', 'cache');
return $response;
Expand All @@ -94,8 +97,9 @@ public function getCss($fileName, $appName) {
* @param string $fileName
* @param bool $gzip is set to true if we use the gzip file
* @return ISimpleFile
* @throws NotFoundException
*/
private function getFile(ISimpleFolder $folder, $fileName, &$gzip) {
private function getFile(ISimpleFolder $folder, string $fileName, bool &$gzip): ISimpleFile {
$encoding = $this->request->getHeader('Accept-Encoding');

if (strpos($encoding, 'gzip') !== false) {
Expand Down
21 changes: 11 additions & 10 deletions core/Controller/JsController.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/**
* @copyright 2017, Roeland Jago Douma <[email protected]>
*
Expand Down Expand Up @@ -29,6 +30,7 @@
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\NotFoundResponse;
use OCP\AppFramework\Http\FileDisplayResponse;
use OCP\AppFramework\Http\Response;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Files\IAppData;
use OCP\Files\NotFoundException;
Expand All @@ -44,12 +46,6 @@ class JsController extends Controller {
/** @var ITimeFactory */
protected $timeFactory;

/**
* @param string $appName
* @param IRequest $request
* @param Factory $appDataFactory
* @param ITimeFactory $timeFactory
*/
public function __construct($appName, IRequest $request, Factory $appDataFactory, ITimeFactory $timeFactory) {
parent::__construct($appName, $request);

Expand All @@ -65,7 +61,7 @@ public function __construct($appName, IRequest $request, Factory $appDataFactory
* @param string $appName css folder name
* @return FileDisplayResponse|NotFoundResponse
*/
public function getJs($fileName, $appName) {
public function getJs(string $fileName, string $appName): Response {
try {
$folder = $this->appData->getFolder($appName);
$gzip = false;
Expand All @@ -78,10 +74,13 @@ public function getJs($fileName, $appName) {
if ($gzip) {
$response->addHeader('Content-Encoding', 'gzip');
}
$response->cacheFor(86400);

$ttl = 31536000;
$response->addHeader('Cache-Control', 'max-age='.$ttl.', immutable');

$expires = new \DateTime();
$expires->setTimestamp($this->timeFactory->getTime());
$expires->add(new \DateInterval('PT24H'));
$expires->add(new \DateInterval('PT'.$ttl.'S'));
$response->addHeader('Expires', $expires->format(\DateTime::RFC1123));
$response->addHeader('Pragma', 'cache');
return $response;
Expand All @@ -92,8 +91,10 @@ public function getJs($fileName, $appName) {
* @param string $fileName
* @param bool $gzip is set to true if we use the gzip file
* @return ISimpleFile
*
* @throws NotFoundException
*/
private function getFile(ISimpleFolder $folder, $fileName, &$gzip) {
private function getFile(ISimpleFolder $folder, string $fileName, bool &$gzip): ISimpleFile {
$encoding = $this->request->getHeader('Accept-Encoding');

if (strpos($encoding, 'gzip') !== false) {
Expand Down
12 changes: 6 additions & 6 deletions tests/Core/Controller/CssControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ public function testGetFile() {
->willReturn($file);

$expected = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => 'text/css']);
$expected->cacheFor(86400);
$expected->addHeader('Cache-Control', 'max-age=31536000, immutable');
$expires = new \DateTime();
$expires->setTimestamp(1337);
$expires->add(new \DateInterval('PT24H'));
$expires->add(new \DateInterval('PT31536000S'));
$expected->addHeader('Expires', $expires->format(\DateTime::RFC1123));
$expected->addHeader('Pragma', 'cache');

Expand All @@ -137,10 +137,10 @@ public function testGetGzipFile() {

$expected = new FileDisplayResponse($gzipFile, Http::STATUS_OK, ['Content-Type' => 'text/css']);
$expected->addHeader('Content-Encoding', 'gzip');
$expected->cacheFor(86400);
$expected->addHeader('Cache-Control', 'max-age=31536000, immutable');
$expires = new \DateTime();
$expires->setTimestamp(1337);
$expires->add(new \DateInterval('PT24H'));
$expires->add(new \DateInterval('PT31536000S'));
$expected->addHeader('Expires', $expires->format(\DateTime::RFC1123));
$expected->addHeader('Pragma', 'cache');

Expand Down Expand Up @@ -170,10 +170,10 @@ function($fileName) use ($file) {
->willReturn('gzip, deflate');

$expected = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => 'text/css']);
$expected->cacheFor(86400);
$expected->addHeader('Cache-Control', 'max-age=31536000, immutable');
$expires = new \DateTime();
$expires->setTimestamp(1337);
$expires->add(new \DateInterval('PT24H'));
$expires->add(new \DateInterval('PT31536000S'));
$expected->addHeader('Expires', $expires->format(\DateTime::RFC1123));
$expected->addHeader('Pragma', 'cache');

Expand Down
12 changes: 6 additions & 6 deletions tests/Core/Controller/JsControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ public function testGetFile() {
->willReturn($file);

$expected = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => 'application/javascript']);
$expected->cacheFor(86400);
$expected->addHeader('Cache-Control', 'max-age=31536000, immutable');
$expires = new \DateTime();
$expires->setTimestamp(1337);
$expires->add(new \DateInterval('PT24H'));
$expires->add(new \DateInterval('PT31536000S'));
$expected->addHeader('Expires', $expires->format(\DateTime::RFC1123));
$expected->addHeader('Pragma', 'cache');

Expand All @@ -137,10 +137,10 @@ public function testGetGzipFile() {

$expected = new FileDisplayResponse($gzipFile, Http::STATUS_OK, ['Content-Type' => 'application/javascript']);
$expected->addHeader('Content-Encoding', 'gzip');
$expected->cacheFor(86400);
$expected->addHeader('Cache-Control', 'max-age=31536000, immutable');
$expires = new \DateTime();
$expires->setTimestamp(1337);
$expires->add(new \DateInterval('PT24H'));
$expires->add(new \DateInterval('PT31536000S'));
$expected->addHeader('Expires', $expires->format(\DateTime::RFC1123));
$expected->addHeader('Pragma', 'cache');

Expand Down Expand Up @@ -170,10 +170,10 @@ function($fileName) use ($file) {
->willReturn('gzip, deflate');

$expected = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => 'application/javascript']);
$expected->cacheFor(86400);
$expected->addHeader('Cache-Control', 'max-age=31536000, immutable');
$expires = new \DateTime();
$expires->setTimestamp(1337);
$expires->add(new \DateInterval('PT24H'));
$expires->add(new \DateInterval('PT31536000S'));
$expected->addHeader('Expires', $expires->format(\DateTime::RFC1123));
$expected->addHeader('Pragma', 'cache');

Expand Down