1 | <?php |
||
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 | } |
||
182 |