]> git.mxchange.org Git - friendica.git/blob - src/Factory/Api/Mastodon/Attachment.php
Merge pull request #10324 from annando/issue-10262
[friendica.git] / src / Factory / Api / Mastodon / Attachment.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, 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\DI;
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;
34
35 class Attachment extends BaseFactory
36 {
37         /** @var BaseURL */
38         protected $baseUrl;
39         /** @var ProfileField */
40         protected $profileField;
41         /** @var Field */
42         protected $mstdnField;
43
44         public function __construct(LoggerInterface $logger, BaseURL $baseURL, ProfileField $profileField, Field $mstdnField)
45         {
46                 parent::__construct($logger);
47
48                 $this->baseUrl = $baseURL;
49                 $this->profileField = $profileField;
50                 $this->mstdnField = $mstdnField;
51         }
52
53         /**
54          * @param int $uriId Uri-ID of the attachments
55          * @return array
56          * @throws HTTPException\InternalServerErrorException
57          * @throws \ImagickException
58          */
59         public function createFromUriId(int $uriId)
60         {
61                 $attachments = [];
62                 foreach (Post\Media::getByURIId($uriId, [Post\Media::AUDIO, Post\Media::VIDEO, Post\Media::IMAGE]) as $attachment) {
63
64                         $filetype = !empty($attachment['mimetype']) ? strtolower(substr($attachment['mimetype'], 0, strpos($attachment['mimetype'], '/'))) : '';
65
66                         if (($filetype == 'audio') || ($attachment['type'] == Post\Media::AUDIO)) {
67                                 $type = 'audio';
68                         } elseif (($filetype == 'video') || ($attachment['type'] == Post\Media::VIDEO)) {
69                                 $type = 'video';
70                         } elseif ($attachment['mimetype'] == 'image/gif') {
71                                 $type = 'gifv';
72                         } elseif (($filetype == 'image') || ($attachment['type'] == Post\Media::IMAGE)) {
73                                 $type = 'image';
74                         } else {
75                                 $type = 'unknown';
76                         }
77
78                         $remote = $attachment['url'];
79                         if ($type == 'image') {
80                                 if (Proxy::isLocalImage($attachment['url'])) {
81                                         $url = $attachment['url'];
82                                         $preview = $attachment['preview'] ?? $url;
83                                         $remote = '';
84                                 } else {
85                                         $url = Proxy::proxifyUrl($attachment['url']);
86                                         $preview = Proxy::proxifyUrl($attachment['url'], false, Proxy::SIZE_SMALL);
87                                 }
88                         } else {
89                                 $url = '';
90                                 $preview = '';
91                         }
92
93                         $object = new \Friendica\Object\Api\Mastodon\Attachment($attachment, $type, $url, $preview, $remote);
94                         $attachments[] = $object->toArray();
95                 }
96
97                 return $attachments;
98         }
99
100         /**
101          * @param int $id id of the photo
102          * @return array
103          * @throws HTTPException\InternalServerErrorException
104          * @throws \ImagickException
105          */
106         public function createFromPhoto(int $id)
107         {
108                 $photo = Photo::selectFirst(['resource-id', 'uid', 'id', 'title', 'type'], ['id' => $id]);
109                 if (empty($photo)) {
110                         return null;
111                 }
112
113                 $attachment = ['id' => $photo['id'], 'description' => $photo['title']];
114
115                 $phototypes = Images::supportedTypes();
116                 $ext = $phototypes[$photo['type']];
117
118                 $url = DI::baseUrl() . '/photo/' . $photo['resource-id'] . '-0.' . $ext;
119
120                 $preview = Photo::selectFirst(['scale'], ["`resource-id` = ? AND `uid` = ? AND `scale` > ?", $photo['resource-id'], $photo['uid'], 0], ['order' => ['scale']]);
121                 if (empty($scale)) {
122                         $preview_url = DI::baseUrl() . '/photo/' . $photo['resource-id'] . '-' . $preview['scale'] . '.' . $ext;
123                 } else {
124                         $preview_url = '';
125                 }
126
127
128                 $object = new \Friendica\Object\Api\Mastodon\Attachment($attachment, 'image', $url, $preview_url, '');
129                 return $object->toArray();
130         }
131 }