summaryrefslogtreecommitdiffstats
path: root/apps
diff options
context:
space:
mode:
authorThomas Tanghus <thomas@tanghus.net>2012-08-09 16:31:04 +0200
committerThomas Tanghus <thomas@tanghus.net>2012-08-09 16:32:05 +0200
commit39814edf81ede7413d98eaff86b183c42b4d8287 (patch)
tree300d4e31a400fa985efb7b3a90e37e8a6484616f /apps
parent00d7ae8319ad0b48b31df4080fe7da7a848de28d (diff)
downloadnextcloud-server-39814edf81ede7413d98eaff86b183c42b4d8287.tar.gz
nextcloud-server-39814edf81ede7413d98eaff86b183c42b4d8287.zip
Another take at profile photo/thumbnail caching. oc-839.
Diffstat (limited to 'apps')
-rw-r--r--apps/contacts/ajax/contact/add.php8
-rw-r--r--apps/contacts/ajax/contact/addproperty.php7
-rw-r--r--apps/contacts/ajax/contact/deleteproperty.php7
-rw-r--r--apps/contacts/ajax/contact/details.php1
-rw-r--r--apps/contacts/ajax/contact/saveproperty.php5
-rw-r--r--apps/contacts/ajax/savecrop.php31
-rw-r--r--apps/contacts/js/contacts.js8
-rw-r--r--apps/contacts/lib/app.php12
-rw-r--r--apps/contacts/photo.php28
-rw-r--r--apps/contacts/thumbnail.php36
10 files changed, 96 insertions, 47 deletions
diff --git a/apps/contacts/ajax/contact/add.php b/apps/contacts/ajax/contact/add.php
index 6aaf5a9df35..043e947dc4b 100644
--- a/apps/contacts/ajax/contact/add.php
+++ b/apps/contacts/ajax/contact/add.php
@@ -49,4 +49,10 @@ if(!$id) {
exit();
}
-OCP\JSON::success(array('data' => array( 'id' => $id, 'aid' => $aid )));
+OCP\JSON::success(array(
+ 'data' => array(
+ 'id' => $id,
+ 'aid' => $aid,
+ 'lastmodified' => OC_Contacts_VCard::lastModified($vcard)->format('U')
+ )
+));
diff --git a/apps/contacts/ajax/contact/addproperty.php b/apps/contacts/ajax/contact/addproperty.php
index ca28140edf1..df064367ef1 100644
--- a/apps/contacts/ajax/contact/addproperty.php
+++ b/apps/contacts/ajax/contact/addproperty.php
@@ -144,4 +144,9 @@ if(!OC_Contacts_VCard::edit($id, $vcard)) {
bailOut($l10n->t('Error adding contact property: '.$name));
}
-OCP\JSON::success(array('data' => array( 'checksum' => $checksum )));
+OCP\JSON::success(array(
+ 'data' => array(
+ 'checksum' => $checksum,
+ 'lastmodified' => OC_Contacts_VCard::lastModified($vcard)->format('U'))
+ )
+);
diff --git a/apps/contacts/ajax/contact/deleteproperty.php b/apps/contacts/ajax/contact/deleteproperty.php
index 9c3f09f8610..d7545ff1fbf 100644
--- a/apps/contacts/ajax/contact/deleteproperty.php
+++ b/apps/contacts/ajax/contact/deleteproperty.php
@@ -44,4 +44,9 @@ if(!OC_Contacts_VCard::edit($id, $vcard)) {
bailOut($l10n->t('Error deleting contact property.'));
}
-OCP\JSON::success(array('data' => array( 'id' => $id )));
+OCP\JSON::success(array(
+ 'data' => array(
+ 'id' => $id,
+ 'lastmodified' => OC_Contacts_VCard::lastModified($vcard)->format('U'),
+ )
+));
diff --git a/apps/contacts/ajax/contact/details.php b/apps/contacts/ajax/contact/details.php
index 0e07f9ec3d8..c22f18937d7 100644
--- a/apps/contacts/ajax/contact/details.php
+++ b/apps/contacts/ajax/contact/details.php
@@ -53,5 +53,6 @@ if(isset($details['PHOTO'])) {
$details['id'] = $id;
$details['displayname'] = $card['fullname'];
$details['addressbookid'] = $card['addressbookid'];
+$details['lastmodified'] = OC_Contacts_App::lastModified($vcard)->format('U');
OC_Contacts_App::setLastModifiedHeader($vcard);
OCP\JSON::success(array('data' => $details));
diff --git a/apps/contacts/ajax/contact/saveproperty.php b/apps/contacts/ajax/contact/saveproperty.php
index f9880c78834..799038b6f1d 100644
--- a/apps/contacts/ajax/contact/saveproperty.php
+++ b/apps/contacts/ajax/contact/saveproperty.php
@@ -148,5 +148,6 @@ if(!OC_Contacts_VCard::edit($id, $vcard)) {
OCP\JSON::success(array('data' => array(
'line' => $line,
'checksum' => $checksum,
- 'oldchecksum' => $_POST['checksum']))
-);
+ 'oldchecksum' => $_POST['checksum']
+ 'lastmodified' => OC_Contacts_VCard::lastModified($vcard)->format('U')
+));
diff --git a/apps/contacts/ajax/savecrop.php b/apps/contacts/ajax/savecrop.php
index 1df5299c7a6..2483d0f7128 100644
--- a/apps/contacts/ajax/savecrop.php
+++ b/apps/contacts/ajax/savecrop.php
@@ -62,17 +62,17 @@ if($data) {
if($image->crop($x1, $y1, $w, $h)) {
if(($image->width() <= 200 && $image->height() <= 200)
|| $image->resize(200)) {
- $card = OC_Contacts_App::getContactVCard($id);
- if(!$card) {
+ $vcard = OC_Contacts_App::getContactVCard($id);
+ if(!$vcard) {
OC_Cache::remove($tmpkey);
bailOut(OC_Contacts_App::$l10n
->t('Error getting contact object.'));
}
- if($card->__isset('PHOTO')) {
+ if($vcard->__isset('PHOTO')) {
OCP\Util::writeLog('contacts',
'savecrop.php: PHOTO property exists.',
OCP\Util::DEBUG);
- $property = $card->__get('PHOTO');
+ $property = $vcard->__get('PHOTO');
if(!$property) {
OC_Cache::remove($tmpkey);
bailOut(OC_Contacts_App::$l10n
@@ -83,27 +83,28 @@ if($data) {
= new Sabre_VObject_Parameter('ENCODING', 'b');
$property->parameters[]
= new Sabre_VObject_Parameter('TYPE', $image->mimeType());
- $card->__set('PHOTO', $property);
+ $vcard->__set('PHOTO', $property);
} else {
OCP\Util::writeLog('contacts',
'savecrop.php: files: Adding PHOTO property.',
OCP\Util::DEBUG);
- $card->addProperty('PHOTO',
+ $vcard->addProperty('PHOTO',
$image->__toString(), array('ENCODING' => 'b',
'TYPE' => $image->mimeType()));
}
$now = new DateTime;
- $card->setString('REV', $now->format(DateTime::W3C));
- if(!OC_Contacts_VCard::edit($id, $card)) {
+ $vcard->setString('REV', $now->format(DateTime::W3C));
+ if(!OC_Contacts_VCard::edit($id, $vcard)) {
bailOut(OC_Contacts_App::$l10n->t('Error saving contact.'));
}
- $tmpl = new OCP\Template("contacts", "part.contactphoto");
- $tmpl->assign('id', $id);
- $tmpl->assign('refresh', true);
- $tmpl->assign('width', $image->width());
- $tmpl->assign('height', $image->height());
- $page = $tmpl->fetchPage();
- OCP\JSON::success(array('data' => array('page'=>$page)));
+ OCP\JSON::success(array(
+ 'data' => array(
+ 'id' => $id,
+ 'width' => $image->width(),
+ 'height' => $image->height(),
+ 'lastmodified' => OC_Contacts_App::lastModified($vcard)->format('U')
+ )
+ ));
} else {
bailOut(OC_Contacts_App::$l10n->t('Error resizing image'));
}
diff --git a/apps/contacts/js/contacts.js b/apps/contacts/js/contacts.js
index 9aec02557fc..67bfa9847ee 100644
--- a/apps/contacts/js/contacts.js
+++ b/apps/contacts/js/contacts.js
@@ -835,7 +835,7 @@ OC.Contacts={
OC.Contacts.propertyContainerFor(obj).data('checksum', '');
if(proptype == 'PHOTO') {
OC.Contacts.Contacts.refreshThumbnail(OC.Contacts.Card.id);
- OC.Contacts.Card.loadPhoto(true);
+ OC.Contacts.Card.loadPhoto();
} else if(proptype == 'NOTE') {
$('#note').find('textarea').val('');
OC.Contacts.propertyContainerFor(obj).hide();
@@ -1218,9 +1218,9 @@ OC.Contacts={
}
});
},
- loadPhoto:function(refresh){
+ loadPhoto:function(){
var self = this;
- var refreshstr = (refresh?'&refresh=1'+Math.random():'')
+ var refreshstr = ''; //'&refresh='+Math.random();
$('#phototools li a').tipsy('hide');
var wrapper = $('#contacts_details_photo_wrapper');
wrapper.addClass('loading').addClass('wait');
@@ -1278,7 +1278,7 @@ OC.Contacts={
var response=jQuery.parseJSON(target.contents().text());
if(response != undefined && response.status == 'success'){
// load cropped photo.
- self.loadPhoto(true);
+ self.loadPhoto();
OC.Contacts.Card.data.PHOTO = true;
}else{
OC.dialogs.alert(response.data.message, t('contacts', 'Error'));
diff --git a/apps/contacts/lib/app.php b/apps/contacts/lib/app.php
index 3972d0e0a5e..b59a8372b74 100644
--- a/apps/contacts/lib/app.php
+++ b/apps/contacts/lib/app.php
@@ -240,6 +240,18 @@ class OC_Contacts_App {
self::getVCategories()->loadFromVObject($contact, true);
}
+ /**
+ * @brief Get the last modification time.
+ * @param $vcard OC_VObject
+ * $return DateTime | null
+ */
+ public static function lastModified($vcard) {
+ $rev = $vcard->getAsString('REV');
+ if ($rev) {
+ return DateTime::createFromFormat(DateTime::W3C, $rev);
+ }
+ }
+
public static function setLastModifiedHeader($contact) {
$rev = $contact->getAsString('REV');
if ($rev) {
diff --git a/apps/contacts/photo.php b/apps/contacts/photo.php
index efdf157cd95..f5c8e6fe4bd 100644
--- a/apps/contacts/photo.php
+++ b/apps/contacts/photo.php
@@ -20,14 +20,15 @@ function getStandardImage() {
}
$id = isset($_GET['id']) ? $_GET['id'] : null;
-$caching = isset($_GET['refresh']) ? 0 : null;
+$etag = null;
+$caching = null;
if(is_null($id)) {
getStandardImage();
}
if(!extension_loaded('gd') || !function_exists('gd_info')) {
- OCP\Util::writeLog('contacts',
+ OCP\Util::writeLog('contacts',
'photo.php. GD module not installed', OCP\Util::DEBUG);
getStandardImage();
}
@@ -39,25 +40,34 @@ if (!$image) {
}
// invalid vcard
if (is_null($contact)) {
- OCP\Util::writeLog('contacts',
- 'photo.php. The VCard for ID ' . $id . ' is not RFC compatible',
+ OCP\Util::writeLog('contacts',
+ 'photo.php. The VCard for ID ' . $id . ' is not RFC compatible',
OCP\Util::ERROR);
} else {
- OCP\Response::enableCaching($caching);
- OC_Contacts_App::setLastModifiedHeader($contact);
-
// Photo :-)
if ($image->loadFromBase64($contact->getAsString('PHOTO'))) {
// OK
- OCP\Response::setETagHeader(md5($contact->getAsString('PHOTO')));
+ $etag = md5($contact->getAsString('PHOTO'));
}
else
// Logo :-/
if ($image->loadFromBase64($contact->getAsString('LOGO'))) {
// OK
- OCP\Response::setETagHeader(md5($contact->getAsString('LOGO')));
+ $etag = md5($contact->getAsString('LOGO'));
}
if ($image->valid()) {
+ $modified = OC_Contacts_App::lastModified($contact);
+ // Force refresh if modified within the last minute.
+ if(!is_null($modified)) {
+ $caching = (time() - $modified->format('U') > 60) ? null : 0;
+ }
+ OCP\Response::enableCaching($caching);
+ if(!is_null($modified)) {
+ OCP\Response::setLastModifiedHeader($modified);
+ }
+ if($etag) {
+ OCP\Response::setETagHeader($etag);
+ }
$max_size = 200;
if ($image->width() > $max_size || $image->height() > $max_size) {
$image->resize($max_size);
diff --git a/apps/contacts/thumbnail.php b/apps/contacts/thumbnail.php
index 6deb5ca379e..1e3714ae6c6 100644
--- a/apps/contacts/thumbnail.php
+++ b/apps/contacts/thumbnail.php
@@ -26,27 +26,26 @@ OCP\App::checkAppEnabled('contacts');
session_write_close();
function getStandardImage() {
- //OCP\Response::setExpiresHeader('P10D');
OCP\Response::enableCaching();
OCP\Response::redirect(OCP\Util::imagePath('contacts', 'person.png'));
}
if(!extension_loaded('gd') || !function_exists('gd_info')) {
- OCP\Util::writeLog('contacts',
+ OCP\Util::writeLog('contacts',
'thumbnail.php. GD module not installed', OCP\Util::DEBUG);
getStandardImage();
exit();
}
$id = $_GET['id'];
-$caching = isset($_GET['refresh']) ? 0 : null;
+$caching = null;
$contact = OC_Contacts_App::getContactVCard($id);
// invalid vcard
if(is_null($contact)) {
- OCP\Util::writeLog('contacts',
- 'thumbnail.php. The VCard for ID ' . $id . ' is not RFC compatible',
+ OCP\Util::writeLog('contacts',
+ 'thumbnail.php. The VCard for ID ' . $id . ' is not RFC compatible',
OCP\Util::ERROR);
getStandardImage();
exit();
@@ -60,30 +59,39 @@ $thumbnail_size = 23;
$image = new OC_Image();
$photo = $contact->getAsString('PHOTO');
if($photo) {
- OCP\Response::setETagHeader(md5($photo));
if($image->loadFromBase64($photo)) {
if($image->centerCrop()) {
if($image->resize($thumbnail_size)) {
+ $modified = OC_Contacts_App::lastModified($contact);
+ // Force refresh if modified within the last minute.
+ if(!is_null($modified)) {
+ $caching = (time() - $modified->format('U') > 60) ? null : 0;
+ }
+ OCP\Response::enableCaching($caching);
+ if(!is_null($modified)) {
+ OCP\Response::setLastModifiedHeader($modified);
+ }
+ OCP\Response::setETagHeader(md5($photo));
if($image->show()) {
exit();
} else {
- OCP\Util::writeLog('contacts',
- 'thumbnail.php. Couldn\'t display thumbnail for ID ' . $id,
+ OCP\Util::writeLog('contacts',
+ 'thumbnail.php. Couldn\'t display thumbnail for ID ' . $id,
OCP\Util::ERROR);
}
} else {
- OCP\Util::writeLog('contacts',
- 'thumbnail.php. Couldn\'t resize thumbnail for ID ' . $id,
+ OCP\Util::writeLog('contacts',
+ 'thumbnail.php. Couldn\'t resize thumbnail for ID ' . $id,
OCP\Util::ERROR);
}
}else{
- OCP\Util::writeLog('contacts',
- 'thumbnail.php. Couldn\'t crop thumbnail for ID ' . $id,
+ OCP\Util::writeLog('contacts',
+ 'thumbnail.php. Couldn\'t crop thumbnail for ID ' . $id,
OCP\Util::ERROR);
}
} else {
- OCP\Util::writeLog('contacts',
- 'thumbnail.php. Couldn\'t load image string for ID ' . $id,
+ OCP\Util::writeLog('contacts',
+ 'thumbnail.php. Couldn\'t load image string for ID ' . $id,
OCP\Util::ERROR);
}
}