Use Model\Photo as much as possible

Most important is to use `Photo::delete()` to have backend
to remove data.
This commit is contained in:
fabrixxm 2018-12-11 20:03:29 +01:00 committed by Hypolite Petovan
parent d549787bc1
commit 8c75c26361
8 changed files with 39 additions and 56 deletions

View File

@ -3961,7 +3961,7 @@ function api_fr_photoalbum_delete($type)
} }
// now let's delete all photos from the album // now let's delete all photos from the album
$result = DBA::delete('photo', ['uid' => api_user(), 'album' => $album]); $result = Photo::delete(['uid' => api_user(), 'album' => $album]);
// return success of deletion or error message // return success of deletion or error message
if ($result) { if ($result) {
@ -3995,11 +3995,11 @@ function api_fr_photoalbum_update($type)
throw new BadRequestException("no new albumname specified"); throw new BadRequestException("no new albumname specified");
} }
// check if album is existing // check if album is existing
if (!DBA::exists('photo', ['uid' => api_user(), 'album' => $album])) { if (!Photo::exists(null, ['uid' => api_user(), 'album' => $album])) {
throw new BadRequestException("album not available"); throw new BadRequestException("album not available");
} }
// now let's update all photos to the albumname // now let's update all photos to the albumname
$result = DBA::update('photo', ['album' => $album_new], ['uid' => api_user(), 'album' => $album]); $result = Photo::update(['album' => $album_new], ['uid' => api_user(), 'album' => $album]);
// return success of updating or error message // return success of updating or error message
if ($result) { if ($result) {
@ -4099,14 +4099,8 @@ function api_fr_photo_create_update($type)
} else { } else {
$mode = "update"; $mode = "update";
// check if photo is existing in database // check if photo is existing in databasei
$r = q( if (!Photo::exists($photo_id, ['uid' => api_user(), 'album' => $album]) {
"SELECT `id` FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s' AND `album` = '%s'",
intval(api_user()),
DBA::escape($photo_id),
DBA::escape($album)
);
if (!DBA::isResult($r)) {
throw new BadRequestException("photo not available"); throw new BadRequestException("photo not available");
} }
} }
@ -4135,47 +4129,40 @@ function api_fr_photo_create_update($type)
// now let's do the changes in update-mode // now let's do the changes in update-mode
if ($mode == "update") { if ($mode == "update") {
$sql_extra = ""; $updated_fields = [];
if (!is_null($desc)) { if (!is_null($desc)) {
$sql_extra .= (($sql_extra != "") ? " ," : "") . "`desc` = '$desc'"; $updated_fields['desc'] = $desc;
} }
if (!is_null($album_new)) { if (!is_null($album_new)) {
$sql_extra .= (($sql_extra != "") ? " ," : "") . "`album` = '$album_new'"; $updated_fields['album'] = $album_new;
} }
if (!is_null($allow_cid)) { if (!is_null($allow_cid)) {
$allow_cid = trim($allow_cid); $allow_cid = trim($allow_cid);
$sql_extra .= (($sql_extra != "") ? " ," : "") . "`allow_cid` = '$allow_cid'"; $updated_fields['allow_cid'] = $allow_cid;
} }
if (!is_null($deny_cid)) { if (!is_null($deny_cid)) {
$deny_cid = trim($deny_cid); $deny_cid = trim($deny_cid);
$sql_extra .= (($sql_extra != "") ? " ," : "") . "`deny_cid` = '$deny_cid'"; $updated_fields['deny_cid'] = $deny_cid;
} }
if (!is_null($allow_gid)) { if (!is_null($allow_gid)) {
$allow_gid = trim($allow_gid); $allow_gid = trim($allow_gid);
$sql_extra .= (($sql_extra != "") ? " ," : "") . "`allow_gid` = '$allow_gid'"; $updated_fields['allow_gid'] = $allow_gid;
} }
if (!is_null($deny_gid)) { if (!is_null($deny_gid)) {
$deny_gid = trim($deny_gid); $deny_gid = trim($deny_gid);
$sql_extra .= (($sql_extra != "") ? " ," : "") . "`deny_gid` = '$deny_gid'"; $updated_fields['deny_gid'] = $deny_gid;
} }
$result = false; $result = false;
if ($sql_extra != "") { if (count($updated_fields) > 0) {
$nothingtodo = false; $nothingtodo = false;
$result = q( $result = Photo::update($updated_fields, ['uid' => api_user(), 'resource-id' => $photo_id, 'album' => $album]);
"UPDATE `photo` SET %s, `edited`='%s' WHERE `uid` = %d AND `resource-id` = '%s' AND `album` = '%s'",
$sql_extra,
DateTimeFormat::utcNow(), // update edited timestamp
intval(api_user()),
DBA::escape($photo_id),
DBA::escape($album)
);
} else { } else {
$nothingtodo = true; $nothingtodo = true;
} }
@ -4224,16 +4211,12 @@ function api_fr_photo_delete($type)
throw new BadRequestException("no photo_id specified"); throw new BadRequestException("no photo_id specified");
} }
// check if photo is existing in database // check if photo is existing in database
$r = q( $r = Photo::exists($photo_id, ['uid' => api_user()]);
"SELECT `id` FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s'", if (!$r) {
intval(api_user()),
DBA::escape($photo_id)
);
if (!DBA::isResult($r)) {
throw new BadRequestException("photo not available"); throw new BadRequestException("photo not available");
} }
// now we can perform on the deletion of the photo // now we can perform on the deletion of the photo
$result = DBA::delete('photo', ['uid' => api_user(), 'resource-id' => $photo_id]); $result = Photo::delete(['uid' => api_user(), 'resource-id' => $photo_id]);
// return success of deletion or error message // return success of deletion or error message
if ($result) { if ($result) {
@ -4343,7 +4326,7 @@ function api_account_update_profile_image($type)
// change specified profile or all profiles to the new resource-id // change specified profile or all profiles to the new resource-id
if ($is_default_profile) { if ($is_default_profile) {
$condition = ["`profile` AND `resource-id` != ? AND `uid` = ?", $data['photo']['id'], api_user()]; $condition = ["`profile` AND `resource-id` != ? AND `uid` = ?", $data['photo']['id'], api_user()];
DBA::update('photo', ['profile' => false], $condition); Photo::update(['profile' => false], $condition);
} else { } else {
$fields = ['photo' => System::baseUrl() . '/photo/' . $data['photo']['id'] . '-4.' . $filetype, $fields = ['photo' => System::baseUrl() . '/photo/' . $data['photo']['id'] . '-4.' . $filetype,
'thumb' => System::baseUrl() . '/photo/' . $data['photo']['id'] . '-5.' . $filetype]; 'thumb' => System::baseUrl() . '/photo/' . $data['photo']['id'] . '-5.' . $filetype];

View File

@ -31,6 +31,7 @@ use Friendica\Model\Contact;
use Friendica\Model\Conversation; use Friendica\Model\Conversation;
use Friendica\Model\FileTag; use Friendica\Model\FileTag;
use Friendica\Model\Item; use Friendica\Model\Item;
use Friendica\Model\Photo;
use Friendica\Protocol\Diaspora; use Friendica\Protocol\Diaspora;
use Friendica\Protocol\Email; use Friendica\Protocol\Email;
use Friendica\Util\DateTimeFormat; use Friendica\Util\DateTimeFormat;
@ -456,16 +457,18 @@ function item_post(App $a) {
// Ensure to only modify photos that you own // Ensure to only modify photos that you own
$srch = '<' . intval($original_contact_id) . '>'; $srch = '<' . intval($original_contact_id) . '>';
$condition = ['allow_cid' => $srch, 'allow_gid' => '', 'deny_cid' => '', 'deny_gid' => '', $condition = [
'resource-id' => $image_uri, 'uid' => $profile_uid]; 'allow_cid' => $srch, 'allow_gid' => '', 'deny_cid' => '', 'deny_gid' => '',
if (!DBA::exists('photo', $condition)) { 'uid' => $profile_uid
];
if (!Photo::exists($image_uri, $condition)) {
continue; continue;
} }
$fields = ['allow_cid' => $str_contact_allow, 'allow_gid' => $str_group_allow, $fields = ['allow_cid' => $str_contact_allow, 'allow_gid' => $str_group_allow,
'deny_cid' => $str_contact_deny, 'deny_gid' => $str_group_deny]; 'deny_cid' => $str_contact_deny, 'deny_gid' => $str_group_deny];
$condition = ['resource-id' => $image_uri, 'uid' => $profile_uid]; $condition = ['resource-id' => $image_uri, 'uid' => $profile_uid];
DBA::update('photo', $fields, $condition); Photo::update($fields, $condition);
} }
} }
} }

View File

@ -344,7 +344,7 @@ function photos_post(App $a)
} }
if (DBA::isResult($r)) { if (DBA::isResult($r)) {
Photo::delete(['resource-id' => $r[0]['resource-id'], 'uid' => $page_owner_uid]); Photo::delete(['uid' => $page_owner_uid, 'resource-id' => $r[0]['resource-id']]);
Item::deleteForUser(['resource-id' => $r[0]['resource-id'], 'uid' => $page_owner_uid], $page_owner_uid); Item::deleteForUser(['resource-id' => $r[0]['resource-id'], 'uid' => $page_owner_uid], $page_owner_uid);

View File

@ -483,7 +483,7 @@ class Contact extends BaseObject
'gender' => $profile['gender'], 'avatar' => $profile['photo'], 'gender' => $profile['gender'], 'avatar' => $profile['photo'],
'contact-type' => $user['account-type'], 'xmpp' => $profile['xmpp']]; 'contact-type' => $user['account-type'], 'xmpp' => $profile['xmpp']];
$avatar = DBA::selectFirst('photo', ['resource-id', 'type'], ['uid' => $uid, 'profile' => true]); $avatar = Photo::selectFirst(['resource-id', 'type'], ['uid' => $uid, 'profile' => true]);
if (DBA::isResult($avatar)) { if (DBA::isResult($avatar)) {
if ($update_avatar) { if ($update_avatar) {
$fields['avatar-date'] = DateTimeFormat::utcNow(); $fields['avatar-date'] = DateTimeFormat::utcNow();

View File

@ -1030,7 +1030,7 @@ class Item extends BaseObject
* generate a resource-id and therefore aren't intimately linked to the item. * generate a resource-id and therefore aren't intimately linked to the item.
*/ */
if (strlen($item['resource-id'])) { if (strlen($item['resource-id'])) {
DBA::delete('photo', ['resource-id' => $item['resource-id'], 'uid' => $item['uid']]); Photo::delete(['resource-id' => $item['resource-id'], 'uid' => $item['uid']]);
} }
// If item is a link to an event, delete the event. // If item is a link to an event, delete the event.
@ -2737,8 +2737,7 @@ class Item extends BaseObject
if ($x) { if ($x) {
$res = substr($i, $x + 1); $res = substr($i, $x + 1);
$i = substr($i, 0, $x); $i = substr($i, 0, $x);
$fields = ['data', 'type', 'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid']; $photo = Photo::getPhotoForUser($uid, $i, $res);
$photo = DBA::selectFirst('photo', $fields, ['resource-id' => $i, 'scale' => $res, 'uid' => $uid]);
if (DBA::isResult($photo)) { if (DBA::isResult($photo)) {
/* /*
* Check to see if we should replace this photo link with an embedded image * Check to see if we should replace this photo link with an embedded image
@ -2762,9 +2761,7 @@ class Item extends BaseObject
} }
} }
if ($replace) { if ($replace) {
$data = $photo['data']; $photo_img = Photo::getImageForPhoto($photo);
$type = $photo['type'];
// If a custom width and height were specified, apply before embedding // If a custom width and height were specified, apply before embedding
if (preg_match("/\[img\=([0-9]*)x([0-9]*)\]/is", substr($orig_body, $img_start, $img_st_close), $match)) { if (preg_match("/\[img\=([0-9]*)x([0-9]*)\]/is", substr($orig_body, $img_start, $img_st_close), $match)) {
Logger::log('scaling photo', Logger::DEBUG); Logger::log('scaling photo', Logger::DEBUG);
@ -2772,14 +2769,12 @@ class Item extends BaseObject
$width = intval($match[1]); $width = intval($match[1]);
$height = intval($match[2]); $height = intval($match[2]);
$Image = new Image($data, $type); $photo_img->scaleDown(max($width, $height));
if ($Image->isValid()) {
$Image->scaleDown(max($width, $height));
$data = $Image->asString();
$type = $Image->getType();
}
} }
$data = $photo_img->asString();
$type = $photo_img->getType();
Logger::log('replacing photo', Logger::DEBUG); Logger::log('replacing photo', Logger::DEBUG);
$image = 'data:' . $type . ';base64,' . base64_encode($data); $image = 'data:' . $type . ';base64,' . base64_encode($data);
Logger::log('replaced: ' . $image, Logger::DATA); Logger::log('replaced: ' . $image, Logger::DATA);

View File

@ -10,6 +10,7 @@ use Friendica\Core\Logger;
use Friendica\Core\System; use Friendica\Core\System;
use Friendica\Core\Worker; use Friendica\Core\Worker;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\Model\Photo;
use Friendica\Network\Probe; use Friendica\Network\Probe;
use Friendica\Util\DateTimeFormat; use Friendica\Util\DateTimeFormat;
@ -141,7 +142,7 @@ class Mail
} }
$image_uri = substr($image, strrpos($image, '/') + 1); $image_uri = substr($image, strrpos($image, '/') + 1);
$image_uri = substr($image_uri, 0, strpos($image_uri, '-')); $image_uri = substr($image_uri, 0, strpos($image_uri, '-'));
DBA::update('photo', ['allow-cid' => '<' . $recipient . '>'], ['resource-id' => $image_uri, 'album' => 'Wall Photos', 'uid' => local_user()]); Photo::update(['allow-cid' => '<' . $recipient . '>'], ['resource-id' => $image_uri, 'album' => 'Wall Photos', 'uid' => local_user()]);
} }
} }
} }

View File

@ -17,6 +17,7 @@ use Friendica\Core\Protocol;
use Friendica\Core\System; use Friendica\Core\System;
use Friendica\Core\Worker; use Friendica\Core\Worker;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\Model\Photo;
use Friendica\Object\Image; use Friendica\Object\Image;
use Friendica\Util\Crypto; use Friendica\Util\Crypto;
use Friendica\Util\DateTimeFormat; use Friendica\Util\DateTimeFormat;
@ -701,7 +702,7 @@ class User
} }
if (!$photo_failure) { if (!$photo_failure) {
DBA::update('photo', ['profile' => 1], ['resource-id' => $hash]); Photo::update(['profile' => 1], ['resource-id' => $hash]);
} }
} }
} }

View File

@ -173,7 +173,7 @@ class CronJobs
} }
$condition = ['`uid` = 0 AND `resource-id` LIKE "pic:%" AND `created` < NOW() - INTERVAL ? SECOND', $cachetime]; $condition = ['`uid` = 0 AND `resource-id` LIKE "pic:%" AND `created` < NOW() - INTERVAL ? SECOND', $cachetime];
DBA::delete('photo', $condition); Photo::delete($condition);
} }
// Delete the cached OEmbed entries that are older than three month // Delete the cached OEmbed entries that are older than three month