1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sausin\LaravelOvh; |
4
|
|
|
|
5
|
|
|
use BadMethodCallException; |
6
|
|
|
use Carbon\Carbon; |
7
|
|
|
use League\Flysystem\Config; |
8
|
|
|
use Nimbusoft\Flysystem\OpenStack\SwiftAdapter; |
9
|
|
|
use OpenStack\Common\Error\BadResponseError; |
10
|
|
|
use OpenStack\ObjectStore\v1\Models\Container; |
11
|
|
|
|
12
|
|
|
class OVHSwiftAdapter extends SwiftAdapter |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* URL base path variables for OVH service |
16
|
|
|
* the HTTPS url is typically of the format |
17
|
|
|
* https://storage.[REGION].cloud.ovh.net/v1/AUTH_[PROJECT_ID]/[CONTAINER_NAME]. |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
protected $urlVars; |
21
|
|
|
|
22
|
|
|
/** Variables from the Filesystem class will be temporarily stored here */ |
23
|
|
|
protected $specialParams; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Constructor. |
27
|
|
|
* |
28
|
|
|
* @param Container $container |
29
|
|
|
* @param array $urlVars |
30
|
|
|
* @param string $prefix |
31
|
|
|
*/ |
32
|
|
|
public function __construct(Container $container, $urlVars = [], $prefix = null) |
33
|
|
|
{ |
34
|
|
|
parent::__construct($container, $prefix); |
35
|
|
|
|
36
|
|
|
$this->urlVars = $urlVars; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Custom function to comply with the Storage::url() function in laravel |
41
|
|
|
* without checking the existence of a file (faster). |
42
|
|
|
* |
43
|
|
|
* @param string $path |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
|
|
public function getUrl($path) |
47
|
|
|
{ |
48
|
|
|
$this->checkParams(); |
49
|
|
|
|
50
|
|
|
return $this->getEndpoint().$path; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Custom function to get a url with confirmed file existence. |
55
|
|
|
* |
56
|
|
|
* @param string $path |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
|
|
public function getUrlConfirm($path) |
60
|
|
|
{ |
61
|
|
|
// check if object exists |
62
|
|
|
try { |
63
|
|
|
$this->getTimestamp($path); |
64
|
|
|
} catch (BadResponseError $e) { |
65
|
|
|
throw $e; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$this->checkParams(); |
69
|
|
|
|
70
|
|
|
return $this->getEndpoint().$path; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Generate a temporary URL for private containers. |
75
|
|
|
* |
76
|
|
|
* @param string $path |
77
|
|
|
* @param Carbon $expiration |
78
|
|
|
* @param array $options |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
|
|
public function getTemporaryUrl($path, $expiration, $options = []) |
82
|
|
|
{ |
83
|
|
|
$this->checkParams(); |
84
|
|
|
|
85
|
|
|
// expiry is relative to current time |
86
|
|
|
$expiresAt = $expiration instanceof Carbon ? $expiration->timestamp : (int) (time() + 60 * 60); |
87
|
|
|
|
88
|
|
|
// get the method |
89
|
|
|
$method = isset($options['method']) ? $options['method'] : 'GET'; |
90
|
|
|
|
91
|
|
|
// the url on the OVH host |
92
|
|
|
$codePath = sprintf( |
93
|
|
|
'/v1/AUTH_%s/%s/%s', |
94
|
|
|
$this->urlVars['projectId'], |
95
|
|
|
$this->urlVars['container'], |
96
|
|
|
$path |
97
|
|
|
); |
98
|
|
|
|
99
|
|
|
// body for the HMAC hash |
100
|
|
|
$body = sprintf("%s\n%s\n%s", $method, $expiresAt, $codePath); |
101
|
|
|
|
102
|
|
|
// the actual hash signature |
103
|
|
|
$signature = hash_hmac('sha1', $body, $this->urlVars['urlKey']); |
104
|
|
|
|
105
|
|
|
// return the url |
106
|
|
|
return sprintf( |
107
|
|
|
'%s?temp_url_sig=%s&temp_url_expires=%s', |
108
|
|
|
$this->getEndpoint().$path, |
109
|
|
|
$signature, |
110
|
|
|
$expiresAt |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Gets the endpoint url of the bucket. |
116
|
|
|
* |
117
|
|
|
* @return string |
118
|
|
|
*/ |
119
|
|
|
protected function getEndpoint() |
120
|
|
|
{ |
121
|
|
|
$this->checkParams(); |
122
|
|
|
|
123
|
|
|
return isset($this->urlVars['endpoint']) |
124
|
|
|
// allows assigning custom endpoint url |
125
|
|
|
? rtrim($this->urlVars['endpoint'], '/').'/' |
126
|
|
|
// if no custom endpoint assigned, use traditional swift v1 endpoint |
127
|
|
|
: sprintf( |
128
|
|
|
'https://storage.%s.cloud.ovh.net/v1/AUTH_%s/%s/', |
129
|
|
|
$this->urlVars['region'], |
130
|
|
|
$this->urlVars['projectId'], |
131
|
|
|
$this->urlVars['container'] |
132
|
|
|
); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Check if the url support variables have |
137
|
|
|
* been correctly defined. |
138
|
|
|
* |
139
|
|
|
* @return void|BadMethodCallException |
140
|
|
|
*/ |
141
|
|
|
protected function checkParams() |
142
|
|
|
{ |
143
|
|
|
$needKeys = ['region', 'projectId', 'container', 'urlKey', 'endpoint']; |
144
|
|
|
|
145
|
|
|
if (! is_array($this->urlVars) || count(array_intersect($needKeys, array_keys($this->urlVars))) !== count($needKeys)) { |
146
|
|
|
throw new BadMethodCallException('Insufficient Url Params', 1); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* {@inheritdoc} |
152
|
|
|
*/ |
153
|
|
|
public function write($path, $contents, Config $config, $size = 0) |
154
|
|
|
{ |
155
|
|
|
$this->specialParams = $config; |
156
|
|
|
|
157
|
|
|
parent::write($path, $contents, $config, $size); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Include support for object deletion. |
162
|
|
|
* |
163
|
|
|
* @param string $path |
164
|
|
|
* @see Nimbusoft\Flysystem\OpenStack |
165
|
|
|
* |
166
|
|
|
* @return array |
167
|
|
|
*/ |
168
|
|
|
protected function getWriteData($path) |
169
|
|
|
{ |
170
|
|
|
$data = ['name' => $path]; |
171
|
|
|
|
172
|
|
|
if ($this->specialParams->has('deleteAfter')) { |
173
|
|
|
$data += ['deleteAfter' => $this->specialParams->get('deleteAfter')]; |
174
|
|
|
} elseif ($this->specialParams->has('deleteAt')) { |
175
|
|
|
$data += ['deleteAt' => $this->specialParams->get('deleteAt')]; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return $data; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|