3 * @copyright Copyright (C) 2010-2022, 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 $filetype = !empty($attachment['mimetype']) ? strtolower(substr($attachment['mimetype'], 0, strpos($attachment['mimetype'], '/'))) : '';
56 if (($filetype == 'audio') || ($attachment['type'] == Post\Media::AUDIO)) {
58 } elseif (($filetype == 'video') || ($attachment['type'] == Post\Media::VIDEO)) {
60 } elseif ($attachment['mimetype'] == 'image/gif') {
62 } elseif (($filetype == 'image') || ($attachment['type'] == Post\Media::IMAGE)) {
68 $remote = $attachment['url'];
69 if ($type == 'image') {
70 $url = Post\Media::getPreviewUrlForId($attachment['id']);
71 $preview = Post\Media::getPreviewUrlForId($attachment['id'], Proxy::SIZE_SMALL);
73 $url = $attachment['url'];
75 if (!empty($attachment['preview'])) {
76 $preview = Post\Media::getPreviewUrlForId($attachment['id'], Proxy::SIZE_SMALL);
82 $object = new \Friendica\Object\Api\Mastodon\Attachment($attachment, $type, $url, $preview, $remote);
83 $attachments[] = $object->toArray();
90 * @param int $id id of the photo
93 * @throws HTTPException\InternalServerErrorException
95 public function createFromPhoto(int $id): array
97 $photo = Photo::selectFirst(['resource-id', 'uid', 'id', 'title', 'type'], ['id' => $id]);
102 $attachment = ['id' => $photo['id'], 'description' => $photo['title']];
104 $photoTypes = Images::supportedTypes();
105 $ext = $photoTypes[$photo['type']];
107 $url = $this->baseUrl . '/photo/' . $photo['resource-id'] . '-0.' . $ext;
109 $preview = Photo::selectFirst(['scale'], ["`resource-id` = ? AND `uid` = ? AND `scale` > ?", $photo['resource-id'], $photo['uid'], 0], ['order' => ['scale']]);
111 $preview_url = $this->baseUrl . '/photo/' . $photo['resource-id'] . '-' . $preview['scale'] . '.' . $ext;
117 $object = new \Friendica\Object\Api\Mastodon\Attachment($attachment, 'image', $url, $preview_url, '');
118 return $object->toArray();