use Friendica\App\BaseURL;
use Friendica\BaseFactory;
+use Friendica\Model\Attach;
use Friendica\Model\Photo;
use Friendica\Network\HTTPException;
use Friendica\Model\Post;
$object = new \Friendica\Object\Api\Mastodon\Attachment($attachment, 'image', $url, $preview_url, '');
return $object->toArray();
}
+
+ /**
+ * @param int $id id of the attachment
+ *
+ * @return array
+ * @throws HTTPException\InternalServerErrorException
+ */
+ public function createFromAttach(int $id): array
+ {
+ $media = Attach::selectFirst(['id', 'filetype'], ['id' => $id]);
+ if (empty($media)) {
+ return [];
+ }
+ $attachment = [
+ 'id' => 'attach:' . $media['id'],
+ 'description' => null,
+ 'blurhash' => null,
+ ];
+
+ $types = [Post\Media::AUDIO => 'audio', Post\Media::VIDEO => 'video', Post\Media::IMAGE => 'image'];
+
+ $type = Post\Media::getType($media['filetype']);
+
+ $url = $this->baseUrl . '/attach/' . $id;
+
+ $object = new \Friendica\Object\Api\Mastodon\Attachment($attachment, $types[$type] ?? 'unknown', $url, '', '');
+ return $object->toArray();
+ }
+
+ public function isAttach(string $id): bool
+ {
+ return substr($id, 0, 7) == 'attach:';
+ }
}
* @param string $src Source file name
* @param int $uid User id
* @param string $filename Optional file name
+ * @param string $filetype Optional file type
* @param string $allow_cid
* @param string $allow_gid
* @param string $deny_cid
* @return boolean|int Insert id or false on failure
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
- public static function storeFile(string $src, int $uid, string $filename = '', string $allow_cid = '', string $allow_gid = '', string $deny_cid = '', string $deny_gid = '')
+ public static function storeFile(string $src, int $uid, string $filename = '', string $filetype = '', string $allow_cid = '', string $allow_gid = '', string $deny_cid = '', string $deny_gid = '')
{
if ($filename === '') {
$filename = basename($src);
$data = @file_get_contents($src);
- return self::store($data, $uid, $filename, '', null, $allow_cid, $allow_gid, $deny_cid, $deny_gid);
+ return self::store($data, $uid, $filename, $filetype, null, $allow_cid, $allow_gid, $deny_cid, $deny_gid);
}
}
}
+ public static function setPermissionForId(int $id, int $uid, string $str_contact_allow, string $str_circle_allow, string $str_contact_deny, string $str_circle_deny)
+ {
+ $fields = [
+ 'allow_cid' => $str_contact_allow, 'allow_gid' => $str_circle_allow,
+ 'deny_cid' => $str_contact_deny, 'deny_gid' => $str_circle_deny,
+ ];
+
+ self::update($fields, ['id' => $id, 'uid' => $uid]);
+ }
+
public static function addAttachmentToBody(string $body, int $uid): string
{
preg_match_all("/\[attachment\](.*?)\[\/attachment\]/ism", $body, $matches, PREG_SET_ORDER);
return $data;
}
- $type = explode('/', current(explode(';', $data['mimetype'])));
+ $data['type'] = self::getType($data['mimetype']);
+ return $data;
+ }
+
+ public static function getType(string $mimeType): int
+ {
+ $type = explode('/', current(explode(';', $mimeType)));
if (count($type) < 2) {
- Logger::info('Unknown MimeType', ['type' => $type, 'media' => $data]);
- $data['type'] = self::UNKNOWN;
- return $data;
+ Logger::info('Unknown MimeType', ['type' => $type, 'media' => $mimeType]);
+ return self::UNKNOWN;
}
$filetype = strtolower($type[0]);
$subtype = strtolower($type[1]);
if ($filetype == 'image') {
- $data['type'] = self::IMAGE;
+ $type = self::IMAGE;
} elseif ($filetype == 'video') {
- $data['type'] = self::VIDEO;
+ $type = self::VIDEO;
} elseif ($filetype == 'audio') {
- $data['type'] = self::AUDIO;
+ $type = self::AUDIO;
} elseif (($filetype == 'text') && ($subtype == 'html')) {
- $data['type'] = self::HTML;
+ $type = self::HTML;
} elseif (($filetype == 'text') && ($subtype == 'xml')) {
- $data['type'] = self::XML;
+ $type = self::XML;
} elseif (($filetype == 'text') && ($subtype == 'plain')) {
- $data['type'] = self::PLAIN;
+ $type = self::PLAIN;
} elseif ($filetype == 'text') {
- $data['type'] = self::TEXT;
+ $type = self::TEXT;
} elseif (($filetype == 'application') && ($subtype == 'x-bittorrent')) {
- $data['type'] = self::TORRENT;
+ $type = self::TORRENT;
} elseif ($filetype == 'application') {
- $data['type'] = self::APPLICATION;
+ $type = self::APPLICATION;
} else {
- $data['type'] = self::UNKNOWN;
- Logger::info('Unknown type', ['filetype' => $filetype, 'subtype' => $subtype, 'media' => $data]);
- return $data;
+ $type = self::UNKNOWN;
+ Logger::info('Unknown type', ['filetype' => $filetype, 'subtype' => $subtype, 'media' => $mimeType]);
}
- Logger::debug('Detected type', ['filetype' => $filetype, 'subtype' => $subtype, 'media' => $data]);
- return $data;
+ Logger::debug('Detected type', ['filetype' => $filetype, 'subtype' => $subtype, 'media' => $mimeType]);
+ return $type;
}
/**
return new InstanceEntity\Configuration(
$statuses_config,
- new InstanceEntity\MediaAttachmentsConfig(Images::supportedMimeTypes(), $image_size_limit, $image_matrix_limit),
+ new InstanceEntity\MediaAttachmentsConfig($this->supportedMimeTypes(), $image_size_limit, $image_matrix_limit),
new InstanceEntity\Polls(),
new InstanceEntity\Accounts(),
);
}
+ private function supportedMimeTypes(): array
+ {
+ $mimetypes = ['audio/aac', 'audio/flac', 'audio/mpeg', 'audio/mp4', 'audio/ogg', 'audio/wav',
+ 'audio/webm', 'video/mp4', 'video/ogg', 'video/webm'];
+ return array_merge(Images::supportedMimeTypes(), $mimetypes);
+ }
+
private function buildContactInfo(): InstanceEntity\Contact
{
$email = implode(',', User::getAdminEmailList());
namespace Friendica\Module\Api\Mastodon;
use Friendica\Core\Logger;
-use Friendica\Core\System;
use Friendica\DI;
+use Friendica\Model\Attach;
+use Friendica\Model\Contact;
use Friendica\Model\Photo;
use Friendica\Model\Post;
use Friendica\Module\BaseApi;
$this->logAndJsonError(422, $this->errorFactory->UnprocessableEntity());
}
- $media = Photo::upload($uid, $_FILES['file'], '', null, null, '', '', $request['description']);
- if (empty($media)) {
- $this->logAndJsonError(422, $this->errorFactory->UnprocessableEntity());
- }
+ $type = Post\Media::getType($_FILES['file']['type']);
+
+ if (in_array($type, [Post\Media::IMAGE, Post\Media::UNKNOWN])) {
+ $media = Photo::upload($uid, $_FILES['file'], '', null, null, '', '', $request['description']);
+ if (empty($media)) {
+ $this->logAndJsonError(422, $this->errorFactory->UnprocessableEntity());
+ }
- Logger::info('Uploaded photo', ['media' => $media]);
+ Logger::info('Uploaded photo', ['media' => $media]);
- $this->jsonExit(DI::mstdnAttachment()->createFromPhoto($media['id']));
+ $this->jsonExit(DI::mstdnAttachment()->createFromPhoto($media['id']));
+ } else {
+ $tempFileName = $_FILES['file']['tmp_name'];
+ $fileName = basename($_FILES['file']['name']);
+ $fileSize = intval($_FILES['file']['size']);
+ $maxFileSize = DI::config()->get('system', 'maxfilesize');
+
+ if ($fileSize <= 0) {
+ @unlink($tempFileName);
+ $this->logAndJsonError(422, $this->errorFactory->UnprocessableEntity());
+ }
+
+ if ($maxFileSize && $fileSize > $maxFileSize) {
+ @unlink($tempFileName);
+ $this->logAndJsonError(422, $this->errorFactory->UnprocessableEntity());
+ }
+
+ $id = Attach::storeFile($tempFileName, self::getCurrentUserID(), $fileName, $_FILES['file']['type'], '<' . Contact::getPublicIdByUserId(self::getCurrentUserID()) . '>');
+ @unlink($tempFileName);
+ Logger::info('Uploaded media', ['id' => $id]);
+ $this->jsonExit(DI::mstdnAttachment()->createFromAttach($id));
+ }
}
public function put(array $request = [])
$this->logAndJsonError(422, $this->errorFactory->UnprocessableEntity());
}
+ if (DI::mstdnAttachment()->isAttach($this->parameters['id']) && Attach::exists(['id' => substr($this->parameters['id'], 7)])) {
+ $this->jsonExit(DI::mstdnAttachment()->createFromAttach(substr($this->parameters['id'], 7)));
+ }
+
$photo = Photo::selectFirst(['resource-id'], ['id' => $this->parameters['id'], 'uid' => $uid]);
if (empty($photo['resource-id'])) {
$media = Post\Media::getById($this->parameters['id']);
}
$id = $this->parameters['id'];
- if (!Photo::exists(['id' => $id, 'uid' => $uid])) {
- $this->logAndJsonError(404, $this->errorFactory->RecordNotFound());
+
+ if (Photo::exists(['id' => $id, 'uid' => $uid])) {
+ $this->jsonExit(DI::mstdnAttachment()->createFromPhoto($id));
+ }
+
+ if (DI::mstdnAttachment()->isAttach($id) && Attach::exists(['id' => substr($id, 7)])) {
+ $this->jsonExit(DI::mstdnAttachment()->createFromAttach(substr($id, 7)));
}
- $this->jsonExit(DI::mstdnAttachment()->createFromPhoto($id));
+ $this->logAndJsonError(404, $this->errorFactory->RecordNotFound());
}
}
use Friendica\Core\Worker;
use Friendica\Database\DBA;
use Friendica\DI;
+use Friendica\Model\Attach;
use Friendica\Model\Contact;
use Friendica\Model\Circle;
use Friendica\Model\Item;
$item['attachments'] = [];
foreach ($media_ids as $id) {
+ if (DI::mstdnAttachment()->isAttach($id) && Attach::exists(['id' => substr($id, 7)])) {
+ $attach = Attach::selectFirst([], ['id' => substr($id, 7)]);
+ $attachment = [
+ 'type' => Post\Media::getType($attach['filetype']),
+ 'mimetype' => $attach['filetype'],
+ 'url' => DI::baseUrl() . '/attach/' . substr($id, 7),
+ 'size' => $attach['filetype'],
+ 'name' => $attach['filename']
+ ];
+ $item['attachments'][] = $attachment;
+ Attach::setPermissionForId(substr($id, 7), $item['uid'], $item['allow_cid'], $item['allow_gid'], $item['deny_cid'], $item['deny_gid']);
+ continue;
+ }
+
$media = DBA::toArray(DBA::p("SELECT `resource-id`, `scale`, `type`, `desc`, `filename`, `datasize`, `width`, `height` FROM `photo`
WHERE `resource-id` IN (SELECT `resource-id` FROM `photo` WHERE `id` = ?) AND `photo`.`uid` = ?
ORDER BY `photo`.`width` DESC LIMIT 2", $id, $item['uid']));
$ext = Images::getExtensionByMimeType($media[0]['type']);
- $attachment = ['type' => Post\Media::IMAGE, 'mimetype' => $media[0]['type'],
- 'url' => DI::baseUrl() . '/photo/' . $media[0]['resource-id'] . '-' . $media[0]['scale'] . $ext,
- 'size' => $media[0]['datasize'],
- 'name' => $media[0]['filename'] ?: $media[0]['resource-id'],
+ $attachment = [
+ 'type' => Post\Media::IMAGE,
+ 'mimetype' => $media[0]['type'],
+ 'url' => DI::baseUrl() . '/photo/' . $media[0]['resource-id'] . '-' . $media[0]['scale'] . $ext,
+ 'size' => $media[0]['datasize'],
+ 'name' => $media[0]['filename'] ?: $media[0]['resource-id'],
'description' => $media[0]['desc'] ?? '',
- 'width' => $media[0]['width'],
- 'height' => $media[0]['height']];
+ 'width' => $media[0]['width'],
+ 'height' => $media[0]['height']
+ ];
if (count($media) > 1) {
$attachment['preview'] = DI::baseUrl() . '/photo/' . $media[1]['resource-id'] . '-' . $media[1]['scale'] . $ext;
$this->return(401, $msg);
}
- $newid = Attach::storeFile($tempFileName, $owner['uid'], $fileName, '<' . $owner['id'] . '>');
+ $newid = Attach::storeFile($tempFileName, $_FILES['userfile']['type'] ?? '', $owner['uid'], $fileName, '<' . $owner['id'] . '>');
@unlink($tempFileName);
/** @var int */
protected $video_size_limit = 0;
/** @var int */
- protected $video_frame_rate_limit = 0;
+ protected $video_frame_rate_limit = 60;
/** @var int */
protected $video_matrix_limit = 0;
$this->supported_mime_types = $supported_mime_types;
$this->image_size_limit = $image_size_limit;
$this->image_matrix_limit = $image_matrix_limit;
+ $this->video_size_limit = $image_size_limit;
+ $this->video_matrix_limit = $image_matrix_limit;
}
}
'/lists/{id:\d+}' => [Module\Api\Mastodon\Lists::class, [R::GET, R::PUT, R::DELETE]],
'/lists/{id:\d+}/accounts' => [Module\Api\Mastodon\Lists\Accounts::class, [R::GET, R::POST, R::DELETE]],
'/markers' => [Module\Api\Mastodon\Markers::class, [R::GET, R::POST]],
- '/media/{id:\d+}' => [Module\Api\Mastodon\Media::class, [R::GET, R::PUT ]],
+ '/media/{id}' => [Module\Api\Mastodon\Media::class, [R::GET, R::PUT ]],
'/mutes' => [Module\Api\Mastodon\Mutes::class, [R::GET ]],
'/notifications' => [Module\Api\Mastodon\Notifications::class, [R::GET ]],
'/notifications/{id:\d+}' => [Module\Api\Mastodon\Notifications::class, [R::GET ]],