diff options
author | Roeland Jago Douma <roeland@famdouma.nl> | 2016-08-29 14:55:23 +0200 |
---|---|---|
committer | Roeland Jago Douma <roeland@famdouma.nl> | 2016-08-30 09:00:16 +0200 |
commit | 14136295b7568c6e34504c101eba0ee10f5c74fd (patch) | |
tree | 8fa8c31163b4a4fc8621919f330f22e5631af385 /core/Controller/AvatarController.php | |
parent | c481210e2b24706f4cb0159bd82eb16ec58d9e92 (diff) | |
download | nextcloud-server-14136295b7568c6e34504c101eba0ee10f5c74fd.tar.gz nextcloud-server-14136295b7568c6e34504c101eba0ee10f5c74fd.zip |
Cache avatars properly
* Set proper caching headers for avatars (15 minutes)
* For our own avatar use some extra logic to invalidate when we update
Diffstat (limited to 'core/Controller/AvatarController.php')
-rw-r--r-- | core/Controller/AvatarController.php | 34 |
1 files changed, 29 insertions, 5 deletions
diff --git a/core/Controller/AvatarController.php b/core/Controller/AvatarController.php index 5b64320948a..484b6f524bf 100644 --- a/core/Controller/AvatarController.php +++ b/core/Controller/AvatarController.php @@ -27,6 +27,7 @@ */ namespace OC\Core\Controller; +use OC\AppFramework\Utility\TimeFactory; use OCP\AppFramework\Controller; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataDisplayResponse; @@ -73,6 +74,9 @@ class AvatarController extends Controller { /** @var string */ protected $userId; + /** @var TimeFactory */ + protected $timeFactory; + /** * @param string $appName * @param IRequest $request @@ -83,6 +87,7 @@ class AvatarController extends Controller { * @param IRootFolder $rootFolder * @param ILogger $logger * @param string $userId + * @param TimeFactory $timeFactory */ public function __construct($appName, IRequest $request, @@ -92,7 +97,8 @@ class AvatarController extends Controller { IUserManager $userManager, IRootFolder $rootFolder, ILogger $logger, - $userId) { + $userId, + TimeFactory $timeFactory) { parent::__construct($appName, $request); $this->avatarManager = $avatarManager; @@ -102,6 +108,7 @@ class AvatarController extends Controller { $this->rootFolder = $rootFolder; $this->logger = $logger; $this->userId = $userId; + $this->timeFactory = $timeFactory; } /** @@ -126,6 +133,21 @@ class AvatarController extends Controller { Http::STATUS_OK, ['Content-Type' => $avatar->getMimeType()]); $resp->setETag($avatar->getEtag()); + + // Let cache this! + $resp->addHeader('Pragma', 'public'); + // Cache for 15 minutes + $resp->cacheFor(900); + // Set last modified + $lastModified = new \DateTime(); + $lastModified->setTimestamp($avatar->getMTime()); + $resp->setLastModified($lastModified); + + $expires = new \DateTime(); + $expires->setTimestamp($this->timeFactory->getTime()); + $expires->add(new \DateInterval('PT15M')); + $resp->addHeader('Expires', $expires->format(\DateTime::RFC2822)); + } catch (NotFoundException $e) { $user = $this->userManager->get($userId); $resp = new JSONResponse([ @@ -133,18 +155,20 @@ class AvatarController extends Controller { 'displayname' => $user->getDisplayName(), ], ]); + // Don't cache this + $resp->cacheFor(0); + $resp->setLastModified(new \DateTime('now', new \DateTimeZone('GMT'))); } catch (\Exception $e) { $resp = new JSONResponse([ 'data' => [ 'displayname' => '', ], ]); + // Don't cache this + $resp->cacheFor(0); + $resp->setLastModified(new \DateTime('now', new \DateTimeZone('GMT'))); } - $resp->addHeader('Pragma', 'public'); - $resp->cacheFor(0); - $resp->setLastModified(new \DateTime('now', new \DateTimeZone('GMT'))); - return $resp; } |