Passed
Push — master ( f82ba1...247912 )
by Ben
08:55
created

ZipAction::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Thinktomorrow\Chief\Mediagallery\Application;
5
6
use ZipStream\ZipStream;
7
use Illuminate\Http\Request;
8
use ZipStream\Option\Archive;
9
use Thinktomorrow\AssetLibrary\Asset;
10
11
final class ZipAction
12
{
13
    public function __construct()
14
    {
15
16
    }
17
18
    public function handle(string $filename, Request $request)
19
    {
20
        $assets = Asset::whereIn('id', (array) $request->input('asset_ids', []))->get();
21
22
        // enable output of HTTP headers TODO: this should be moved to the controller instead...
23
        $options = new Archive();
24
        $options->setSendHttpHeaders(true);
25
26
        $zip = new ZipStream($filename, $options);
27
28
        $assets->each(function(Asset $asset) use($zip){
29
            if(file_exists($asset->getFirstMediaPath())) {
30
                $zip->addFileFromPath($asset->filename(), $asset->getFirstMediaPath());
31
            }
32
        });
33
34
        // Output the zip as download
35
        $zip->finish();
36
    }
37
}
38