1
0
mirror of https://github.com/owncloudarchive/contacts.git synced 2025-01-18 07:52:21 +01:00

Move cacheThumbnail to Properties. Fix #252

This commit is contained in:
Thomas Tanghus 2013-11-07 14:06:12 +01:00
parent 4f8635c0e2
commit aa8a51d9a7
7 changed files with 103 additions and 58 deletions

View File

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

View File

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

View File

@ -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(

View File

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

View File

@ -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) {

View File

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

View File

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