1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Swis\Laravel\Encrypted; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Foundation\Application; |
6
|
|
|
use Illuminate\Filesystem\FilesystemAdapter; |
7
|
|
|
use Illuminate\Support\Arr; |
8
|
|
|
use Illuminate\Support\Facades\Storage; |
9
|
|
|
use Illuminate\Support\ServiceProvider; |
10
|
|
|
use League\Flysystem\Filesystem; |
11
|
|
|
use League\Flysystem\Local\LocalFilesystemAdapter; |
12
|
|
|
use League\Flysystem\UnixVisibility\PortableVisibilityConverter; |
13
|
|
|
use League\Flysystem\Visibility; |
14
|
|
|
use Swis\Flysystem\Encrypted\EncryptedFilesystemAdapter; |
15
|
|
|
|
16
|
|
|
class EncryptedDataServiceProvider extends ServiceProvider |
17
|
|
|
{ |
18
|
14 |
|
public function register(): void |
19
|
|
|
{ |
20
|
14 |
|
$this->registerEncrypter(); |
21
|
7 |
|
} |
22
|
|
|
|
23
|
14 |
|
protected function registerEncrypter(): void |
24
|
|
|
{ |
25
|
14 |
|
$this->app->alias('encrypter', 'encrypted-data.encrypter'); |
26
|
7 |
|
} |
27
|
|
|
|
28
|
14 |
|
public function boot(): void |
29
|
|
|
{ |
30
|
14 |
|
$this->setupStorageDriver(); |
31
|
7 |
|
} |
32
|
|
|
|
33
|
14 |
|
protected function setupStorageDriver(): void |
34
|
|
|
{ |
35
|
14 |
|
Storage::extend( |
36
|
14 |
|
'local-encrypted', |
37
|
14 |
|
function (Application $app, array $config) { |
38
|
2 |
|
$visibility = PortableVisibilityConverter::fromArray( |
39
|
2 |
|
$config['permissions'] ?? [], |
40
|
2 |
|
$config['directory_visibility'] ?? $config['visibility'] ?? Visibility::PRIVATE |
41
|
1 |
|
); |
42
|
|
|
|
43
|
2 |
|
$links = ($config['links'] ?? null) === 'skip' |
44
|
|
|
? LocalFilesystemAdapter::SKIP_LINKS |
45
|
2 |
|
: LocalFilesystemAdapter::DISALLOW_LINKS; |
46
|
|
|
|
47
|
2 |
|
$adapter = new EncryptedFilesystemAdapter( |
48
|
2 |
|
new LocalFilesystemAdapter( |
49
|
2 |
|
$config['root'], |
50
|
1 |
|
$visibility, |
51
|
2 |
|
$config['lock'] ?? LOCK_EX, |
52
|
1 |
|
$links |
53
|
1 |
|
), |
54
|
2 |
|
$app->make('encrypted-data.encrypter') |
55
|
1 |
|
); |
56
|
|
|
|
57
|
2 |
|
$driver = new Filesystem( |
58
|
2 |
|
$adapter, |
59
|
2 |
|
Arr::only($config, [ |
60
|
2 |
|
'directory_visibility', |
61
|
1 |
|
'disable_asserts', |
62
|
1 |
|
'temporary_url', |
63
|
1 |
|
'url', |
64
|
1 |
|
'visibility', |
65
|
1 |
|
]) |
66
|
1 |
|
); |
67
|
|
|
|
68
|
2 |
|
return new FilesystemAdapter($driver, $adapter, $config); |
69
|
14 |
|
} |
70
|
7 |
|
); |
71
|
7 |
|
} |
72
|
|
|
} |
73
|
|
|
|