Moved Avatar retrieval into Avatar class

Backwards compatible functions are still in Profile class.
This commit is contained in:
Mikael Nordfeldth 2013-10-06 15:54:06 +02:00
parent c3d46b81a8
commit fb94a16217
10 changed files with 114 additions and 65 deletions

View File

@ -75,7 +75,7 @@ class AvatarbynicknameAction extends Action
try { try {
$avatar = Avatar::getUploaded($profile); $avatar = Avatar::getUploaded($profile);
$url = $avatar->displayUrl(); $url = $avatar->displayUrl();
} catch (Exception $e) { } catch (NoAvatarException $e) {
$url = Avatar::defaultImage(AVATAR_PROFILE_SIZE); $url = Avatar::defaultImage(AVATAR_PROFILE_SIZE);
} }
} else { } else {

View File

@ -136,7 +136,7 @@ class AvatarsettingsAction extends SettingsAction
'alt' => $user->nickname)); 'alt' => $user->nickname));
$this->elementEnd('div'); $this->elementEnd('div');
$this->elementEnd('li'); $this->elementEnd('li');
} catch (NoResultException $e) { } catch (NoAvatarException $e) {
// No original avatar found! // No original avatar found!
} }
@ -157,7 +157,7 @@ class AvatarsettingsAction extends SettingsAction
$this->submit('delete', _m('BUTTON','Delete')); $this->submit('delete', _m('BUTTON','Delete'));
} }
$this->elementEnd('li'); $this->elementEnd('li');
} catch (Exception $e) { } catch (NoAvatarException $e) {
// No previously uploaded avatar to preview. // No previously uploaded avatar to preview.
} }

View File

@ -157,7 +157,7 @@ class FoafAction extends Action
} }
$this->elementEnd('Image'); $this->elementEnd('Image');
$this->elementEnd('img'); $this->elementEnd('img');
} catch (Exception $e) { } catch (NoAvatarException $e) {
// No avatar for this user! // No avatar for this user!
} }

View File

