Add cache 1 day cache to preview endpoint

Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
This commit is contained in:
Roeland Jago Douma 2016-10-21 12:51:22 +02:00
parent 55af6b45f7
commit 9e6b26dcd0
No known key found for this signature in database
GPG Key ID: 1E152838F164D13B
2 changed files with 26 additions and 5 deletions

View File

@ -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);
}

View File

@ -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'));
}
}