in image share link, return s3 url address of image

This commit is contained in:
Čarodej
2022-04-07 09:11:49 +02:00
parent ade6b05bd0
commit 7bd1079ce9
4 changed files with 91 additions and 91 deletions
@@ -18,12 +18,10 @@ class DownloadFileAction
$filePath = "files/$file->user_id/$file->basename";
// Get pretty name
$fileName = getPrettyName($file->basename, $file->name, $file->mimetype);
$fileName = getPrettyName($file);
// Check if file exist
if (! Storage::exists($filePath)) {
return response('The file not found.', 404);
}
abort_if(Storage::missing($filePath), 404, 'The file not found.');
// Format response header
$header = [
@@ -58,17 +58,24 @@ class SharePublicIndexController extends Controller
/**
* Get image from storage and show it
*/
private function get_single_image(File $file, string $user_id): StreamedResponse
private function get_single_image(File $file, string $userId): StreamedResponse|RedirectResponse
{
// Store user download size
($this->recordDownload)($file->getRawOriginal('filesize'), $user_id);
// Record user download
($this->recordDownload)($file->filesize, $userId);
// Get file path
$path = "/files/$user_id/$file->basename";
$path = "/files/$userId/$file->basename";
// Check if file exist
if (! Storage::exists($path)) {
abort(404);
abort_if(Storage::missing($path), 404);
// If s3 redirect to temporary download url
if (isStorageDriver('s3')) {
return redirect()->away(Storage::temporaryUrl($path, now()->addHour(), [
'ResponseAcceptRanges' => 'bytes',
'ResponseContentType' => Storage::mimeType($path),
'ResponseContentLength' => Storage::size($path),
]));
}
return Storage::response($path, "{$file->name}.{$file->mimetype}", [
+6 -11
View File
@@ -728,25 +728,20 @@ if (! function_exists('get_file_type_from_mimetype')) {
if (! function_exists('getPrettyName')) {
/**
* Format pretty name file
*
* @param $basename
* @param $name
* @param $mimetype
* @return string
*/
function getPrettyName($basename, $name, $mimetype): string
function getPrettyName(File $file): string
{
$file_extension = substr(strrchr($basename, '.'), 1);
$file_extension = substr(strrchr($file->basename, '.'), 1);
if (str_contains($name, $file_extension)) {
return $name;
if (str_contains($file->name, $file_extension)) {
return $file->name;
}
if ($file_extension) {
return $name . '.' . $file_extension;
return $file->name . '.' . $file_extension;
}
return $name . '.' . $mimetype;
return $file->name . '.' . $file->mimetype;
}
}