1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sausin\LaravelOvh; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Storage; |
6
|
|
|
use Illuminate\Support\ServiceProvider; |
7
|
|
|
use League\Flysystem\Filesystem; |
8
|
|
|
use OpenStack\ObjectStore\v1\Models\Container; |
9
|
|
|
use OpenStack\OpenStack; |
10
|
|
|
|
11
|
|
|
class OVHServiceProvider extends ServiceProvider |
12
|
|
|
{ |
13
|
|
|
/** @var OVHConfiguration */ |
14
|
|
|
private OVHConfiguration $config; |
|
|
|
|
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Bootstrap the application services. |
18
|
|
|
*/ |
19
|
|
|
public function boot(): void |
20
|
|
|
{ |
21
|
|
|
$this->configureCommands(); |
22
|
|
|
|
23
|
|
|
$this->configureStorage(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Configures available commands. |
28
|
|
|
*/ |
29
|
|
|
protected function configureCommands(): void |
30
|
|
|
{ |
31
|
|
|
if (!$this->app->runningInConsole()) { |
32
|
|
|
return; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$this->commands([ |
36
|
|
|
Commands\SetTempUrlKey::class, |
37
|
|
|
]); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Configures extended filesystem storage for interaction with OVH Object Storage. |
42
|
|
|
*/ |
43
|
|
|
protected function configureStorage(): void |
44
|
|
|
{ |
45
|
|
|
Storage::extend('ovh', function ($app, array $config) { |
46
|
|
|
// Creates a Configuration instance. |
47
|
|
|
$this->config = OVHConfiguration::make($config); |
48
|
|
|
|
49
|
|
|
$client = $this->makeOpenStackClient(); |
50
|
|
|
|
51
|
|
|
// Get the Object Storage container. |
52
|
|
|
$container = $client->objectStoreV1()->getContainer($this->config->container); |
53
|
|
|
|
54
|
|
|
return $this->makeFileSystem($container); |
55
|
|
|
}); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Creates an OpenStack client instance, needed for interaction with OVH OpenStack. |
60
|
|
|
* |
61
|
|
|
* @return OpenStack |
62
|
|
|
*/ |
63
|
|
|
protected function makeOpenStackClient(): OpenStack |
64
|
|
|
{ |
65
|
|
|
return new OpenStack([ |
66
|
|
|
'authUrl' => $this->config->authUrl, |
67
|
|
|
'region' => $this->config->region, |
68
|
|
|
'user' => [ |
69
|
|
|
'name' => $this->config->username, |
70
|
|
|
'password' => $this->config->password, |
71
|
|
|
'domain' => [ |
72
|
|
|
'name' => $this->config->userDomain, |
73
|
|
|
], |
74
|
|
|
], |
75
|
|
|
'scope' => [ |
76
|
|
|
'project' => [ |
77
|
|
|
'id' => $this->config->projectId, |
78
|
|
|
], |
79
|
|
|
], |
80
|
|
|
]); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Creates a Filesystem instance for interaction with the Object Storage. |
85
|
|
|
* |
86
|
|
|
* @param Container $container |
87
|
|
|
* @return Filesystem |
88
|
|
|
*/ |
89
|
|
|
protected function makeFileSystem(Container $container): Filesystem |
90
|
|
|
{ |
91
|
|
|
return new Filesystem( |
92
|
|
|
new OVHSwiftAdapter($container, $this->config), |
93
|
|
|
[ |
94
|
|
|
'swiftLargeObjectThreshold' => $this->config->swiftLargeObjectThreshold, |
95
|
|
|
'swiftSegmentSize' => $this->config->swiftSegmentSize, |
96
|
|
|
'swiftSegmentContainer' => $this->config->swiftSegmentContainer, |
97
|
|
|
] |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|