]> git.mxchange.org Git - friendica.git/blob - src/Factory/Api/Mastodon/Attachment.php
Use Args::getMethod() at various places
[friendica.git] / src / Factory / Api / Mastodon / Attachment.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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                         $filetype = !empty($attachment['mimetype']) ? strtolower(substr($attachment['mimetype'], 0, strpos($attachment['mimetype'], '/'))) : '';
55
56                         if (($filetype == 'audio') || ($attachment['type'] == Post\Media::AUDIO)) {
57                                 $type = 'audio';
58                         } elseif (($filetype == 'video') || ($attachment['type'] == Post\Media::VIDEO)) {
59                                 $type = 'video';
60                         } elseif ($attachment['mimetype'] == 'image/gif') {
61                                 $type = 'gifv';
62                         } elseif (($filetype == 'image') || ($attachment['type'] == Post\Media::IMAGE)) {
63                                 $type = 'image';
64                         } else {
65                                 $type = 'unknown';
66                         }
67
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);
72                         } else {
73                                 $url = $attachment['url'];
74
75                                 if (!empty($attachment['preview'])) {
76                                         $preview = Post\Media::getPreviewUrlForId($attachment['id'], Proxy::SIZE_SMALL);
77                                 } else {
78                                         $preview = '';
79                                 }
80                         }
81
82                         $object        = new \Friendica\Object\Api\Mastodon\Attachment($attachment, $type, $url, $preview, $remote);
83                         $attachments[] = $object->toArray();
84                 }
85
86                 return $attachments;
87         }
88
89         /**
90          * @param int $id id of the photo
91          *
92          * @return array
93          * @throws HTTPException\InternalServerErrorException
94          */
95         public function createFromPhoto(int $id): array
96         {
97                 $photo = Photo::selectFirst(['resource-id', 'uid', 'id', 'title', 'type'], ['id' => $id]);
98                 if (empty($photo)) {
99                         return [];
100                 }
101
102                 $attachment = ['id' => $photo['id'], 'description' => $photo['title']];
103
104                 $photoTypes = Images::supportedTypes();
105                 $ext        = $photoTypes[$photo['type']];
106
107                 $url = $this->baseUrl . '/photo/' . $photo['resource-id'] . '-0.' . $ext;
108
109                 $preview = Photo::selectFirst(['scale'], ["`resource-id` = ? AND `uid` = ? AND `scale` > ?", $photo['resource-id'], $photo['uid'], 0], ['order' => ['scale']]);
110                 if (empty($scale)) {
111                         $preview_url = $this->baseUrl . '/photo/' . $photo['resource-id'] . '-' . $preview['scale'] . '.' . $ext;
112                 } else {
113                         $preview_url = '';
114                 }
115
116
117                 $object = new \Friendica\Object\Api\Mastodon\Attachment($attachment, 'image', $url, $preview_url, '');
118                 return $object->toArray();
119         }
120 }