@ -74,26 +74,69 @@ class Avatar extends Managed_DataObject
} }
$avatar->delete(); $avatar->delete();
} }
} catch (NoResultException $e) { } catch (NoAvatarException $e) {
// There are no avatars to delete, a sort of success. // There are no avatars to delete, a sort of success.
} }
return true; return true;
} }
static protected $_avatars = array();
/*
* Get an avatar by profile. Currently can't call newSize with $height
*/
public static function byProfile(Profile $target, $width=null, $height=null)
{
$width = (int) floor($width);
$height = !is_null($height) ? (int) floor($height) : null;
if (is_null($height)) {
$height = $width;
}
$size = "{$width}x{$height}";
if (!isset(self::$_avatars[$target->id])) {
self::$_avatars[$target->id] = array();
} elseif (isset(self::$_avatars[$target->id][$size])){
return self::$_avatars[$target->id][$size];
}
$avatar = null;
if (Event::handle('StartProfileGetAvatar', array($target, $width, &$avatar))) {
$avatar = self::pkeyGet(
array(
'profile_id' => $target->id,
'width' => $width,
'height' => $height,
)
);
Event::handle('EndProfileGetAvatar', array($target, $width, &$avatar));
}
if (is_null($avatar)) {
// Obviously we can't find an avatar, so let's resize the original!
$avatar = Avatar::newSize($target, $width);
} elseif (!($avatar instanceof Avatar)) {
throw new NoAvatarException($target, $avatar);
}
self::$_avatars[$target->id]["{$avatar->width}x{$avatar->height}"] = $avatar;
return $avatar;
}
public static function getUploaded(Profile $target) public static function getUploaded(Profile $target)
{ {
$avatar = new Avatar(); $avatar = new Avatar();
$avatar->profile_id = $target->id; $avatar->profile_id = $target->id;
$avatar->original = true; $avatar->original = true;
if (!$avatar->find(true)) { if (!$avatar->find(true)) {
throw new NoResultException($avatar); throw new NoAvatarException($target, $avatar);
} }
if (!file_exists(Avatar::path($avatar->filename))) { if (!file_exists(Avatar::path($avatar->filename))) {
// The delete call may be odd for, say, unmounted filesystems // The delete call may be odd for, say, unmounted filesystems
// that cause a file to currently not exist, but actually it does... // that cause a file to currently not exist, but actually it does...
$avatar->delete(); $avatar->delete();
throw new FileNotFoundException(Avatar::path($avatar->filename)); throw new NoAvatarException($target, $avatar);
} }
return $avatar; return $avatar;
} }
@ -102,7 +145,7 @@ class Avatar extends Managed_DataObject
$avatar = new Avatar(); $avatar = new Avatar();
$avatar->profile_id = $target->id; $avatar->profile_id = $target->id;
if (!$avatar->find()) { if (!$avatar->find()) {
throw new NoResultException($avatar); throw new NoAvatarException($target, $avatar);
} }
return $avatar->fetchAll(); return $avatar->fetchAll();
} }
@ -174,6 +217,14 @@ class Avatar extends Managed_DataObject
} }
} }
static function urlByProfile(Profile $target, $width=null, $height=null) {
try {
return self::byProfile($target, $width, $height)->displayUrl();
} catch (Exception $e) {
return self::defaultImage($width);
}
}
static function defaultImage($size) static function defaultImage($size)
{ {
static $sizenames = array(AVATAR_PROFILE_SIZE => 'profile', static $sizenames = array(AVATAR_PROFILE_SIZE => 'profile',
@ -182,9 +233,9 @@ class Avatar extends Managed_DataObject
return Theme::path('default-avatar-'.$sizenames[$size].'.png'); return Theme::path('default-avatar-'.$sizenames[$size].'.png');
} }
static function newSize(Profile $target, $size) { static function newSize(Profile $target, $width) {
$size = floor($size); $width = (int) floor($width);
if ($size < 1 || $size > common_config('avatar', 'maxsize')) { if ($width < 1 || $width > common_config('avatar', 'maxsize')) {
// TRANS: An error message when avatar size is unreasonable // TRANS: An error message when avatar size is unreasonable
throw new Exception(_m('Avatar size too large')); throw new Exception(_m('Avatar size too large'));
} }
@ -192,12 +243,12 @@ class Avatar extends Managed_DataObject
$original = Avatar::getUploaded($target); $original = Avatar::getUploaded($target);
$imagefile = new ImageFile($target->id, Avatar::path($original->filename)); $imagefile = new ImageFile($target->id, Avatar::path($original->filename));
$filename = $imagefile->resize($size); $filename = $imagefile->resize($width);
$scaled = clone($original); $scaled = clone($original);
$scaled->original = false; $scaled->original = false;
$scaled->width = $size; $scaled->width = $width;
$scaled->height = $size; $scaled->height = $width;
$scaled->url = Avatar::url($filename); $scaled->url = Avatar::url($filename);
$scaled->filename = $filename; $scaled->filename = $filename;
$scaled->created = common_sql_now(); $scaled->created = common_sql_now();

View File

@ -120,38 +120,7 @@ class Profile extends Managed_DataObject
public function getAvatar($width, $height=null) public function getAvatar($width, $height=null)
{ {
$width = (int) floor($width); return Avatar::byProfile($this, $width, $height);
if (is_null($height)) {
$height = $width;
}
if (isset($this->_avatars[$width])) {
return $this->_avatars[$width];
}
if (Event::handle('StartProfileGetAvatar', array($this, $width, &$avatar))) {
$avatar = Avatar::pkeyGet(
array(
'profile_id' => $this->id,
'width' => $width,
'height' => $height
)
);
Event::handle('EndProfileGetAvatar', array($this, $width, &$avatar));
}
if (is_null($avatar)) {
// Obviously we can't find an avatar, so let's resize the original!
$avatar = Avatar::newSize($this, $width);
} elseif (!($avatar instanceof Avatar)) {
throw new Exception('Bad Avatar retrieved');
}
// cache the avatar for future use
$this->_avatars[$width] = $avatar;
return $avatar;
} }
public function setOriginal($filename) public function setOriginal($filename)
@ -574,13 +543,7 @@ class Profile extends Managed_DataObject
function avatarUrl($size=AVATAR_PROFILE_SIZE) function avatarUrl($size=AVATAR_PROFILE_SIZE)
{ {
$size = floor($size); return Avatar::urlByProfile($this, $size);
try {
$avatar = $this->getAvatar($size);
return $avatar->displayUrl();
} catch (Exception $e) {
return Avatar::defaultImage($size);
}
} }
function getSubscribed($offset=0, $limit=null) function getSubscribed($offset=0, $limit=null)

View File

@ -460,9 +460,9 @@ class ActivityObject
$object->link = $profile->profileurl; $object->link = $profile->profileurl;
try { try {
$orig = Avatar::getUploaded($profile); $avatar = Avatar::getUploaded($profile);
$object->avatarLinks[] = AvatarLink::fromAvatar($orig); $object->avatarLinks[] = AvatarLink::fromAvatar($avatar);
} catch (Exception $e) { } catch (NoAvatarException $e) {
// Could not find an original avatar to link // Could not find an original avatar to link
} }
@ -475,9 +475,9 @@ class ActivityObject
foreach ($sizes as $size) { foreach ($sizes as $size) {
$alink = null; $alink = null;
try { try {
$avatar = $profile->getAvatar($size); $avatar = Avatar::byProfile($profile, $size);
$alink = AvatarLink::fromAvatar($avatar); $alink = AvatarLink::fromAvatar($avatar);
} catch (Exception $e) { } catch (NoAvatarException $e) {
$alink = new AvatarLink(); $alink = new AvatarLink();
$alink->type = 'image/png'; $alink->type = 'image/png';
$alink->height = $size; $alink->height = $size;

View File

@ -58,11 +58,8 @@ class AvatarLink
} }
} }
static function fromAvatar($avatar) static function fromAvatar(Avatar $avatar)
{ {
if (empty($avatar)) {
return null;
}
$alink = new AvatarLink(); $alink = new AvatarLink();
$alink->type = $avatar->mediatype; $alink->type = $avatar->mediatype;
$alink->height = $avatar->height; $alink->height = $avatar->height;

38
lib/noavatarexception.php Normal file
View File

@ -0,0 +1,38 @@
<?php
/**
* StatusNet, the distributed open-source microblogging tool
*
* Class for an exception when unable to retrieve or resize a profile's avatar.
*
* PHP version 5
*
* LICENCE: This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @category Exception
* @package GNUSocial
* @author Mikael Nordfeldth <mmn@hethane.se>
* @copyright 2013 Free Software Foundation, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
* @link http://www.gnu.org/software/social/
*/
if (!defined('GNUSOCIAL')) { exit(1); }
class NoAvatarException extends NoResultException
{
public function __construct(Profile $target, Avatar $avatar)
{
parent::__construct($avatar);
}
}

View File

@ -34,6 +34,6 @@ class NoResultException extends ServerException
public function __construct(DB_DataObject $obj) public function __construct(DB_DataObject $obj)
{ {
// We could log an entry here with the search parameters // We could log an entry here with the search parameters
parent::__construct(_('No result found on lookup.')); parent::__construct(sprintf(_('No result found on %s lookup.'), get_class($obj)));
} }
} }

View File

@ -320,8 +320,8 @@ class TwitterImport
common_debug(__METHOD__ . " - Updating profile avatar (profile_id={$profile->id}) " . common_debug(__METHOD__ . " - Updating profile avatar (profile_id={$profile->id}) " .
"from {$avatar->filename} to {$filename}"); "from {$avatar->filename} to {$filename}");
// else we continue with creating a new avatar // else we continue with creating a new avatar
} catch (Exception $e) { } catch (NoAvatarException $e) {
// Avatar was not found. We can catch NoResultException or FileNotFoundException // Avatar was not found. We can catch NoAvatarException or FileNotFoundException
// but generally we just want to continue creating a new avatar. // but generally we just want to continue creating a new avatar.
common_debug(__METHOD__ . " - No avatar found for (profile_id={$profile->id})"); common_debug(__METHOD__ . " - No avatar found for (profile_id={$profile->id})");
} }