]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Mastodon/Media.php
Move jsonError out of Factory\Api\Mastodon\Error->RecordNotFound
[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                 $this->checkAllowedScope(self::SCOPE_WRITE);
39                 $uid = self::getCurrentUserID();
40
41                 $request = $this->getRequest([
42                         'file'        => [], // The file to be attached, using multipart form data.
43                         'thumbnail'   => [], // The custom thumbnail of the media to be attached, using multipart form data.
44                         'description' => '', // A plain-text description of the media, for accessibility purposes.
45                         'focus'       => '', // Two floating points (x,y), comma-delimited ranging from -1.0 to 1.0
46                 ], $request);
47
48                 Logger::info('Photo post', ['request' => $request, 'files' => $_FILES]);
49
50                 if (empty($_FILES['file'])) {
51                         DI::mstdnError()->UnprocessableEntity();
52                 }
53
54                 $media = Photo::upload($uid, $_FILES['file'], '', null, null, '', '', $request['description']);
55                 if (empty($media)) {
56                         DI::mstdnError()->UnprocessableEntity();
57                 }
58
59                 Logger::info('Uploaded photo', ['media' => $media]);
60
61                 $this->jsonExit(DI::mstdnAttachment()->createFromPhoto($media['id']));
62         }
63
64         public function put(array $request = [])
65         {
66                 $this->checkAllowedScope(self::SCOPE_WRITE);
67                 $uid = self::getCurrentUserID();
68
69                 $request = $this->getRequest([
70                         'file'        => [], // The file to be attached, using multipart form data.
71                         'thumbnail'   => [], // The custom thumbnail of the media to be attached, using multipart form data.
72                         'description' => '', // A plain-text description of the media, for accessibility purposes.
73                         'focus'       => '', // Two floating points (x,y), comma-delimited ranging from -1.0 to 1.0
74                 ], $request);
75
76                 if (empty($this->parameters['id'])) {
77                         DI::mstdnError()->UnprocessableEntity();
78                 }
79
80                 $photo = Photo::selectFirst(['resource-id'], ['id' => $this->parameters['id'], 'uid' => $uid]);
81                 if (empty($photo['resource-id'])) {
82                         $media = Post\Media::getById($this->parameters['id']);
83                         if (empty($media['uri-id'])) {
84                                 $this->logErrorAndJsonExit(404, $this->errorFactory->RecordNotFound());
85                         }
86                         if (!Post::exists(['uri-id' => $media['uri-id'], 'uid' => $uid, 'origin' => true])) {
87                                 $this->logErrorAndJsonExit(404, $this->errorFactory->RecordNotFound());
88                         }
89                         Post\Media::updateById(['description' => $request['description']], $this->parameters['id']);
90                         $this->jsonExit(DI::mstdnAttachment()->createFromId($this->parameters['id']));
91                 }
92
93                 Photo::update(['desc' => $request['description']], ['resource-id' => $photo['resource-id']]);
94
95                 $this->jsonExit(DI::mstdnAttachment()->createFromPhoto($this->parameters['id']));
96         }
97
98         /**
99          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
100          */
101         protected function rawContent(array $request = [])
102         {
103                 $this->checkAllowedScope(self::SCOPE_READ);
104                 $uid = self::getCurrentUserID();
105
106                 if (empty($this->parameters['id'])) {
107                         DI::mstdnError()->UnprocessableEntity();
108                 }
109
110                 $id = $this->parameters['id'];
111                 if (!Photo::exists(['id' => $id, 'uid' => $uid])) {
112                         $this->logErrorAndJsonExit(404, $this->errorFactory->RecordNotFound());
113                 }
114
115                 $this->jsonExit(DI::mstdnAttachment()->createFromPhoto($id));
116         }
117 }