|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sausin\LaravelOvh; |
|
4
|
|
|
|
|
5
|
|
|
use BadMethodCallException; |
|
6
|
|
|
use OpenStack\ObjectStore\v1\Service; |
|
7
|
|
|
use OpenStack\Common\Error\BadResponseError; |
|
8
|
|
|
use OpenStack\ObjectStore\v1\Models\Container; |
|
9
|
|
|
use Nimbusoft\Flysystem\OpenStack\SwiftAdapter; |
|
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
|
|
|
/** |
|
22
|
|
|
* Constructor. |
|
23
|
|
|
* |
|
24
|
|
|
* @param Container $container |
|
25
|
|
|
* @param array $urlVars |
|
26
|
|
|
* @param string $prefix |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct(Container $container, $urlVars = [], $prefix = null) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->container = $container; |
|
31
|
|
|
|
|
32
|
|
|
$this->urlVars = $urlVars; |
|
33
|
|
|
$this->setPathPrefix($prefix); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Custom function to comply with the Storage::url() function in laravel |
|
38
|
|
|
* without checking the existence of a file (faster). |
|
39
|
|
|
* |
|
40
|
|
|
* @param string $path |
|
41
|
|
|
* @return string |
|
42
|
|
|
*/ |
|
43
|
|
|
public function getUrl($path) |
|
44
|
|
|
{ |
|
45
|
|
View Code Duplication |
if (! is_array($this->urlVars) || sizeof($this->urlVars) !== 4) { |
|
|
|
|
|
|
46
|
|
|
throw new BadMethodCallException('Insufficient Url Params', 1); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$urlBasePath = sprintf( |
|
50
|
|
|
'https://storage.%s.cloud.ovh.net/v1/AUTH_%s/%s/', |
|
51
|
|
|
$this->urlVars[0], |
|
52
|
|
|
$this->urlVars[1], |
|
53
|
|
|
$this->urlVars[2] |
|
54
|
|
|
); |
|
55
|
|
|
|
|
56
|
|
|
return $urlBasePath.$path; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Custom function to get a url with confirmed file existence. |
|
61
|
|
|
* |
|
62
|
|
|
* @param string $path |
|
63
|
|
|
* @return string |
|
64
|
|
|
*/ |
|
65
|
|
|
public function getUrlConfirm($path) |
|
66
|
|
|
{ |
|
67
|
|
|
// check if object exists |
|
68
|
|
|
try { |
|
69
|
|
|
$this->getTimestamp($path); |
|
70
|
|
|
} catch (BadResponseError $e) { |
|
71
|
|
|
throw $e; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
View Code Duplication |
if (! is_array($this->urlVars) || sizeof($this->urlVars) !== 4) { |
|
|
|
|
|
|
75
|
|
|
throw new BadMethodCallException('Insufficient Url Params', 1); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$urlBasePath = sprintf( |
|
79
|
|
|
'https://storage.%s.cloud.ovh.net/v1/AUTH_%s/%s/', |
|
80
|
|
|
$this->urlVars[0], |
|
81
|
|
|
$this->urlVars[1], |
|
82
|
|
|
$this->urlVars[2] |
|
83
|
|
|
); |
|
84
|
|
|
|
|
85
|
|
|
return $urlBasePath.$path; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Generate a temporary URL for private containers. |
|
90
|
|
|
* |
|
91
|
|
|
* @param string $path |
|
92
|
|
|
* @param int $expiry |
|
93
|
|
|
* @param string $method |
|
94
|
|
|
* @return string |
|
95
|
|
|
*/ |
|
96
|
|
|
public function getTemporaryUrl($path, $expiry = 60*60, $method = 'GET') |
|
97
|
|
|
{ |
|
98
|
|
View Code Duplication |
if (! is_array($this->urlVars) || sizeof($this->urlVars) !== 4) { |
|
|
|
|
|
|
99
|
|
|
throw new BadMethodCallException('Insufficient Url Params', 1); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$expiresAt = (int) (time() + $expiry); |
|
103
|
|
|
|
|
104
|
|
|
$codePath = sprintf( |
|
105
|
|
|
'/v1/AUTH_%s/%s/%s', |
|
106
|
|
|
$this->urlVars[1], |
|
107
|
|
|
$this->urlVars[2], |
|
108
|
|
|
$path |
|
109
|
|
|
); |
|
110
|
|
|
|
|
111
|
|
|
$body = sprintf("%s\n%s\n%s", $method, $expiresAt, $codePath); |
|
112
|
|
|
|
|
113
|
|
|
$signature = hash_hmac('sha1', $body, $this->urlVars[3]); |
|
114
|
|
|
|
|
115
|
|
|
return sprintf( |
|
116
|
|
|
'%s%s?temp_url_sig=%s&temp_url_expires=%s', |
|
117
|
|
|
sprintf('https://storage.%s.cloud.ovh.net', $this->urlVars[0]), |
|
118
|
|
|
$codePath, |
|
119
|
|
|
$signature, |
|
120
|
|
|
$expiresAt |
|
121
|
|
|
); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.