2021-05-09 12:59:23 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (C) 2010-2021, the Friendica project
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Friendica\Module\Api\Mastodon;
|
|
|
|
|
2021-05-16 09:56:02 +00:00
|
|
|
use Friendica\Core\Logger;
|
2021-05-09 12:59:23 +00:00
|
|
|
use Friendica\Core\System;
|
|
|
|
use Friendica\DI;
|
|
|
|
use Friendica\Model\Photo;
|
|
|
|
use Friendica\Module\BaseApi;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see https://docs.joinmastodon.org/methods/statuses/media/
|
|
|
|
*/
|
|
|
|
class Media extends BaseApi
|
|
|
|
{
|
2021-11-14 23:13:47 +01:00
|
|
|
public function post()
|
2021-05-16 09:56:02 +00:00
|
|
|
{
|
2021-06-08 12:00:22 +00:00
|
|
|
self::checkAllowedScope(self::SCOPE_WRITE);
|
2021-05-16 09:56:02 +00:00
|
|
|
$uid = self::getCurrentUserID();
|
|
|
|
|
|
|
|
Logger::info('Photo post', ['request' => $_REQUEST, 'files' => $_FILES]);
|
|
|
|
|
|
|
|
if (empty($_FILES['file'])) {
|
|
|
|
DI::mstdnError()->UnprocessableEntity();
|
|
|
|
}
|
|
|
|
|
|
|
|
$media = Photo::upload($uid, $_FILES['file']);
|
|
|
|
if (empty($media)) {
|
|
|
|
DI::mstdnError()->UnprocessableEntity();
|
|
|
|
}
|
|
|
|
|
|
|
|
Logger::info('Uploaded photo', ['media' => $media]);
|
|
|
|
|
|
|
|
System::jsonExit(DI::mstdnAttachment()->createFromPhoto($media['id']));
|
|
|
|
}
|
|
|
|
|
2021-11-14 23:13:47 +01:00
|
|
|
public function put()
|
2021-05-09 12:59:23 +00:00
|
|
|
{
|
2021-06-08 12:00:22 +00:00
|
|
|
self::checkAllowedScope(self::SCOPE_WRITE);
|
2021-05-16 07:37:11 +00:00
|
|
|
$uid = self::getCurrentUserID();
|
|
|
|
|
2021-05-28 06:10:32 +00:00
|
|
|
$request = self::getRequest([
|
|
|
|
'file' => [], // The file to be attached, using multipart form data.
|
|
|
|
'thumbnail' => [], // The custom thumbnail of the media to be attached, using multipart form data.
|
|
|
|
'description' => '', // A plain-text description of the media, for accessibility purposes.
|
|
|
|
'focus' => '', // Two floating points (x,y), comma-delimited ranging from -1.0 to 1.0
|
|
|
|
]);
|
2021-05-16 09:56:02 +00:00
|
|
|
|
2021-11-14 23:19:25 +01:00
|
|
|
if (empty($this->parameters['id'])) {
|
2021-05-16 09:56:02 +00:00
|
|
|
DI::mstdnError()->UnprocessableEntity();
|
|
|
|
}
|
|
|
|
|
2021-11-14 23:19:25 +01:00
|
|
|
$photo = Photo::selectFirst(['resource-id'], ['id' => $this->parameters['id'], 'uid' => $uid]);
|
2021-05-16 09:56:02 +00:00
|
|
|
if (empty($photo['resource-id'])) {
|
|
|
|
DI::mstdnError()->RecordNotFound();
|
|
|
|
}
|
|
|
|
|
2021-05-28 06:10:32 +00:00
|
|
|
Photo::update(['desc' => $request['description']], ['resource-id' => $photo['resource-id']]);
|
2021-05-16 09:56:02 +00:00
|
|
|
|
2021-11-14 23:19:25 +01:00
|
|
|
System::jsonExit(DI::mstdnAttachment()->createFromPhoto($this->parameters['id']));
|
2021-05-09 12:59:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
|
|
|
*/
|
2021-11-14 23:13:47 +01:00
|
|
|
public function rawContent()
|
2021-05-09 12:59:23 +00:00
|
|
|
{
|
2021-06-08 12:00:22 +00:00
|
|
|
self::checkAllowedScope(self::SCOPE_READ);
|
2021-05-09 12:59:23 +00:00
|
|
|
$uid = self::getCurrentUserID();
|
|
|
|
|
2021-11-14 23:19:25 +01:00
|
|
|
if (empty($this->parameters['id'])) {
|
2021-05-12 14:00:15 +00:00
|
|
|
DI::mstdnError()->UnprocessableEntity();
|
2021-05-09 12:59:23 +00:00
|
|
|
}
|
|
|
|
|
2021-11-14 23:19:25 +01:00
|
|
|
$id = $this->parameters['id'];
|
2021-05-09 12:59:23 +00:00
|
|
|
if (!Photo::exists(['id' => $id, 'uid' => $uid])) {
|
|
|
|
DI::mstdnError()->RecordNotFound();
|
|
|
|
}
|
|
|
|
|
|
|
|
System::jsonExit(DI::mstdnAttachment()->createFromPhoto($id));
|
|
|
|
}
|
|
|
|
}
|