diff options
author | Roeland Jago Douma <roeland@famdouma.nl> | 2016-10-21 12:51:22 +0200 |
---|---|---|
committer | Roeland Jago Douma <roeland@famdouma.nl> | 2016-11-03 14:00:33 +0100 |
commit | 9e6b26dcd05362599bade82d7b326336a5cc7c0b (patch) | |
tree | 15ece4612b963dc3a9705cec4491253da329b69e | |
parent | 55af6b45f72e0717d7b6f07b69bc0f12f4fb4cd9 (diff) | |
download | nextcloud-server-9e6b26dcd05362599bade82d7b326336a5cc7c0b.tar.gz nextcloud-server-9e6b26dcd05362599bade82d7b326336a5cc7c0b.zip |
Add cache 1 day cache to preview endpoint
Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
-rw-r--r-- | core/Controller/PreviewController.php | 22 | ||||
-rw-r--r-- | tests/Core/Controller/PreviewControllerTest.php | 9 |
2 files changed, 26 insertions, 5 deletions
diff --git a/core/Controller/PreviewController.php b/core/Controller/PreviewController.php index 3a245374e96..4b9ba8b60d4 100644 --- a/core/Controller/PreviewController.php +++ b/core/Controller/PreviewController.php @@ -24,6 +24,7 @@ namespace OC\Core\Controller; use OCP\AppFramework\Controller; +use OCP\AppFramework\Utility\ITimeFactory; use OCP\Files\File; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; @@ -44,6 +45,9 @@ class PreviewController extends Controller { /** @var IPreview */ private $preview; + /** @var ITimeFactory */ + private $timeFactory; + /** * PreviewController constructor. * @@ -57,13 +61,15 @@ class PreviewController extends Controller { IRequest $request, IPreview $preview, IRootFolder $root, - $userId + $userId, + ITimeFactory $timeFactory ) { parent::__construct($appName, $request); $this->preview = $preview; $this->root = $root; $this->userId = $userId; + $this->timeFactory = $timeFactory; } /** @@ -105,7 +111,19 @@ class PreviewController extends Controller { try { $f = $this->preview->getPreview($file, $x, $y, !$a, $mode); - return new FileDisplayResponse($f, Http::STATUS_OK, ['Content-Type' => $f->getMimeType()]); + $response = new FileDisplayResponse($f, Http::STATUS_OK, ['Content-Type' => $f->getMimeType()]); + + // Let cache this! + $response->addHeader('Pragma', 'public'); + + // Cache previews for 24H + $response->cacheFor(3600 * 24); + $expires = new \DateTime(); + $expires->setTimestamp($this->timeFactory->getTime()); + $expires->add(new \DateInterval('P1D')); + $response->addHeader('Expires', $expires->format(\DateTime::RFC2822)); + + return $response; } catch (NotFoundException $e) { return new DataResponse([], Http::STATUS_NOT_FOUND); } diff --git a/tests/Core/Controller/PreviewControllerTest.php b/tests/Core/Controller/PreviewControllerTest.php index 63a26761344..df1af6a0802 100644 --- a/tests/Core/Controller/PreviewControllerTest.php +++ b/tests/Core/Controller/PreviewControllerTest.php @@ -25,6 +25,7 @@ namespace Tests\Core\Controller; use OC\Core\Controller\PreviewController; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; +use OCP\AppFramework\Utility\ITimeFactory; use OCP\Files\File; use OCP\Files\Folder; use OCP\Files\IRootFolder; @@ -59,7 +60,8 @@ class PreviewControllerTest extends \Test\TestCase { $this->createMock(IRequest::class), $this->previewManager, $this->rootFolder, - $this->userId + $this->userId, + $this->createMock(ITimeFactory::class) ); } @@ -216,8 +218,9 @@ class PreviewControllerTest extends \Test\TestCase { ->willReturn('myMime'); $res = $this->controller->getPreview('file', 10, 10, true, true, 'myMode'); - $expected = new Http\FileDisplayResponse($preview, Http::STATUS_OK, ['Content-Type' => 'myMime']); - $this->assertEquals($expected, $res); + $this->assertEquals('myMime', $res->getHeaders()['Content-Type']); + $this->assertEquals(Http::STATUS_OK, $res->getStatus()); + $this->assertEquals($preview, $this->invokePrivate($res, 'file')); } } |