|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Swis\Laravel\Encrypted; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Foundation\Application; |
|
6
|
|
|
use Illuminate\Support\Arr; |
|
7
|
|
|
use Illuminate\Support\Facades\Response; |
|
8
|
|
|
use Illuminate\Support\Facades\Storage; |
|
9
|
|
|
use Illuminate\Support\ServiceProvider; |
|
10
|
|
|
use Illuminate\Support\Str; |
|
11
|
|
|
use League\Flysystem\Adapter\Local as LocalAdapter; |
|
12
|
|
|
use League\Flysystem\Filesystem; |
|
13
|
|
|
use Swis\Flysystem\Encrypted\EncryptedAdapter; |
|
14
|
|
|
|
|
15
|
|
|
class EncryptedDataServiceProvider extends ServiceProvider |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @throws \LogicException |
|
19
|
|
|
*/ |
|
20
|
87 |
|
public function boot() |
|
21
|
|
|
{ |
|
22
|
87 |
|
$this->registerStorageDriver(); |
|
23
|
87 |
|
$this->registerResponseMacro(); |
|
24
|
87 |
|
} |
|
25
|
|
|
|
|
26
|
87 |
|
protected function registerStorageDriver() |
|
27
|
|
|
{ |
|
28
|
87 |
|
Storage::extend( |
|
29
|
87 |
|
'local-encrypted', |
|
30
|
58 |
|
function (Application $app, array $config) { |
|
31
|
3 |
|
$permissions = $config['permissions'] ?? []; |
|
32
|
|
|
|
|
33
|
3 |
|
$links = ($config['links'] ?? null) === 'skip' |
|
34
|
|
|
? LocalAdapter::SKIP_LINKS |
|
35
|
3 |
|
: LocalAdapter::DISALLOW_LINKS; |
|
36
|
|
|
|
|
37
|
3 |
|
return new Filesystem( |
|
38
|
3 |
|
new EncryptedAdapter( |
|
39
|
3 |
|
new LocalAdapter( |
|
40
|
3 |
|
$config['root'], |
|
41
|
3 |
|
$config['lock'] ?? LOCK_EX, |
|
42
|
1 |
|
$links, |
|
43
|
1 |
|
$permissions |
|
44
|
|
|
), |
|
45
|
3 |
|
$app->make('encrypter') |
|
46
|
|
|
), |
|
47
|
3 |
|
Arr::only($config, ['visibility', 'disable_asserts', 'url']) |
|
48
|
|
|
); |
|
49
|
87 |
|
} |
|
50
|
|
|
); |
|
51
|
87 |
|
} |
|
52
|
|
|
|
|
53
|
87 |
|
protected function registerResponseMacro() |
|
54
|
|
|
{ |
|
55
|
87 |
|
Response::macro( |
|
56
|
87 |
|
'downloadEncrypted', |
|
57
|
|
|
/** |
|
58
|
|
|
* @param \SplFileInfo|string $file |
|
59
|
|
|
* @param string|null $name |
|
60
|
|
|
* @param array $headers |
|
61
|
|
|
* @param string|null $disposition |
|
62
|
|
|
* |
|
63
|
|
|
* @return \Swis\Laravel\Encrypted\BinaryFileResponse |
|
64
|
|
|
*/ |
|
65
|
58 |
|
function ($file, $name = null, array $headers = [], $disposition = 'attachment') { |
|
66
|
12 |
|
$response = new BinaryFileResponse($file, 200, $headers, false, $disposition); |
|
67
|
|
|
|
|
68
|
12 |
|
if (null !== $name) { |
|
69
|
3 |
|
return $response->setContentDisposition( |
|
70
|
3 |
|
$disposition, |
|
|
|
|
|
|
71
|
1 |
|
$name, |
|
72
|
3 |
|
str_replace('%', '', Str::ascii($name)) |
|
73
|
|
|
); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
9 |
|
return $response; |
|
77
|
87 |
|
} |
|
78
|
|
|
); |
|
79
|
87 |
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|