2021-04-18 02:17:57 +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 App\Core;
|
|
|
|
|
|
|
|
use App\Core\DB\DB;
|
|
|
|
use function App\Core\I18n\_m;
|
|
|
|
use App\Entity\Attachment;
|
|
|
|
use App\Util\Common;
|
|
|
|
use App\Util\Exception\ClientException;
|
|
|
|
use App\Util\Exception\NoSuchFileException;
|
2021-05-01 14:02:14 +01:00
|
|
|
use App\Util\Exception\ServerException;
|
2021-04-18 02:17:57 +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;
|
|
|
|
|
|
|
|
class GSFile
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Perform file validation (checks and normalization) and store the given file
|
|
|
|
*/
|
2021-05-01 22:14:38 +01:00
|
|
|
public static function validateAndStoreFileAsAttachment(SymfonyFile $sfile,
|
|
|
|
string $dest_dir,
|
|
|
|
?string $title = null,
|
|
|
|
bool $is_local = true,
|
|
|
|
int $actor_id = null): Attachment
|
2021-04-18 02:17:57 +01:00
|
|
|
{
|
2021-04-28 21:16:59 +01:00
|
|
|
Event::handle('HashFile', [$sfile->getPathname(), &$hash]);
|
2021-04-18 02:17:57 +01:00
|
|
|
// The following properly gets the mimetype with `file` or other
|
|
|
|
// available methods, so should be safe
|
2021-04-18 05:47:16 +01:00
|
|
|
$mimetype = $sfile->getMimeType();
|
2021-04-29 21:29:21 +01:00
|
|
|
Event::handle('AttachmentValidation', [&$sfile, &$mimetype, &$title]);
|
2021-04-18 05:47:16 +01:00
|
|
|
$attachment = Attachment::create([
|
2021-04-18 02:17:57 +01:00
|
|
|
'file_hash' => $hash,
|
|
|
|
'gsactor_id' => $actor_id,
|
2021-04-18 05:47:16 +01:00
|
|
|
'mimetype' => $mimetype,
|
2021-04-18 02:17:57 +01:00
|
|
|
'title' => $title ?: _m('Untitled attachment'),
|
|
|
|
'filename' => $hash,
|
|
|
|
'is_local' => $is_local,
|
2021-04-29 21:29:21 +01:00
|
|
|
'size' => $sfile->getSize(),
|
2021-04-18 02:17:57 +01:00
|
|
|
]);
|
|
|
|
$sfile->move($dest_dir, $hash);
|
2021-04-25 22:14:35 +01:00
|
|
|
DB::persist($attachment);
|
|
|
|
Event::handle('AttachmentStoreNew', [&$attachment]);
|
2021-04-18 05:47:16 +01:00
|
|
|
return $attachment;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-04-25 22:14:35 +01:00
|
|
|
* Create an attachment for the given URL, fetching the mimetype
|
|
|
|
*
|
|
|
|
* @throws \InvalidArgumentException
|
2021-04-18 05:47:16 +01:00
|
|
|
*/
|
2021-05-01 22:14:38 +01:00
|
|
|
public static function validateAndStoreURLAsAttachment(string $url): Attachment
|
2021-04-18 05:47:16 +01:00
|
|
|
{
|
2021-04-25 22:14:35 +01:00
|
|
|
if (Common::isValidHttpUrl($url)) {
|
2021-04-27 21:53:59 +01:00
|
|
|
$head = HTTPClient::head($url);
|
2021-04-25 22:14:35 +01:00
|
|
|
$headers = $head->getHeaders();
|
|
|
|
$headers = array_change_key_case($headers, CASE_LOWER);
|
|
|
|
$attachment = Attachment::create([
|
2021-04-27 21:53:59 +01:00
|
|
|
'remote_url' => $url,
|
|
|
|
'remote_url_hash' => hash(Attachment::URLHASH_ALGO, $url),
|
|
|
|
'mimetype' => $headers['content-type'][0],
|
|
|
|
'is_local' => false,
|
2021-04-25 22:14:35 +01:00
|
|
|
]);
|
|
|
|
DB::persist($attachment);
|
2021-04-27 21:53:59 +01:00
|
|
|
Event::handle('AttachmentStoreNew', [&$attachment]);
|
2021-04-25 22:14:35 +01:00
|
|
|
return $attachment;
|
|
|
|
} else {
|
|
|
|
throw new \InvalidArgumentException();
|
|
|
|
}
|
2021-04-18 02:17:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
{
|
2021-05-01 14:02:14 +01:00
|
|
|
if (is_file($filepath)) {
|
|
|
|
$response = new BinaryFileResponse(
|
|
|
|
$filepath,
|
|
|
|
Response::HTTP_OK,
|
|
|
|
[
|
|
|
|
'Content-Description' => 'File Transfer',
|
|
|
|
'Content-Type' => $mimetype,
|
|
|
|
'Content-Disposition' => HeaderUtils::makeDisposition($disposition, $output_filename ?: _m('Untitled attachment'), _m('Untitled attachment')),
|
|
|
|
'Cache-Control' => 'public',
|
|
|
|
],
|
|
|
|
$public = true,
|
|
|
|
$disposition = null,
|
|
|
|
$add_etag = true,
|
|
|
|
$add_last_modified = true
|
|
|
|
);
|
|
|
|
if (Common::config('site', 'x_static_delivery')) {
|
|
|
|
$response->trustXSendfileTypeHeader();
|
|
|
|
}
|
|
|
|
return $response;
|
|
|
|
} else {
|
|
|
|
throw new ServerException(_m('This attachment is not stored locally'));
|
2021-04-18 02:17:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Throw a client exception if the cache key $id doesn't contain
|
|
|
|
* exactly one entry
|
|
|
|
*
|
|
|
|
* @param mixed $except
|
|
|
|
* @param mixed $id
|
|
|
|
*/
|
|
|
|
public static function error($except, $id, array $res)
|
|
|
|
{
|
|
|
|
switch (count($res)) {
|
|
|
|
case 0:
|
|
|
|
throw new $except();
|
|
|
|
case 1:
|
|
|
|
return $res[0];
|
|
|
|
default:
|
|
|
|
Log::error('Media query returned more than one result for identifier: \"' . $id . '\"');
|
|
|
|
throw new ClientException(_m('Internal server error'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the file info by id
|
|
|
|
*
|
|
|
|
* Returns the file's hash, mimetype and title
|
|
|
|
*/
|
|
|
|
public static function getFileInfo(int $id)
|
|
|
|
{
|
|
|
|
return self::error(NoSuchFileException::class,
|
|
|
|
$id,
|
|
|
|
Cache::get("file-info-{$id}",
|
|
|
|
function () use ($id) {
|
|
|
|
return DB::dql('select at.file_hash, at.mimetype, at.title ' .
|
|
|
|
'from App\\Entity\\Attachment at ' .
|
|
|
|
'where at.id = :id',
|
|
|
|
['id' => $id]);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----- 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
|
|
|
|
{
|
2021-04-28 16:01:40 +01:00
|
|
|
$res = self::getFileInfo($id);
|
|
|
|
$res['filepath'] = Common::config('attachments', 'dir') . $res['file_hash'];
|
2021-04-18 02:17:57 +01:00
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the minor part of a mimetype. image/webp -> image
|
|
|
|
*/
|
|
|
|
public static function mimetypeMajor(string $mime)
|
|
|
|
{
|
2021-04-25 22:14:35 +01:00
|
|
|
return explode('/', self::mimetypeBare($mime))[0];
|
2021-04-18 02:17:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the minor part of a mimetype. image/webp -> webp
|
|
|
|
*/
|
|
|
|
public static function mimetypeMinor(string $mime)
|
|
|
|
{
|
2021-04-25 22:14:35 +01:00
|
|
|
return explode('/', self::mimetypeBare($mime))[1];
|
2021-04-18 02:17:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get only the mimetype and not additional info (separated from bare mime with semi-colon)
|
|
|
|
*/
|
2021-04-25 22:14:35 +01:00
|
|
|
public static function mimetypeBare(string $mimetype)
|
2021-04-18 02:17:57 +01:00
|
|
|
{
|
|
|
|
$mimetype = mb_strtolower($mimetype);
|
|
|
|
if (($semicolon = mb_strpos($mimetype, ';')) !== false) {
|
|
|
|
$mimetype = mb_substr($mimetype, 0, $semicolon);
|
|
|
|
}
|
|
|
|
return trim($mimetype);
|
|
|
|
}
|
|
|
|
}
|