]> git.mxchange.org Git - friendica.git/blob - src/Factory/Api/Mastodon/Attachment.php
Fix test
[friendica.git] / src / Factory / Api / Mastodon / Attachment.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Factory\Api\Mastodon;
23
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;
32
33 class Attachment extends BaseFactory
34 {
35         /** @var BaseURL */
36         private $baseUrl;
37
38         public function __construct(LoggerInterface $logger, BaseURL $baseURL)
39         {
40                 parent::__construct($logger);
41
42                 $this->baseUrl = $baseURL;
43         }
44
45         /**
46          * @param int $uriId Uri-ID of the attachments
47          * @return array
48          * @throws HTTPException\InternalServerErrorException
49          */
50         public function createFromUriId(int $uriId): array
51         {
52                 $attachments = [];
53                 foreach (Post\Media::getByURIId($uriId, [Post\Media::AUDIO, Post\Media::VIDEO, Post\Media::IMAGE]) as $attachment) {
54                         $attachments[] = $this->createFromMediaArray($attachment);
55                 }
56
57                 return $attachments;
58         }
59
60         /**
61          * @param int $id id of the media
62          * @return \Friendica\Object\Api\Mastodon\Attachment
63          * @throws HTTPException\InternalServerErrorException
64          */
65         public function createFromId(int $id): \Friendica\Object\Api\Mastodon\Attachment
66         {
67                 $attachment = Post\Media::getById($id);
68                 if (empty($attachment)) {
69                         return [];
70                 }
71                 return $this->createFromMediaArray($attachment);
72         }
73
74         /**
75          * @param array $attachment
76          * @return \Friendica\Object\Api\Mastodon\Attachment
77          * @throws HTTPException\InternalServerErrorException
78          */
79         private function createFromMediaArray(array $attachment):  \Friendica\Object\Api\Mastodon\Attachment
80         {
81                 $filetype = !empty($attachment['mimetype']) ? strtolower(substr($attachment['mimetype'], 0, strpos($attachment['mimetype'], '/'))) : '';
82
83                 if (($filetype == 'audio') || ($attachment['type'] == Post\Media::AUDIO)) {
84                         $type = 'audio';
85                 } elseif (($filetype == 'video') || ($attachment['type'] == Post\Media::VIDEO)) {
86                         $type = 'video';
87                 } elseif ($attachment['mimetype'] == 'image/gif') {
88                         $type = 'gifv';
89                 } elseif (($filetype == 'image') || ($attachment['type'] == Post\Media::IMAGE)) {
90                         $type = 'image';
91                 } else {
92                         $type = 'unknown';
93                 }
94
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);
99                 } else {
100                         $url = $attachment['url'];
101
102                         if (!empty($attachment['preview'])) {
103                                 $preview = Post\Media::getPreviewUrlForId($attachment['id'], Proxy::SIZE_SMALL);
104                         } else {
105                                 $preview = '';
106                         }
107                 }
108
109                 return new \Friendica\Object\Api\Mastodon\Attachment($attachment, $type, $url, $preview, $remote);
110         }
111
112         /**
113          * @param int $id id of the photo
114          *
115          * @return array
116          * @throws HTTPException\InternalServerErrorException
117          */
118         public function createFromPhoto(int $id): array
119         {
120                 $photo = Photo::selectFirst(['resource-id', 'uid', 'id', 'title', 'type', 'width', 'height', 'blurhash'], ['id' => $id]);
121                 if (empty($photo)) {
122                         return [];
123                 }
124
125                 $attachment = [
126                         'id'          => $photo['id'],
127                         'description' => $photo['title'],
128                         'width'       => $photo['width'],
129                         'height'      => $photo['height'],
130                         'blurhash'    => $photo['blurhash'],
131                 ];
132
133                 $photoTypes = Images::supportedTypes();
134                 $ext        = $photoTypes[$photo['type']];
135
136                 $url = $this->baseUrl . '/photo/' . $photo['resource-id'] . '-0.' . $ext;
137
138                 $preview = Photo::selectFirst(['scale'], ["`resource-id` = ? AND `uid` = ? AND `scale` > ?", $photo['resource-id'], $photo['uid'], 0], ['order' => ['scale']]);
139                 if (empty($scale)) {
140                         $preview_url = $this->baseUrl . '/photo/' . $photo['resource-id'] . '-' . $preview['scale'] . '.' . $ext;
141                 } else {
142                         $preview_url = '';
143                 }
144
145                 $object = new \Friendica\Object\Api\Mastodon\Attachment($attachment, 'image', $url, $preview_url, '');
146                 return $object->toArray();
147         }
148 }