From aa8a51d9a7cb9691de664bdc546df052a36fc08d Mon Sep 17 00:00:00 2001 From: Thomas Tanghus Date: Thu, 7 Nov 2013 14:06:12 +0100 Subject: [PATCH] Move cacheThumbnail to Properties. Fix #252 --- lib/backend/database.php | 12 +++-- lib/contact.php | 13 +++++- lib/controller/contactphotocontroller.php | 9 +++- lib/hooks.php | 15 +++++-- lib/utils/jsonserializer.php | 11 ++++- lib/utils/properties.php | 55 +++++++++++++++++++++++ lib/vobject/vcard.php | 46 ------------------- 7 files changed, 103 insertions(+), 58 deletions(-) diff --git a/lib/backend/database.php b/lib/backend/database.php index de23384f..e9d029f4 100644 --- a/lib/backend/database.php +++ b/lib/backend/database.php @@ -554,7 +554,7 @@ class Database extends AbstractBackend { $this->touchAddressBook($addressbookid); \OCP\Util::emitHook('OCA\Contacts', 'post_createContact', - array('id' => $newid, 'parent' => $addressbookid, 'contact' => $contact) + array('id' => $newid, 'parent' => $addressbookid, 'backend' => $this->name, 'contact' => $contact) ); return (string)$newid; } @@ -564,7 +564,7 @@ class Database extends AbstractBackend { * * @param string $addressbookid * @param mixed $id Contact ID - * @param mixed $contact + * @param VCard|string $contact * @param array $options - Optional (backend specific options) * @see getContact * @return bool @@ -639,7 +639,13 @@ class Database extends AbstractBackend { $this->touchAddressBook($addressbookid); if(!$isBatch) { \OCP\Util::emitHook('OCA\Contacts', 'post_updateContact', - array('id' => $id, 'parent' => $addressbookid, 'contact' => $contact, 'carddav' => $isCardDAV) + array( + 'backend' => $this->name, + 'addressBookId' => $addressbookid, + 'contactId' => $id, + 'contact' => $contact, + 'carddav' => $isCardDAV + ) ); } return true; diff --git a/lib/contact.php b/lib/contact.php index 3dea6c16..5b5c7699 100644 --- a/lib/contact.php +++ b/lib/contact.php @@ -22,7 +22,8 @@ namespace OCA\Contacts; -use Sabre\VObject\Property; +use Sabre\VObject\Property, + OCA\Contacts\Utils\Properties; /** * Subclass this class or implement IPIMObject interface for PIM objects @@ -705,7 +706,14 @@ class Contact extends VObject\VCard implements IPIMObject { } parent::__unset($key); if($key === 'PHOTO') { - $this->cacheThumbnail(null, true); + Properties::cacheThumbnail( + $this->getBackend()->name, + $this->getParent()->getId(), + $this->getId(), + null, + null, + array('remove' => true) + ); } $this->setSaved(false); } @@ -765,4 +773,5 @@ class Contact extends VObject\VCard implements IPIMObject { return $vcal; } } + } diff --git a/lib/controller/contactphotocontroller.php b/lib/controller/contactphotocontroller.php index 09209432..0ba65006 100644 --- a/lib/controller/contactphotocontroller.php +++ b/lib/controller/contactphotocontroller.php @@ -331,7 +331,14 @@ class ContactPhotoController extends Controller { if(!$contact->save()) { return $response->bailOut(App::$l10n->t('Error saving contact.')); } - $thumbnail = $contact->cacheThumbnail($image); + + $thumbnail = Properties::cacheThumbnail( + $params['backend'], + $params['addressBookId'] + $params['contactId'], + $image + ); + $response->setData(array( 'status' => 'success', 'data' => array( diff --git a/lib/hooks.php b/lib/hooks.php index 3556da1e..3fbb99c7 100644 --- a/lib/hooks.php +++ b/lib/hooks.php @@ -117,16 +117,23 @@ class Hooks{ public static function contactUpdated($parameters) { //\OCP\Util::writeLog('contacts', __METHOD__.' parameters: '.print_r($parameters, true), \OCP\Util::DEBUG); $contact = $parameters['contact']; - Utils\Properties::updateIndex($parameters['id'], $contact); + Utils\Properties::updateIndex($parameters['contactId'], $contact); // If updated via CardDAV we don't know if PHOTO has changed if(isset($parameters['carddav']) && $parameters['carddav']) { if(isset($contact->PHOTO) || isset($contact->LOGO)) { - $contact->cacheThumbnail(null, false, true); + Utils\Properties::cacheThumbnail( + $parameters['backend'], + $parameters['addressBookId'], + $parameters['contactId'], + null, + $contact, + array('update' => true) + ); } $tagMgr = \OC::$server->getTagManager()->load('contact'); - $tagMgr->purgeObjects(array($parameters['id'])); + $tagMgr->purgeObjects(array($parameters['contactId'])); if(isset($contact->CATEGORIES)) { - $tagMgr->addMultiple($contact->CATEGORIES->getParts(), true, $parameters['id']); + $tagMgr->addMultiple($contact->CATEGORIES->getParts(), true, $parameters['contactId']); } } } diff --git a/lib/utils/jsonserializer.php b/lib/utils/jsonserializer.php index 52dd7fcf..03c13054 100644 --- a/lib/utils/jsonserializer.php +++ b/lib/utils/jsonserializer.php @@ -24,7 +24,8 @@ namespace OCA\Contacts\Utils; use OCA\Contacts\VObject; -use OCA\Contacts\Contact; +use OCA\Contacts\Contact, + OCA\Contacts\Utils\Properties; /** * This class serializes properties, components an @@ -90,7 +91,13 @@ class JSONSerializer { $details = array(); if(isset($contact->PHOTO) || isset($contact->LOGO)) { - $details['thumbnail'] = $contact->cacheThumbnail(); + $details['thumbnail'] = Properties::cacheThumbnail( + $contact->getBackend()->name, + $contact->getParent()->getId(), + $contact->getId(), + null, + $contact + ); } foreach($contact->children as $property) { diff --git a/lib/utils/properties.php b/lib/utils/properties.php index 04bdc3b7..09402ab9 100644 --- a/lib/utils/properties.php +++ b/lib/utils/properties.php @@ -22,10 +22,15 @@ namespace OCA\Contacts\Utils; +use OCA\Contacts\App; + Properties::$l10n = \OCP\Util::getL10N('contacts'); Class Properties { + const THUMBNAIL_PREFIX = 'contact-thumbnail-'; + const THUMBNAIL_SIZE = 28; + private static $deleteindexstmt; private static $updateindexstmt; protected static $cardsTableName = '*PREFIX*contacts_cards'; @@ -276,4 +281,54 @@ Class Properties { } } } + + public static function cacheThumbnail($backendName, $addressBookId, $contactId, + \OCP\Image $image = null, $vcard = null, $options = array()) { + $cache = \OC::$server->getCache(); + $key = self::THUMBNAIL_PREFIX . $backendName . '::' . $addressBookId . '::' . $contactId; + //$cache->remove($key); + if($cache->hasKey($key) && $image === null + && (isset($options['remove']) && $options['remove'] === false) + && (isset($options['update']) && $options['update'] === false)) { + return $cache->get($key); + } + if(isset($options['remove']) && $options['remove']) { + $cache->remove($key); + if(!isset($options['update']) || !$options['update']) { + return false; + } + } + if(is_null($image)) { + if(is_null($vcard)) { + $app = new App(); + $vcard = $app->getContact($backendName, $addressBookId, $contactId); + } + $image = new \OCP\Image(); + if(!isset($vcard->PHOTO) && !isset($vcard->LOGO)) { + return false; + } + if(!$image->loadFromBase64((string)$vcard->PHOTO)) { + if(!$image->loadFromBase64((string)$vcard->LOGO)) { + return false; + } + } + } + if(!$image->centerCrop()) { + \OCP\Util::writeLog('contacts', + __METHOD__ .'. Couldn\'t crop thumbnail for ID ' . $key, + \OCP\Util::ERROR); + return false; + } + if(!$image->resize(self::THUMBNAIL_SIZE)) { + \OCP\Util::writeLog('contacts', + __METHOD__ . '. Couldn\'t resize thumbnail for ID ' . $key, + \OCP\Util::ERROR); + return false; + } + // Cache as base64 for around a month + $cache->set($key, strval($image), 3000000); + \OCP\Util::writeLog('contacts', 'Caching ' . $key, \OCP\Util::DEBUG); + return $cache->get($key); + } + } diff --git a/lib/vobject/vcard.php b/lib/vobject/vcard.php index 128d80ea..3ac3ff4e 100644 --- a/lib/vobject/vcard.php +++ b/lib/vobject/vcard.php @@ -35,9 +35,6 @@ use Sabre\VObject; */ class VCard extends VObject\Component\VCard { - const THUMBNAIL_PREFIX = 'contact-thumbnail-'; - const THUMBNAIL_SIZE = 28; - /** * The following constants are used by the validate() method. */ @@ -308,47 +305,4 @@ class VCard extends VObject\Component\VCard { return $this->groups; } - // TODO: Cleanup these parameters and move method to Utils class - public function cacheThumbnail(\OCP\Image $image = null, $remove = false, $update = false) { - $key = self::THUMBNAIL_PREFIX . $this->combinedKey(); - //\OC_Cache::remove($key); - if(\OC_Cache::hasKey($key) && $image === null && $remove === false && $update === false) { - return \OC_Cache::get($key); - } - if($remove) { - \OC_Cache::remove($key); - if(!$update) { - return false; - } - } - if(is_null($image)) { - $this->retrieve(); - $image = new \OCP\Image(); - if(!isset($this->PHOTO) && !isset($this->LOGO)) { - return false; - } - if(!$image->loadFromBase64((string)$this->PHOTO)) { - if(!$image->loadFromBase64((string)$this->LOGO)) { - return false; - } - } - } - if(!$image->centerCrop()) { - \OCP\Util::writeLog('contacts', - __METHOD__ .'. Couldn\'t crop thumbnail for ID ' . $key, - \OCP\Util::ERROR); - return false; - } - if(!$image->resize(self::THUMBNAIL_SIZE)) { - \OCP\Util::writeLog('contacts', - __METHOD__ . '. Couldn\'t resize thumbnail for ID ' . $key, - \OCP\Util::ERROR); - return false; - } - // Cache as base64 for around a month - \OC_Cache::set($key, strval($image), 3000000); - \OCP\Util::writeLog('contacts', 'Caching ' . $key, \OCP\Util::DEBUG); - return \OC_Cache::get($key); - } - } \ No newline at end of file