1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sausin\LaravelOvh; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use League\Flysystem\Config; |
7
|
|
|
use Nimbusoft\Flysystem\OpenStack\SwiftAdapter; |
8
|
|
|
use OpenStack\Common\Error\BadResponseError; |
9
|
|
|
use OpenStack\ObjectStore\v1\Models\Container; |
10
|
|
|
|
11
|
|
|
class OVHSwiftAdapter extends SwiftAdapter |
12
|
|
|
{ |
13
|
|
|
/** @var OVHConfiguration */ |
14
|
|
|
protected OVHConfiguration $config; |
|
|
|
|
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* OVHSwiftAdapter constructor. |
18
|
|
|
* |
19
|
|
|
* @param Container $container |
20
|
|
|
* @param OVHConfiguration $config |
21
|
|
|
* @param string|null $prefix |
22
|
|
|
*/ |
23
|
|
|
public function __construct(Container $container, OVHConfiguration $config, ?string $prefix = null) |
24
|
|
|
{ |
25
|
|
|
parent::__construct($container, $prefix); |
26
|
|
|
|
27
|
|
|
$this->config = $config; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Gets the endpoint url of the bucket. |
32
|
|
|
* |
33
|
|
|
* @param string|null $path |
34
|
|
|
* @return string |
35
|
|
|
*/ |
36
|
|
|
protected function getEndpoint(?string $path = null): string |
37
|
|
|
{ |
38
|
|
|
$url = !empty($this->config->endpoint) |
39
|
|
|
// Allows assigning custom endpoint url |
40
|
|
|
? rtrim($this->config->endpoint, '/').'/' |
41
|
|
|
// If no custom endpoint assigned, use traditional swift v1 endpoint |
42
|
|
|
: sprintf( |
43
|
|
|
'https://storage.%s.cloud.ovh.net/v1/AUTH_%s/%s/', |
44
|
|
|
$this->config->region, |
45
|
|
|
$this->config->projectId, |
46
|
|
|
$this->config->container |
47
|
|
|
); |
48
|
|
|
|
49
|
|
|
if (!empty($path)) { |
50
|
|
|
$url .= ltrim($path, '/'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $url; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Custom function to comply with the Storage::url() function in laravel |
58
|
|
|
* without checking the existence of a file (faster). |
59
|
|
|
* |
60
|
|
|
* @param string $path |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
|
|
public function getUrl($path) |
64
|
|
|
{ |
65
|
|
|
return $this->getEndpoint($path); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Custom function to get an url with confirmed file existence. |
70
|
|
|
* |
71
|
|
|
* @param string $path |
72
|
|
|
* @return string |
73
|
|
|
* @throws BadResponseError |
74
|
|
|
*/ |
75
|
|
|
public function getUrlConfirm($path): string |
76
|
|
|
{ |
77
|
|
|
// check if object exists |
78
|
|
|
try { |
79
|
|
|
$this->has($path); |
80
|
|
|
} catch (BadResponseError $e) { |
81
|
|
|
throw $e; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $this->getEndpoint($path); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Generate a temporary URL for private containers. |
89
|
|
|
* |
90
|
|
|
* @param string $path |
91
|
|
|
* @param Carbon|null $expiresAt |
92
|
|
|
* @param array $options |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
public function getTemporaryUrl(string $path, ?Carbon $expiresAt = null, array $options = []): string |
96
|
|
|
{ |
97
|
|
|
// Ensure $path doesn't begin with a slash |
98
|
|
|
$path = ltrim($path, '/'); |
99
|
|
|
|
100
|
|
|
// Expiry is relative to current time |
101
|
|
|
if (empty($expiresAt)) { |
102
|
|
|
$expiresAt = Carbon::now()->addHour(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
// Get the method |
106
|
|
|
$method = $options['method'] ?? 'GET'; |
107
|
|
|
|
108
|
|
|
// The url on the OVH host |
109
|
|
|
$codePath = sprintf( |
110
|
|
|
'/v1/AUTH_%s/%s/%s', |
111
|
|
|
$this->config->projectId, |
112
|
|
|
$this->config->container, |
113
|
|
|
$path |
114
|
|
|
); |
115
|
|
|
|
116
|
|
|
// Body for the HMAC hash |
117
|
|
|
$body = sprintf("%s\n%s\n%s", $method, $expiresAt->timestamp, $codePath); |
118
|
|
|
|
119
|
|
|
// The actual hash signature |
120
|
|
|
$signature = hash_hmac('sha1', $body, $this->config->tempUrlKey); |
121
|
|
|
|
122
|
|
|
// Return signed url |
123
|
|
|
return sprintf( |
124
|
|
|
'%s?temp_url_sig=%s&temp_url_expires=%s', |
125
|
|
|
$this->getEndpoint($path), |
126
|
|
|
$signature, |
127
|
|
|
$expiresAt |
128
|
|
|
); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Expose the container to allow for modification to metadata. |
133
|
|
|
* |
134
|
|
|
* @return Container |
135
|
|
|
*/ |
136
|
|
|
public function getContainer(): Container |
137
|
|
|
{ |
138
|
|
|
return $this->container; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Include support for object deletion. |
143
|
|
|
* |
144
|
|
|
* @param string $path |
145
|
|
|
* @param Config $config |
146
|
|
|
* @return array |
147
|
|
|
* @see SwiftAdapter |
148
|
|
|
*/ |
149
|
|
|
protected function getWriteData($path, $config): array |
150
|
|
|
{ |
151
|
|
|
$data = ['name' => $path]; |
152
|
|
|
|
153
|
|
|
if ($config->has('deleteAfter')) { |
154
|
|
|
$data['deleteAfter'] = $config->get('deleteAfter'); |
155
|
|
|
} elseif ($config->has('deleteAt')) { |
156
|
|
|
$data['deleteAt'] = $config->get('deleteAt'); |
157
|
|
|
} elseif (!empty($this->config->deleteAfter)) { |
158
|
|
|
$data['deleteAfter'] = $this->config->deleteAfter; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return $data; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|