|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sausin\LaravelOvh; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Psr7\Stream; |
|
6
|
|
|
use League\Flysystem\Config; |
|
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 $urlBasePathVars; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Constructor. |
|
23
|
|
|
* |
|
24
|
|
|
* @param Container $container |
|
25
|
|
|
* @param string $prefix |
|
26
|
|
|
*/ |
|
27
|
|
|
public function __construct(Container $container, $urlBasePathVars = [], $prefix = null) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->setPathPrefix($prefix); |
|
30
|
|
|
$this->container = $container; |
|
31
|
|
|
|
|
32
|
|
|
$this->urlBasePathVars = $urlBasePathVars; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Custom function to comply with the Storage::url() function in laravel |
|
37
|
|
|
* without checking the existence of a file (faster). |
|
38
|
|
|
* |
|
39
|
|
|
* @param string $path |
|
40
|
|
|
* @return string |
|
41
|
|
|
*/ |
|
42
|
|
|
public function getUrl($path) |
|
43
|
|
|
{ |
|
44
|
|
|
if (! $this->urlBasePathVars) { |
|
|
|
|
|
|
45
|
|
|
throw new \Exception('Empty array', 1); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$urlBasePath = sprintf( |
|
49
|
|
|
'https://storage.%s.cloud.ovh.net/v1/AUTH_%s/%s/', |
|
50
|
|
|
$this->urlBasePathVars[0], |
|
51
|
|
|
$this->urlBasePathVars[1], |
|
52
|
|
|
$this->urlBasePathVars[2] |
|
53
|
|
|
); |
|
54
|
|
|
|
|
55
|
|
|
return $urlBasePath.$path; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Custom function to get a url with confirmed file existence. |
|
60
|
|
|
* |
|
61
|
|
|
* @param string $path |
|
62
|
|
|
* @return string |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getUrlConfirm($path) |
|
65
|
|
|
{ |
|
66
|
|
|
// check if object exists |
|
67
|
|
|
try { |
|
68
|
|
|
$this->getTimestamp($path); |
|
69
|
|
|
} catch (BadResponseError $e) { |
|
70
|
|
|
throw $e; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
if (! $this->urlBasePathVars) { |
|
|
|
|
|
|
74
|
|
|
throw new \Exception('Empty array', 1); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$urlBasePath = sprintf( |
|
78
|
|
|
'https://storage.%s.cloud.ovh.net/v1/AUTH_%s/%s/', |
|
79
|
|
|
$this->urlBasePathVars[0], |
|
80
|
|
|
$this->urlBasePathVars[1], |
|
81
|
|
|
$this->urlBasePathVars[2] |
|
82
|
|
|
); |
|
83
|
|
|
|
|
84
|
|
|
return $urlBasePath.$path; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.