summaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorRoeland Jago Douma <roeland@famdouma.nl>2016-08-29 14:55:23 +0200
committerRoeland Jago Douma <roeland@famdouma.nl>2016-08-30 09:00:16 +0200
commit14136295b7568c6e34504c101eba0ee10f5c74fd (patch)
tree8fa8c31163b4a4fc8621919f330f22e5631af385 /core
parentc481210e2b24706f4cb0159bd82eb16ec58d9e92 (diff)
downloadnextcloud-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')
-rw-r--r--core/Controller/AvatarController.php34
-rw-r--r--core/js/config.php14
-rw-r--r--core/js/jquery.avatar.js23
-rw-r--r--core/templates/layout.user.php4
4 files changed, 61 insertions, 14 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;
}
diff --git a/core/js/config.php b/core/js/config.php
index e88d9c741a1..8e85dc91cb1 100644
--- a/core/js/config.php
+++ b/core/js/config.php
@@ -149,8 +149,8 @@ $array = array(
"firstDay" => json_encode($l->l('firstday', null)) ,
"oc_config" => json_encode(
array(
- 'session_lifetime' => min(\OCP\Config::getSystemValue('session_lifetime', OC::$server->getIniWrapper()->getNumeric('session.gc_maxlifetime')), OC::$server->getIniWrapper()->getNumeric('session.gc_maxlifetime')),
- 'session_keepalive' => \OCP\Config::getSystemValue('session_keepalive', true),
+ 'session_lifetime' => min($config->getSystemValue('session_lifetime', OC::$server->getIniWrapper()->getNumeric('session.gc_maxlifetime')), OC::$server->getIniWrapper()->getNumeric('session.gc_maxlifetime')),
+ 'session_keepalive' => $config->getSystemValue('session_keepalive', true),
'version' => implode('.', \OCP\Util::getVersion()),
'versionstring' => OC_Util::getVersionString(),
'enable_avatars' => \OC::$server->getConfig()->getSystemValue('enable_avatars', true) === true,
@@ -187,9 +187,17 @@ $array = array(
'longFooter' => $defaults->getLongFooter(),
'folder' => OC_Util::getTheme(),
)
- )
+ ),
);
+if (OC_User::getUser() !== null && OC_User::getUser() !== false) {
+ $array['oc_userconfig'] = json_encode([
+ 'avatar' => [
+ 'version' => (int)$config->getUserValue(OC_User::getUser(), 'avatar', 'version', 0),
+ ]
+ ]);
+}
+
// Allow hooks to modify the output values
OC_Hook::emit('\OCP\Config', 'js', array('array' => &$array));
diff --git a/core/js/jquery.avatar.js b/core/js/jquery.avatar.js
index 6ae9cf78a13..754400acd7b 100644
--- a/core/js/jquery.avatar.js
+++ b/core/js/jquery.avatar.js
@@ -74,10 +74,25 @@
user = String(user).replace(/\//g,'');
var $div = this;
+ var url;
- var url = OC.generateUrl(
- '/avatar/{user}/{size}',
- {user: user, size: Math.ceil(size * window.devicePixelRatio)});
+ // If this is our own avatar we have to use the version attribute
+ if (user === OC.getCurrentUser().uid) {
+ url = OC.generateUrl(
+ '/avatar/{user}/{size}?v={version}',
+ {
+ user: user,
+ size: Math.ceil(size * window.devicePixelRatio),
+ version: oc_userconfig.avatar.version
+ });
+ } else {
+ url = OC.generateUrl(
+ '/avatar/{user}/{size}',
+ {
+ user: user,
+ size: Math.ceil(size * window.devicePixelRatio)
+ });
+ }
// If the displayname is not defined we use the old code path
if (typeof(displayname) === 'undefined') {
@@ -122,7 +137,7 @@
$div.show();
$div.text('');
$div.append(img);
- }
+ };
img.width = size;
img.height = size;
diff --git a/core/templates/layout.user.php b/core/templates/layout.user.php
index 17f895bc17d..b5466b6fc05 100644
--- a/core/templates/layout.user.php
+++ b/core/templates/layout.user.php
@@ -66,8 +66,8 @@
<div class="avatardiv<?php if ($_['userAvatarSet']) { print_unescaped(' avatardiv-shown'); } else { print_unescaped('" style="display: none'); } ?>">
<?php if ($_['userAvatarSet']): ?>
<img alt="" width="32" height="32"
- src="<?php p(\OC::$server->getURLGenerator()->linkToRoute('core.avatar.getAvatar', ['userId' => $_['user_uid'], 'size' => 32]));?>"
- srcset="<?php p(\OC::$server->getURLGenerator()->linkToRoute('core.avatar.getAvatar', ['userId' => $_['user_uid'], 'size' => 64]));?> 2x, <?php p(\OC::$server->getURLGenerator()->linkToRoute('core.avatar.getAvatar', ['userId' => $_['user_uid'], 'size' => 128]));?> 4x"
+ src="<?php p(\OC::$server->getURLGenerator()->linkToRoute('core.avatar.getAvatar', ['userId' => $_['user_uid'], 'size' => 32, 'v' => $_['userAvatarVersion']]));?>"
+ srcset="<?php p(\OC::$server->getURLGenerator()->linkToRoute('core.avatar.getAvatar', ['userId' => $_['user_uid'], 'size' => 64, 'v' => $_['userAvatarVersion']]));?> 2x, <?php p(\OC::$server->getURLGenerator()->linkToRoute('core.avatar.getAvatar', ['userId' => $_['user_uid'], 'size' => 128, 'v' => $_['userAvatarVersion']]));?> 4x"
>
<?php endif; ?>
</div>