]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Mastodon/Media.php
Merge remote-tracking branch 'upstream/2023.03-rc' into api-edit
[friendica.git] / src / Module / Api / Mastodon / Media.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\Module\Api\Mastodon;
23
24 use Friendica\Core\Logger;
25 use Friendica\Core\System;
26 use Friendica\DI;
27 use Friendica\Model\Photo;
28 use Friendica\Model\Post;
29 use Friendica\Module\BaseApi;
30
31 /**
32  * @see https://docs.joinmastodon.org/methods/statuses/media/
33  */
34 class Media extends BaseApi
35 {
36         protected function post(array $request = [])
37         {
38                 self::checkAllowedScope(self::SCOPE_WRITE);
39                 $uid = self::getCurrentUserID();
40
41                 Logger::info('Photo post', ['request' => $request, 'files' => $_FILES]);
42
43                 if (empty($_FILES['file'])) {
44                         DI::mstdnError()->UnprocessableEntity();
45                 }
46
47                 $media = Photo::upload($uid, $_FILES['file']);
48                 if (empty($media)) {
49                         DI::mstdnError()->UnprocessableEntity();
50                 }
51
52                 Logger::info('Uploaded photo', ['media' => $media]);
53
54                 System::jsonExit(DI::mstdnAttachment()->createFromPhoto($media['id']));
55         }
56
57         public function put(array $request = [])
58         {
59                 self::checkAllowedScope(self::SCOPE_WRITE);
60                 $uid = self::getCurrentUserID();
61
62                 $request = $this->getRequest([
63                         'file'        => [], // The file to be attached, using multipart form data.
64                         'thumbnail'   => [], // The custom thumbnail of the media to be attached, using multipart form data.
65                         'description' => '', // A plain-text description of the media, for accessibility purposes.
66                         'focus'       => '', // Two floating points (x,y), comma-delimited ranging from -1.0 to 1.0
67                 ], $request);
68
69                 if (empty($this->parameters['id'])) {
70                         DI::mstdnError()->UnprocessableEntity();
71                 }
72
73                 $photo = Photo::selectFirst(['resource-id'], ['id' => $this->parameters['id'], 'uid' => $uid]);
74                 if (empty($photo['resource-id'])) {
75                         $media = Post\Media::getById($this->parameters['id']);
76                         if (empty($media['uri-id'])) {
77                                 DI::mstdnError()->RecordNotFound();
78                         }
79                         if (!Post::exists(['uri-id' => $media['uri-id'], 'uid' => $uid, 'origin' => true])) {
80                                 DI::mstdnError()->RecordNotFound();
81                         }
82                         Post\Media::updateById(['description' => $request['description']], $this->parameters['id']);
83                         System::jsonExit(DI::mstdnAttachment()->createFromId($this->parameters['id']));
84                 }
85
86                 Photo::update(['desc' => $request['description']], ['resource-id' => $photo['resource-id']]);
87
88                 System::jsonExit(DI::mstdnAttachment()->createFromPhoto($this->parameters['id']));
89         }
90
91         /**
92          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
93          */
94         protected function rawContent(array $request = [])
95         {
96                 self::checkAllowedScope(self::SCOPE_READ);
97                 $uid = self::getCurrentUserID();
98
99                 if (empty($this->parameters['id'])) {
100                         DI::mstdnError()->UnprocessableEntity();
101                 }
102
103                 $id = $this->parameters['id'];
104                 if (!Photo::exists(['id' => $id, 'uid' => $uid])) {
105                         DI::mstdnError()->RecordNotFound();
106                 }
107
108                 System::jsonExit(DI::mstdnAttachment()->createFromPhoto($id));
109         }
110 }