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