[Media] Display images and videos inline in notes
This commit is contained in:
parent
a9d5f8ac5b
commit
6165f7cd55
@ -22,20 +22,26 @@
|
|||||||
namespace Component\Media\Controller;
|
namespace Component\Media\Controller;
|
||||||
|
|
||||||
use App\Core\Controller;
|
use App\Core\Controller;
|
||||||
use Component\Media\Media;
|
use Component\Media\Media as M;
|
||||||
use Exception;
|
use Exception;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
|
||||||
class Avatar extends Controller
|
class Media extends Controller
|
||||||
{
|
{
|
||||||
public function send(Request $request, string $nickname, string $size)
|
public function avatar(Request $request, string $nickname, string $size)
|
||||||
{
|
{
|
||||||
switch ($size) {
|
switch ($size) {
|
||||||
case 'full':
|
case 'full':
|
||||||
$res = Media::getAvatarFileInfo($nickname);
|
$res = M::getAvatarFileInfo($nickname);
|
||||||
return Media::sendFile($res['file_path'], $res['mimetype'], $res['title']);
|
return M::sendFile($res['file_path'], $res['mimetype'], $res['title']);
|
||||||
default:
|
default:
|
||||||
throw new Exception('Not implemented');
|
throw new Exception('Not implemented');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function attachment_inline(Request $request, int $id)
|
||||||
|
{
|
||||||
|
$res = M::getAttachmentFileInfo($id);
|
||||||
|
return M::sendFile($res['file_path'], $res['mimetype'], $res['title']);
|
||||||
|
}
|
||||||
}
|
}
|
@ -34,7 +34,8 @@ class Media extends Module
|
|||||||
|
|
||||||
public function onAddRoute($r)
|
public function onAddRoute($r)
|
||||||
{
|
{
|
||||||
$r->connect('avatar', '/{nickname<' . Nickname::DISPLAY_FMT . '>}/avatar/{size<full|big|medium|small>?full}', [Controller\Avatar::class, 'send']);
|
$r->connect('avatar', '/{nickname<' . Nickname::DISPLAY_FMT . '>}/avatar/{size<full|big|medium|small>?full}', [Controller\Media::class, 'avatar']);
|
||||||
|
$r->connect('attachment_inline', '/attachment/{id<\d+>}', [Controller\Media::class, 'attachment_inline']);
|
||||||
return Event::next;
|
return Event::next;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -86,48 +86,66 @@ abstract class Utils
|
|||||||
return $response;
|
return $response;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function error($res, string $nickname)
|
public static function error($except, $id, array $res)
|
||||||
{
|
{
|
||||||
switch (count($res)) {
|
switch (count($res)) {
|
||||||
case 0:
|
case 0:
|
||||||
throw new NoAvatarException();
|
throw new $except();
|
||||||
case 1:
|
case 1:
|
||||||
return $res[0];
|
return $res[0];
|
||||||
default:
|
default:
|
||||||
Log::error('Avatar query returned more than one result for nickname ' . $nickname);
|
Log::error('Media query returned more than one result for identifier: \"' . $id . '\"');
|
||||||
throw new Exception(_m('Internal server error'));
|
throw new Exception(_m('Internal server error'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function getAvatar(string $nickname)
|
public static function getAvatar(string $nickname)
|
||||||
{
|
{
|
||||||
return self::error(
|
return self::error(NoAvatarException::class,
|
||||||
Cache::get('avatar-' . $nickname,
|
$nickname,
|
||||||
function () use ($nickname) {
|
Cache::get("avatar-{$nickname}",
|
||||||
return DB::dql('select a from App\\Entity\\Avatar a ' .
|
function () use ($nickname) {
|
||||||
'join App\Entity\GSActor g with a.gsactor_id = g.id ' .
|
return DB::dql('select a from App\\Entity\\Avatar a ' .
|
||||||
'where g.nickname = :nickname',
|
'join App\Entity\GSActor g with a.gsactor_id = g.id ' .
|
||||||
['nickname' => $nickname]);
|
'where g.nickname = :nickname',
|
||||||
}),
|
['nickname' => $nickname]);
|
||||||
$nickname
|
}));
|
||||||
);
|
}
|
||||||
|
|
||||||
|
public static function getFileInfo(int $id)
|
||||||
|
{
|
||||||
|
return self::error(NoSuchFileException::class,
|
||||||
|
$id,
|
||||||
|
Cache::get("file-info-{$id}",
|
||||||
|
function () use ($id) {
|
||||||
|
return DB::dql('select f.file_hash, f.mimetype, f.title ' .
|
||||||
|
'from App\\Entity\\File f ' .
|
||||||
|
'where f.id = :id',
|
||||||
|
['id' => $id]);
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getAttachmentFileInfo(int $id)
|
||||||
|
{
|
||||||
|
$res = self::getFileInfo($id);
|
||||||
|
$res['file_path'] = Common::config('attachments', 'dir') . $res['file_hash'];
|
||||||
|
return $res;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function getAvatarFileInfo(string $nickname)
|
public static function getAvatarFileInfo(string $nickname)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$res = self::error(
|
$res = self::error(NoAvatarException::class,
|
||||||
Cache::get('avatar-file-info-' . $nickname,
|
$nickname,
|
||||||
function () use ($nickname) {
|
Cache::get("avatar-file-info-{$nickname}",
|
||||||
return DB::dql('select f.file_hash, f.mimetype, f.title ' .
|
function () use ($nickname) {
|
||||||
'from App\\Entity\\File f ' .
|
return DB::dql('select f.file_hash, f.mimetype, f.title ' .
|
||||||
'join App\\Entity\\Avatar a with f.id = a.file_id ' .
|
'from App\\Entity\\File f ' .
|
||||||
'join App\\Entity\\GSActor g with g.id = a.gsactor_id ' .
|
'join App\\Entity\\Avatar a with f.id = a.file_id ' .
|
||||||
'where g.nickname = :nickname',
|
'join App\\Entity\\GSActor g with g.id = a.gsactor_id ' .
|
||||||
['nickname' => $nickname]);
|
'where g.nickname = :nickname',
|
||||||
}),
|
['nickname' => $nickname]);
|
||||||
$nickname
|
}));
|
||||||
);
|
|
||||||
$res['file_path'] = Avatar::getFilePathStatic($res['file_hash']);
|
$res['file_path'] = Avatar::getFilePathStatic($res['file_hash']);
|
||||||
return $res;
|
return $res;
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
@ -146,7 +164,7 @@ abstract class Utils
|
|||||||
throw new Exception('No user is logged in and no avatar provided to `getAvatarUrl`');
|
throw new Exception('No user is logged in and no avatar provided to `getAvatarUrl`');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Cache::get('avatar-url-' . $nickname, function () use ($nickname) {
|
return Cache::get("avatar-url-{$nickname}", function () use ($nickname) {
|
||||||
try {
|
try {
|
||||||
return self::getAvatar($nickname)->getUrl();
|
return self::getAvatar($nickname)->getUrl();
|
||||||
} catch (NoAvatarException $e) {
|
} catch (NoAvatarException $e) {
|
||||||
|
30
src/Util/Exception/NoSuchFileException.php
Normal file
30
src/Util/Exception/NoSuchFileException.php
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
// {{{ License
|
||||||
|
// This file is part of GNU social - https://www.gnu.org/software/social
|
||||||
|
//
|
||||||
|
// GNU social 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.
|
||||||
|
//
|
||||||
|
// GNU social 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 GNU social. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
// }}}
|
||||||
|
|
||||||
|
namespace App\Util\Exception;
|
||||||
|
|
||||||
|
use function App\Core\I18n\_m;
|
||||||
|
|
||||||
|
class NoSuchFileException extends ClientException
|
||||||
|
{
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
parent::__construct(_m('No such file found'));
|
||||||
|
}
|
||||||
|
}
|
@ -7,11 +7,17 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="note-content">
|
<div class="note-content">
|
||||||
{{ note.getContent() }}
|
{{ note.getContent() }}
|
||||||
{% for attachment in note.getAttachments() %}
|
<div class="note-attachments">
|
||||||
<div>
|
{% for attachment in note.getAttachments() %}
|
||||||
<i> {{ attachment.getTitle() }} </i>
|
{% if attachment.mimetype starts with 'image/' %}
|
||||||
</div>
|
<img src="{{ path('attachment_inline', {'id': attachment.getId()}) }}"> {{ attachment.getTitle() }} </img>
|
||||||
{% endfor %}
|
{% elseif attachment.mimetype starts with 'video/' %}
|
||||||
|
<video src="{{ path('attachment_inline', {'id': attachment.getId()}) }}"> {{ attachment.getTitle() }} </img>
|
||||||
|
{% else %}
|
||||||
|
<i> {{ attachment.getTitle() }} </i>
|
||||||
|
{% endif %}
|
||||||
|
{% endfor %}
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="note-actions">
|
<div class="note-actions">
|
||||||
{% for act in get_note_actions(note) %}
|
{% for act in get_note_actions(note) %}
|
||||||
|
Loading…
Reference in New Issue
Block a user