From 9e6b26dcd05362599bade82d7b326336a5cc7c0b Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Fri, 21 Oct 2016 12:51:22 +0200 Subject: Add cache 1 day cache to preview endpoint Signed-off-by: Roeland Jago Douma --- core/Controller/PreviewController.php | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) (limited to 'core/Controller/PreviewController.php') 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); } -- cgit v1.2.3