- s3 direct download

This commit is contained in:
Čarodej
2022-03-25 19:14:32 +01:00
parent e232543ea4
commit bbd8e70c9d
5 changed files with 37 additions and 17 deletions
@@ -3,29 +3,28 @@
namespace Domain\Sharing\Controllers;
use App\Http\Controllers\Controller;
use Domain\Files\Actions\DownloadFileAction;
use Domain\Files\Models\File;
use Domain\Sharing\Actions\ProtectShareRecordAction;
use Domain\Sharing\Actions\VerifyAccessToItemWithinAction;
use Domain\Sharing\Models\Share;
use Domain\Traffic\Actions\RecordDownloadAction;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Response;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Illuminate\Support\Facades\Storage;
use Symfony\Component\HttpFoundation\StreamedResponse;
class DirectlyDownloadFileController extends Controller
{
public function __construct(
private DownloadFileAction $downloadFile,
private RecordDownloadAction $recordDownload,
private ProtectShareRecordAction $protectShareRecord,
private RecordDownloadAction $recordDownload,
private ProtectShareRecordAction $protectShareRecord,
private VerifyAccessToItemWithinAction $verifyAccessToItemWithin,
) {
}
){}
public function __invoke(
Share $share
): BinaryFileResponse|Response {
): Response|StreamedResponse|RedirectResponse
{
// Check if item is not a folder
if ($share->type !== 'file') {
return response('This content is not downloadable');
@@ -35,7 +34,7 @@ class DirectlyDownloadFileController extends Controller
($this->protectShareRecord)($share);
// Check if user can download file
if (! $share->user->canDownload()) {
if (!$share->user->canDownload()) {
return response([
'type' => 'error',
'message' => 'This user action is not allowed.',
@@ -52,11 +51,32 @@ class DirectlyDownloadFileController extends Controller
// Store user download size
($this->recordDownload)(
file_size: (int) $file->getRawOriginal('filesize'),
file_size: (int)$file->getRawOriginal('filesize'),
user_id: $share->user_id,
);
// Finally, download file
return ($this->downloadFile)($file, $share->user_id);
// Get file path
$path = "files/$share->user_id/$file->basename";
// Check if file exist
if (!Storage::exists($path)) {
abort(404);
}
// Get pretty name
$pretty_name = get_pretty_name($file->basename, $file->name, $file->mimetype);
// If s3 redirect to temporary url
if (is_storage_driver('s3')) {
return redirect()->away(Storage::temporaryUrl($path, now()->addHour(), [
'ResponseAcceptRanges' => 'bytes',
'ResponseContentType' => Storage::mimeType($path),
'ResponseContentLength' => Storage::size($path),
'ResponseContentRange' => 'bytes 0-600/' . Storage::size($path),
'ResponseContentDisposition' => "attachment; filename=$pretty_name",
]));
}
return Storage::download($path, $pretty_name);
}
}