1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\PersonalDataDownload\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Storage; |
6
|
|
|
use Symfony\Component\HttpFoundation\Response; |
7
|
|
|
use Symfony\Component\HttpFoundation\StreamedResponse; |
8
|
|
|
|
9
|
|
|
class PersonalDataDownloadController |
10
|
|
|
{ |
11
|
|
|
public function __invoke(string $zipFilename): StreamedResponse |
12
|
|
|
{ |
13
|
|
|
$this->ensureAuthorizedToDownload($zipFilename); |
14
|
|
|
|
15
|
|
|
return $this->responseStream($zipFilename); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
protected function ensureAuthorizedToDownload(string $zipFilename) |
19
|
|
|
{ |
20
|
|
|
if (! config('personal-data-download.authentication_required')) { |
21
|
|
|
return; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
if (! auth()->check()) { |
|
|
|
|
25
|
|
|
abort(Response::HTTP_FORBIDDEN); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
[$owningUserId] = explode('-', $zipFilename); |
|
|
|
|
29
|
|
|
|
30
|
|
|
if ($owningUserId != auth()->user()->id) { |
|
|
|
|
31
|
|
|
abort(Response::HTTP_UNAUTHORIZED); |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
protected function responseStream(string $filename): StreamedResponse |
36
|
|
|
{ |
37
|
|
|
$disk = Storage::disk(config('personal-data-download.disk')); |
38
|
|
|
|
39
|
|
|
if (! $disk->exists($filename)) { |
40
|
|
|
abort(404); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$downloadHeaders = [ |
44
|
|
|
'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0', |
45
|
|
|
'Content-Type' => 'application/zip', |
46
|
|
|
'Content-Length' => $disk->size($filename), |
47
|
|
|
'Content-Disposition' => 'attachment; filename="'.$filename.'"', |
48
|
|
|
'Pragma' => 'public', |
49
|
|
|
]; |
50
|
|
|
|
51
|
|
|
return response()->stream(function () use ($filename, $disk) { |
|
|
|
|
52
|
|
|
$stream = $disk->readStream($filename); |
53
|
|
|
|
54
|
|
|
fpassthru($stream); |
55
|
|
|
|
56
|
|
|
if (is_resource($stream)) { |
57
|
|
|
fclose($stream); |
58
|
|
|
} |
59
|
|
|
}, 200, $downloadHeaders); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: