1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Swis\Filesystem\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 LocalEncryptedFilesystemServiceProvider extends ServiceProvider |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @throws \LogicException |
19
|
|
|
*/ |
20
|
60 |
|
public function boot() |
21
|
|
|
{ |
22
|
60 |
|
$this->registerStorageDriver(); |
23
|
60 |
|
$this->registerResponseMacro(); |
24
|
60 |
|
} |
25
|
|
|
|
26
|
60 |
|
protected function registerStorageDriver() |
27
|
|
|
{ |
28
|
60 |
|
Storage::extend( |
29
|
60 |
|
'local-encrypted', |
30
|
|
|
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
|
60 |
|
} |
50
|
|
|
); |
51
|
60 |
|
} |
52
|
|
|
|
53
|
60 |
|
protected function registerResponseMacro() |
54
|
|
|
{ |
55
|
60 |
|
Response::macro( |
56
|
60 |
|
'downloadEncrypted', |
57
|
|
|
/** |
58
|
|
|
* @param \SplFileInfo|string $file |
59
|
|
|
* @param null $name |
|
|
|
|
60
|
|
|
* @param array $headers |
61
|
|
|
* @param string $disposition |
62
|
|
|
* |
63
|
|
|
* @return \Swis\Filesystem\Encrypted\BinaryFileResponse |
64
|
|
|
*/ |
65
|
|
|
function ($file, $name = null, array $headers = [], $disposition = 'attachment') { |
66
|
3 |
|
$response = new BinaryFileResponse($file, 200, $headers, false, $disposition); |
67
|
|
|
|
68
|
3 |
|
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
|
|
|
return $response; |
77
|
60 |
|
} |
78
|
|
|
); |
79
|
60 |
|
} |
80
|
|
|
} |
81
|
|
|
|