3 * @copyright Copyright (C) 2010-2022, 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\Module\BaseApi;
31 * @see https://docs.joinmastodon.org/methods/statuses/media/
33 class Media extends BaseApi
35 protected function post(array $request = [])
37 self::checkAllowedScope(self::SCOPE_WRITE);
38 $uid = self::getCurrentUserID();
40 Logger::info('Photo post', ['request' => $request, 'files' => $_FILES]);
42 if (empty($_FILES['file'])) {
43 DI::mstdnError()->UnprocessableEntity();
46 $media = Photo::upload($uid, $_FILES['file']);
48 DI::mstdnError()->UnprocessableEntity();
51 Logger::info('Uploaded photo', ['media' => $media]);
53 System::jsonExit(DI::mstdnAttachment()->createFromPhoto($media['id']));
56 public function put(array $request = [])
58 self::checkAllowedScope(self::SCOPE_WRITE);
59 $uid = self::getCurrentUserID();
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
68 if (empty($this->parameters['id'])) {
69 DI::mstdnError()->UnprocessableEntity();
72 $photo = Photo::selectFirst(['resource-id'], ['id' => $this->parameters['id'], 'uid' => $uid]);
73 if (empty($photo['resource-id'])) {
74 DI::mstdnError()->RecordNotFound();
77 Photo::update(['desc' => $request['description']], ['resource-id' => $photo['resource-id']]);
79 System::jsonExit(DI::mstdnAttachment()->createFromPhoto($this->parameters['id']));
83 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
85 protected function rawContent(array $request = [])
87 self::checkAllowedScope(self::SCOPE_READ);
88 $uid = self::getCurrentUserID();
90 if (empty($this->parameters['id'])) {
91 DI::mstdnError()->UnprocessableEntity();
94 $id = $this->parameters['id'];
95 if (!Photo::exists(['id' => $id, 'uid' => $uid])) {
96 DI::mstdnError()->RecordNotFound();
99 System::jsonExit(DI::mstdnAttachment()->createFromPhoto($id));