Completed
Push — master ( 9b630e...42b120 )
by Saurabh
01:09
created

OVHSwiftAdapter::getUrlConfirm()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
rs 9.4285
cc 2
eloc 12
nc 2
nop 1
1
<?php
2
3
namespace Sausin\LaravelOvh;
4
5
use Carbon\Carbon;
6
use BadMethodCallException;
7
use OpenStack\ObjectStore\v1\Service;
8
use OpenStack\Common\Error\BadResponseError;
9
use OpenStack\ObjectStore\v1\Models\Container;
10
use Nimbusoft\Flysystem\OpenStack\SwiftAdapter;
11
12
class OVHSwiftAdapter extends SwiftAdapter
13
{
14
    /**
15
     * URL base path variables for OVH service
16
     * the HTTPS url is typically of the format
17
     * https://storage.[REGION].cloud.ovh.net/v1/AUTH_[PROJECT_ID]/[CONTAINER_NAME].
18
     * @var array
19
     */
20
    protected $urlVars;
21
22
    /**
23
     * Constructor.
24
     *
25
     * @param Container $container
26
     * @param array     $urlVars
27
     * @param string    $prefix
28
     */
29
    public function __construct(Container $container, $urlVars = [], $prefix = null)
30
    {
31
        $this->container = $container;
32
33
        $this->urlVars = $urlVars;
34
        $this->setPathPrefix($prefix);
35
    }
36
37
    /**
38
     * Custom function to comply with the Storage::url() function in laravel
39
     * without checking the existence of a file (faster).
40
     *
41
     * @param  string $path
42
     * @return string
43
     */
44
    public function getUrl($path)
45
    {
46
        $this->checkParams();
47
48
        $urlBasePath = sprintf(
49
            'https://storage.%s.cloud.ovh.net/v1/AUTH_%s/%s/',
50
            $this->urlVars[0],
51
            $this->urlVars[1],
52
            $this->urlVars[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
        $this->checkParams();
74
75
        $urlBasePath = sprintf(
76
            'https://storage.%s.cloud.ovh.net/v1/AUTH_%s/%s/',
77
            $this->urlVars[0],
78
            $this->urlVars[1],
79
            $this->urlVars[2]
80
        );
81
82
        return $urlBasePath.$path;
83
    }
84
85
    /**
86
     * Generate a temporary URL for private containers.
87
     *
88
     * @param  string   $path
89
     * @param  Carbon   $expiration
90
     * @param  array    $options
91
     * @return string
92
     */
93
    public function getTemporaryUrl($path, $expiration, $options = [])
94
    {
95
        $this->checkParams();
96
97
        // expiry is relative to current time
98
        $expiresAt = $expiration instanceof Carbon ? $expiration->timestamp : (int) (time() + 60 * 60);
99
100
        // get the method
101
        $method = isset($options['method']) ? $options['method'] : 'GET';
102
103
        // the url on the OVH host
104
        $codePath = sprintf(
105
            '/v1/AUTH_%s/%s/%s',
106
            $this->urlVars[1],
107
            $this->urlVars[2],
108
            $path
109
        );
110
111
        // body for the HMAC hash
112
        $body = sprintf("%s\n%s\n%s", $method, $expiresAt, $codePath);
113
114
        // the actual hash signature
115
        $signature = hash_hmac('sha1', $body, $this->urlVars[3]);
116
117
        // return the url
118
        return sprintf(
119
            '%s%s?temp_url_sig=%s&temp_url_expires=%s',
120
            sprintf('https://storage.%s.cloud.ovh.net', $this->urlVars[0]),
121
            $codePath,
122
            $signature,
123
            $expiresAt
124
        );
125
    }
126
127
    /**
128
     * Check if the url support variables have
129
     * been correctly defined.
130
     *
131
     * @return void|BadMethodCallException
132
     */
133
    protected function checkParams()
134
    {
135
        if (! is_array($this->urlVars) || count($this->urlVars) !== 4) {
136
            throw new BadMethodCallException('Insufficient Url Params', 1);
137
        }
138
    }
139
}
140