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