2020-08-14 23:34:58 +01:00
|
|
|
<?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 Component\Media;
|
|
|
|
|
|
|
|
use App\Core\Cache;
|
|
|
|
use App\Core\DB\DB;
|
|
|
|
use function App\Core\I18n\_m;
|
|
|
|
use App\Core\Log;
|
2021-04-15 23:28:28 +01:00
|
|
|
use App\Entity\Attachment;
|
2020-08-14 23:34:58 +01:00
|
|
|
use App\Entity\Avatar;
|
|
|
|
use App\Util\Common;
|
2020-11-06 19:47:15 +00:00
|
|
|
use App\Util\Exception\ClientException;
|
2020-08-19 16:31:52 +01:00
|
|
|
use Component\Media\Exception\NoAvatarException;
|
2020-08-14 23:34:58 +01:00
|
|
|
use Exception;
|
2020-08-19 16:31:52 +01:00
|
|
|
use Symfony\Component\Asset\Package;
|
|
|
|
use Symfony\Component\Asset\VersionStrategy\EmptyVersionStrategy;
|
2020-08-14 23:34:58 +01:00
|
|
|
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
|
|
|
use Symfony\Component\HttpFoundation\File\File as SymfonyFile;
|
|
|
|
use Symfony\Component\HttpFoundation\HeaderUtils;
|
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
|
|
|
|
abstract class Utils
|
|
|
|
{
|
2020-11-06 19:47:15 +00:00
|
|
|
/**
|
|
|
|
* Perform file validation (checks and normalization) and store the given file
|
|
|
|
*/
|
2021-04-15 23:28:28 +01:00
|
|
|
public static function validateAndStoreAttachment(SymfonyFile $sfile,
|
|
|
|
string $dest_dir,
|
|
|
|
?string $title = null,
|
|
|
|
bool $is_local = true,
|
|
|
|
?int $actor_id = null): Attachment
|
2020-08-14 23:34:58 +01:00
|
|
|
{
|
|
|
|
// The following properly gets the mimetype with `file` or other
|
|
|
|
// available methods, so should be safe
|
2021-04-15 23:28:28 +01:00
|
|
|
$hash = hash_file(Attachment::FILEHASH_ALGO, $sfile->getPathname());
|
|
|
|
$file = Attachment::create([
|
2020-08-14 23:34:58 +01:00
|
|
|
'file_hash' => $hash,
|
2020-08-15 06:15:36 +01:00
|
|
|
'actor_id' => $actor_id,
|
2020-08-14 23:34:58 +01:00
|
|
|
'mimetype' => $sfile->getMimeType(),
|
|
|
|
'title' => $title ?: _m('Untitled attachment'),
|
2021-04-15 23:28:28 +01:00
|
|
|
'filename' => $hash,
|
2020-08-14 23:34:58 +01:00
|
|
|
'is_local' => $is_local,
|
|
|
|
]);
|
|
|
|
$sfile->move($dest_dir, $hash);
|
|
|
|
// TODO Normalize file types
|
|
|
|
return $file;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Include $filepath in the response, for viewing or downloading.
|
|
|
|
*
|
|
|
|
* @throws ServerException
|
|
|
|
*/
|
|
|
|
public static function sendFile(string $filepath, string $mimetype, ?string $output_filename, string $disposition = 'inline'): Response
|
|
|
|
{
|
|
|
|
$response = new BinaryFileResponse(
|
|
|
|
$filepath,
|
|
|
|
Response::HTTP_OK,
|
|
|
|
[
|
|
|
|
'Content-Description' => 'File Transfer',
|
|
|
|
'Content-Type' => $mimetype,
|
2021-04-15 23:28:28 +01:00
|
|
|
'Content-Disposition' => HeaderUtils::makeDisposition($disposition, $output_filename ?: _m('Untitled attachment')),
|
2020-09-05 22:18:21 +01:00
|
|
|
'Cache-Control' => 'public',
|
2020-08-14 23:34:58 +01:00
|
|
|
],
|
|
|
|
$public = true,
|
|
|
|
$disposition = null,
|
|
|
|
$add_etag = true,
|
|
|
|
$add_last_modified = true
|
|
|
|
);
|
|
|
|
if (Common::config('site', 'x_static_delivery')) {
|
|
|
|
$response->trustXSendfileTypeHeader();
|
|
|
|
}
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2020-11-06 19:47:15 +00:00
|
|
|
/**
|
|
|
|
* Throw a client exception if the cache key $id doesn't contain
|
|
|
|
* exactly one entry
|
|
|
|
*
|
|
|
|
* @param mixed $except
|
|
|
|
* @param mixed $id
|
|
|
|
*/
|
|
|
|
private static function error($except, $id, array $res)
|
2020-08-14 23:34:58 +01:00
|
|
|
{
|
2020-08-19 16:31:52 +01:00
|
|
|
switch (count($res)) {
|
|
|
|
case 0:
|
2020-09-05 03:28:50 +01:00
|
|
|
throw new $except();
|
2020-08-19 16:31:52 +01:00
|
|
|
case 1:
|
|
|
|
return $res[0];
|
|
|
|
default:
|
2020-09-05 03:28:50 +01:00
|
|
|
Log::error('Media query returned more than one result for identifier: \"' . $id . '\"');
|
2020-11-06 19:47:15 +00:00
|
|
|
throw new ClientException(_m('Internal server error'));
|
2020-08-14 23:34:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-06 19:47:15 +00:00
|
|
|
/**
|
|
|
|
* Get the file info by id
|
|
|
|
*
|
|
|
|
* Returns the file's hash, mimetype and title
|
|
|
|
*/
|
2020-09-05 03:28:50 +01:00
|
|
|
public static function getFileInfo(int $id)
|
|
|
|
{
|
|
|
|
return self::error(NoSuchFileException::class,
|
|
|
|
$id,
|
|
|
|
Cache::get("file-info-{$id}",
|
|
|
|
function () use ($id) {
|
2021-04-15 23:28:28 +01:00
|
|
|
return DB::dql('select at.file_hash, at.mimetype, at.title ' .
|
|
|
|
'from App\\Entity\\Attachment at ' .
|
|
|
|
'where at.id = :id',
|
2020-09-05 03:28:50 +01:00
|
|
|
['id' => $id]);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2020-11-06 19:47:15 +00:00
|
|
|
// ----- Attachment ------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the attachment file info by id
|
|
|
|
*
|
|
|
|
* Returns the attachment file's hash, mimetype, title and path
|
|
|
|
*/
|
|
|
|
public static function getAttachmentFileInfo(int $id): array
|
2020-09-05 03:28:50 +01:00
|
|
|
{
|
|
|
|
$res = self::getFileInfo($id);
|
|
|
|
$res['file_path'] = Common::config('attachments', 'dir') . $res['file_hash'];
|
|
|
|
return $res;
|
2020-08-14 23:34:58 +01:00
|
|
|
}
|
|
|
|
|
2020-11-06 19:47:15 +00:00
|
|
|
// ----- Avatar ------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the avatar associated with the given nickname
|
|
|
|
*/
|
|
|
|
public static function getAvatar(?string $nickname = null): Avatar
|
|
|
|
{
|
|
|
|
$nickname = $nickname ?: Common::userNickname();
|
|
|
|
return self::error(NoAvatarException::class,
|
|
|
|
$nickname,
|
|
|
|
Cache::get("avatar-{$nickname}",
|
|
|
|
function () use ($nickname) {
|
|
|
|
return DB::dql('select a from App\\Entity\\Avatar a ' .
|
|
|
|
'join App\Entity\GSActor g with a.gsactor_id = g.id ' .
|
|
|
|
'where g.nickname = :nickname',
|
|
|
|
['nickname' => $nickname]);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the cached avatar associated with the given nickname, or the current user if not given
|
|
|
|
*/
|
|
|
|
public static function getAvatarUrl(?string $nickname = null): string
|
|
|
|
{
|
|
|
|
$nickname = $nickname ?: Common::userNickname();
|
|
|
|
return Cache::get("avatar-url-{$nickname}", function () use ($nickname) {
|
|
|
|
try {
|
|
|
|
return self::getAvatar($nickname)->getUrl();
|
|
|
|
} catch (NoAvatarException $e) {
|
|
|
|
}
|
|
|
|
$package = new Package(new EmptyVersionStrategy());
|
|
|
|
return $package->getUrl(Common::config('avatar', 'default'));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the cached avatar file info associated with the given nickname
|
|
|
|
*
|
|
|
|
* Returns the avatar file's hash, mimetype, title and path.
|
|
|
|
* Ensures exactly one cached value exists
|
|
|
|
*/
|
|
|
|
public static function getAvatarFileInfo(string $nickname): array
|
2020-08-14 23:34:58 +01:00
|
|
|
{
|
|
|
|
try {
|
2020-09-05 03:28:50 +01:00
|
|
|
$res = self::error(NoAvatarException::class,
|
|
|
|
$nickname,
|
|
|
|
Cache::get("avatar-file-info-{$nickname}",
|
|
|
|
function () use ($nickname) {
|
|
|
|
return DB::dql('select f.file_hash, f.mimetype, f.title ' .
|
2021-04-15 23:28:28 +01:00
|
|
|
'from App\\Entity\\Attachment f ' .
|
2020-09-05 03:28:50 +01:00
|
|
|
'join App\\Entity\\Avatar a with f.id = a.file_id ' .
|
|
|
|
'join App\\Entity\\GSActor g with g.id = a.gsactor_id ' .
|
|
|
|
'where g.nickname = :nickname',
|
|
|
|
['nickname' => $nickname]);
|
|
|
|
}));
|
2020-08-14 23:34:58 +01:00
|
|
|
$res['file_path'] = Avatar::getFilePathStatic($res['file_hash']);
|
|
|
|
return $res;
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$filepath = INSTALLDIR . '/public/assets/default-avatar.svg';
|
|
|
|
return ['file_path' => $filepath, 'mimetype' => 'image/svg+xml', 'title' => null];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|