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