3 * @copyright Copyright (C) 2010-2023, 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;
26 use Friendica\Model\Photo;
27 use Friendica\Network\HTTPException;
28 use Friendica\Model\Post;
29 use Friendica\Util\Images;
30 use Friendica\Util\Proxy;
31 use Psr\Log\LoggerInterface;
33 class Attachment extends BaseFactory
38 public function __construct(LoggerInterface $logger, BaseURL $baseURL)
40 parent::__construct($logger);
42 $this->baseUrl = $baseURL;
46 * @param int $uriId Uri-ID of the attachments
48 * @throws HTTPException\InternalServerErrorException
50 public function createFromUriId(int $uriId): array
53 foreach (Post\Media::getByURIId($uriId, [Post\Media::AUDIO, Post\Media::VIDEO, Post\Media::IMAGE]) as $attachment) {
54 $attachments[] = $this->createFromMediaArray($attachment);
61 * @param int $id id of the media
63 * @throws HTTPException\InternalServerErrorException
65 public function createFromId(int $id): array
67 $attachment = Post\Media::getById($id);
68 if (empty($attachment)) {
71 return $this->createFromMediaArray($attachment);
75 * @param array $attachment
77 * @throws HTTPException\InternalServerErrorException
79 private function createFromMediaArray(array $attachment): array
81 $filetype = !empty($attachment['mimetype']) ? strtolower(substr($attachment['mimetype'], 0, strpos($attachment['mimetype'], '/'))) : '';
83 if (($filetype == 'audio') || ($attachment['type'] == Post\Media::AUDIO)) {
85 } elseif (($filetype == 'video') || ($attachment['type'] == Post\Media::VIDEO)) {
87 } elseif ($attachment['mimetype'] == 'image/gif') {
89 } elseif (($filetype == 'image') || ($attachment['type'] == Post\Media::IMAGE)) {
95 $remote = $attachment['url'];
96 if ($type == 'image') {
97 $url = Post\Media::getPreviewUrlForId($attachment['id']);
98 $preview = Post\Media::getPreviewUrlForId($attachment['id'], Proxy::SIZE_SMALL);
100 $url = $attachment['url'];
102 if (!empty($attachment['preview'])) {
103 $preview = Post\Media::getPreviewUrlForId($attachment['id'], Proxy::SIZE_SMALL);
109 $object = new \Friendica\Object\Api\Mastodon\Attachment($attachment, $type, $url, $preview, $remote);
110 return $object->toArray();
114 * @param int $id id of the photo
117 * @throws HTTPException\InternalServerErrorException
119 public function createFromPhoto(int $id): array
121 $photo = Photo::selectFirst(['resource-id', 'uid', 'id', 'title', 'type', 'width', 'height', 'blurhash'], ['id' => $id]);
127 'id' => $photo['id'],
128 'description' => $photo['title'],
129 'width' => $photo['width'],
130 'height' => $photo['height'],
131 'blurhash' => $photo['blurhash'],
134 $photoTypes = Images::supportedTypes();
135 $ext = $photoTypes[$photo['type']];
137 $url = $this->baseUrl . '/photo/' . $photo['resource-id'] . '-0.' . $ext;
139 $preview = Photo::selectFirst(['scale'], ["`resource-id` = ? AND `uid` = ? AND `scale` > ?", $photo['resource-id'], $photo['uid'], 0], ['order' => ['scale']]);
141 $preview_url = $this->baseUrl . '/photo/' . $photo['resource-id'] . '-' . $preview['scale'] . '.' . $ext;
146 $object = new \Friendica\Object\Api\Mastodon\Attachment($attachment, 'image', $url, $preview_url, '');
147 return $object->toArray();