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 $this->checkAllowedScope(self::SCOPE_WRITE);
39 $uid = self::getCurrentUserID();
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
48 Logger::info('Photo post', ['request' => $request, 'files' => $_FILES]);
50 if (empty($_FILES['file'])) {
51 $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity());
54 $media = Photo::upload($uid, $_FILES['file'], '', null, null, '', '', $request['description']);
56 $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity());
59 Logger::info('Uploaded photo', ['media' => $media]);
61 $this->jsonExit(DI::mstdnAttachment()->createFromPhoto($media['id']));
64 public function put(array $request = [])
66 $this->checkAllowedScope(self::SCOPE_WRITE);
67 $uid = self::getCurrentUserID();
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
76 if (empty($this->parameters['id'])) {
77 $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity());
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());
86 if (!Post::exists(['uri-id' => $media['uri-id'], 'uid' => $uid, 'origin' => true])) {
87 $this->logErrorAndJsonExit(404, $this->errorFactory->RecordNotFound());
89 Post\Media::updateById(['description' => $request['description']], $this->parameters['id']);
90 $this->jsonExit(DI::mstdnAttachment()->createFromId($this->parameters['id']));
93 Photo::update(['desc' => $request['description']], ['resource-id' => $photo['resource-id']]);
95 $this->jsonExit(DI::mstdnAttachment()->createFromPhoto($this->parameters['id']));
99 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
101 protected function rawContent(array $request = [])
103 $this->checkAllowedScope(self::SCOPE_READ);
104 $uid = self::getCurrentUserID();
106 if (empty($this->parameters['id'])) {
107 $this->logErrorAndJsonExit(422, $this->errorFactory->UnprocessableEntity());
110 $id = $this->parameters['id'];
111 if (!Photo::exists(['id' => $id, 'uid' => $uid])) {
112 $this->logErrorAndJsonExit(404, $this->errorFactory->RecordNotFound());
115 $this->jsonExit(DI::mstdnAttachment()->createFromPhoto($id));