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