1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\PersonalDataExport\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Routing\Controller; |
6
|
|
|
use Illuminate\Support\Facades\Storage; |
7
|
|
|
use Spatie\PersonalDataExport\Http\Middleware\EnsureAuthorizedToDownload; |
8
|
|
|
use Spatie\PersonalDataExport\Http\Middleware\FiresPersonalDataExportDownloadedEvent; |
9
|
|
|
use Symfony\Component\HttpFoundation\StreamedResponse; |
10
|
|
|
|
11
|
|
|
class PersonalDataExportController extends Controller |
12
|
|
|
{ |
13
|
|
|
public function __construct() |
14
|
|
|
{ |
15
|
|
|
$this->middleware(EnsureAuthorizedToDownload::class); |
16
|
|
|
$this->middleware(FiresPersonalDataExportDownloadedEvent::class); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function __invoke(string $zipFilename): StreamedResponse |
20
|
|
|
{ |
21
|
|
|
return $this->responseStream($zipFilename); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
protected function responseStream(string $filename): StreamedResponse |
25
|
|
|
{ |
26
|
|
|
$disk = Storage::disk(config('personal-data-export.disk')); |
27
|
|
|
|
28
|
|
|
if (! $disk->exists($filename)) { |
29
|
|
|
abort(404); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$downloadFilename = auth()->user() |
|
|
|
|
33
|
|
|
? auth()->user()->personalDataExportName() |
34
|
|
|
: $filename; |
35
|
|
|
|
36
|
|
|
$downloadHeaders = [ |
37
|
|
|
'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0', |
38
|
|
|
'Content-Type' => 'application/zip', |
39
|
|
|
'Content-Length' => $disk->size($filename), |
40
|
|
|
'Content-Disposition' => 'attachment; filename="' . $downloadFilename . '"', |
41
|
|
|
'Pragma' => 'public', |
42
|
|
|
]; |
43
|
|
|
|
44
|
|
|
return response()->stream(function () use ($filename, $disk) { |
|
|
|
|
45
|
|
|
$stream = $disk->readStream($filename); |
46
|
|
|
|
47
|
|
|
fpassthru($stream); |
48
|
|
|
|
49
|
|
|
if (is_resource($stream)) { |
50
|
|
|
fclose($stream); |
51
|
|
|
} |
52
|
|
|
}, 200, $downloadHeaders); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
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: