3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Factory\Api\Mastodon;
24 use Friendica\App\BaseURL;
25 use Friendica\BaseFactory;
27 use Friendica\Model\Photo;
28 use Friendica\Network\HTTPException;
29 use Friendica\Model\Post;
30 use Friendica\Repository\ProfileField;
31 use Friendica\Util\Images;
32 use Friendica\Util\Proxy;
33 use Psr\Log\LoggerInterface;
35 class Attachment extends BaseFactory
39 /** @var ProfileField */
40 protected $profileField;
42 protected $mstdnField;
44 public function __construct(LoggerInterface $logger, BaseURL $baseURL, ProfileField $profileField, Field $mstdnField)
46 parent::__construct($logger);
48 $this->baseUrl = $baseURL;
49 $this->profileField = $profileField;
50 $this->mstdnField = $mstdnField;
54 * @param int $uriId Uri-ID of the attachments
56 * @throws HTTPException\InternalServerErrorException
57 * @throws \ImagickException
59 public function createFromUriId(int $uriId)
62 foreach (Post\Media::getByURIId($uriId, [Post\Media::AUDIO, Post\Media::VIDEO, Post\Media::IMAGE]) as $attachment) {
64 $filetype = !empty($attachment['mimetype']) ? strtolower(substr($attachment['mimetype'], 0, strpos($attachment['mimetype'], '/'))) : '';
66 if (($filetype == 'audio') || ($attachment['type'] == Post\Media::AUDIO)) {
68 } elseif (($filetype == 'video') || ($attachment['type'] == Post\Media::VIDEO)) {
70 } elseif ($attachment['mimetype'] == 'image/gif') {
72 } elseif (($filetype == 'image') || ($attachment['type'] == Post\Media::IMAGE)) {
78 $remote = $attachment['url'];
79 if ($type == 'image') {
80 if (Proxy::isLocalImage($attachment['url'])) {
81 $url = $attachment['url'];
82 $preview = $attachment['preview'] ?? $url;
85 $url = Proxy::proxifyUrl($attachment['url']);
86 $preview = Proxy::proxifyUrl($attachment['url'], false, Proxy::SIZE_SMALL);
93 $object = new \Friendica\Object\Api\Mastodon\Attachment($attachment, $type, $url, $preview, $remote);
94 $attachments[] = $object->toArray();
101 * @param int $id id of the photo
103 * @throws HTTPException\InternalServerErrorException
104 * @throws \ImagickException
106 public function createFromPhoto(int $id)
108 $photo = Photo::selectFirst(['resource-id', 'uid', 'id', 'title', 'type'], ['id' => $id]);
113 $attachment = ['id' => $photo['id'], 'description' => $photo['title']];
115 $phototypes = Images::supportedTypes();
116 $ext = $phototypes[$photo['type']];
118 $url = DI::baseUrl() . '/photo/' . $photo['resource-id'] . '-0.' . $ext;
120 $preview = Photo::selectFirst(['scale'], ["`resource-id` = ? AND `uid` = ? AND `scale` > ?", $photo['resource-id'], $photo['uid'], 0], ['order' => ['scale']]);
122 $preview_url = DI::baseUrl() . '/photo/' . $photo['resource-id'] . '-' . $preview['scale'] . '.' . $ext;
128 $object = new \Friendica\Object\Api\Mastodon\Attachment($attachment, 'image', $url, $preview_url, '');
129 return $object->toArray();