3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Module\Api\Mastodon;
24 use Friendica\Core\Logger;
25 use Friendica\Core\System;
27 use Friendica\Model\Photo;
28 use Friendica\Model\Post;
29 use Friendica\Module\BaseApi;
32 * @see https://docs.joinmastodon.org/methods/statuses/media/
34 class Media extends BaseApi
36 protected function post(array $request = [])
38 self::checkAllowedScope(self::SCOPE_WRITE);
39 $uid = self::getCurrentUserID();
41 Logger::info('Photo post', ['request' => $request, 'files' => $_FILES]);
43 if (empty($_FILES['file'])) {
44 DI::mstdnError()->UnprocessableEntity();
47 $media = Photo::upload($uid, $_FILES['file']);
49 DI::mstdnError()->UnprocessableEntity();
52 Logger::info('Uploaded photo', ['media' => $media]);
54 System::jsonExit(DI::mstdnAttachment()->createFromPhoto($media['id']));
57 public function put(array $request = [])
59 self::checkAllowedScope(self::SCOPE_WRITE);
60 $uid = self::getCurrentUserID();
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
69 if (empty($this->parameters['id'])) {
70 DI::mstdnError()->UnprocessableEntity();
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();
79 if (!Post::exists(['uri-id' => $media['uri-id'], 'uid' => $uid, 'origin' => true])) {
80 DI::mstdnError()->RecordNotFound();
82 Post\Media::updateById(['description' => $request['description']], $this->parameters['id']);
83 System::jsonExit(DI::mstdnAttachment()->createFromId($this->parameters['id']));
86 Photo::update(['desc' => $request['description']], ['resource-id' => $photo['resource-id']]);
88 System::jsonExit(DI::mstdnAttachment()->createFromPhoto($this->parameters['id']));
92 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
94 protected function rawContent(array $request = [])
96 self::checkAllowedScope(self::SCOPE_READ);
97 $uid = self::getCurrentUserID();
99 if (empty($this->parameters['id'])) {
100 DI::mstdnError()->UnprocessableEntity();
103 $id = $this->parameters['id'];
104 if (!Photo::exists(['id' => $id, 'uid' => $uid])) {
105 DI::mstdnError()->RecordNotFound();
108 System::jsonExit(DI::mstdnAttachment()->createFromPhoto($id));