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
|
|
|
if ($user = auth()->user()) { |
|
|
|
|
44
|
|
|
if (method_exists($user, 'getPersonalDataDownloadName')) { |
45
|
|
|
$filename = $user->getPersonalDataDownloadName($filename); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$downloadHeaders = [ |
50
|
|
|
'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0', |
51
|
|
|
'Content-Type' => 'application/zip', |
52
|
|
|
'Content-Length' => $disk->size($filename), |
53
|
|
|
'Content-Disposition' => 'attachment; filename="'.$filename.'"', |
54
|
|
|
'Pragma' => 'public', |
55
|
|
|
]; |
56
|
|
|
|
57
|
|
|
return response()->stream(function () use ($filename, $disk) { |
|
|
|
|
58
|
|
|
$stream = $disk->readStream($filename); |
59
|
|
|
|
60
|
|
|
fpassthru($stream); |
61
|
|
|
|
62
|
|
|
if (is_resource($stream)) { |
63
|
|
|
fclose($stream); |
64
|
|
|
} |
65
|
|
|
}, 200, $downloadHeaders); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
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: