1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sausin\LaravelOvh\Commands; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Illuminate\Console\Command; |
7
|
|
|
use Illuminate\Support\Facades\Config; |
8
|
|
|
use Illuminate\Support\Facades\Storage; |
9
|
|
|
use InvalidArgumentException; |
10
|
|
|
use League\Flysystem\Cached\CachedAdapter; |
11
|
|
|
use OpenStack\ObjectStore\v1\Models\Container; |
12
|
|
|
|
13
|
|
|
class SetTempUrlKey extends Command |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* The name and signature of the console command. |
17
|
|
|
* |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $signature = 'ovh:set-temp-url-key |
21
|
|
|
{--disk=ovh : The disk using your OVH container} |
22
|
|
|
{--key= : The key you want to set up on your container} |
23
|
|
|
{--force : Forcibly set a new key on the container}'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The console command description. |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $description = 'Set temp url key on the private container, making the use of Storage::temporaryUrl() possible'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The Object Storage Container. |
34
|
|
|
* |
35
|
|
|
* @var Container |
36
|
|
|
*/ |
37
|
|
|
protected $container; |
38
|
|
|
|
39
|
|
|
/** array */ |
40
|
|
|
protected $containerMeta = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Execute the console command. |
44
|
|
|
* |
45
|
|
|
* If the '--force' flag is provided, a new Temp URL Key will be generated and |
46
|
|
|
* forcefully set in the Container's metadata, overriding any existing keys. |
47
|
|
|
* |
48
|
|
|
* If the command is not forced and there's an existing key, the User will be |
49
|
|
|
* prompted to override the existing keys. |
50
|
|
|
* |
51
|
|
|
* @return void |
52
|
|
|
*/ |
53
|
|
|
public function handle(): void |
54
|
|
|
{ |
55
|
|
|
try { |
56
|
|
|
$disk = $this->getDisk(); |
57
|
|
|
|
58
|
|
|
$adapter = Storage::disk($disk)->getAdapter(); |
59
|
|
|
|
60
|
|
|
if ($adapter instanceof CachedAdapter) { |
61
|
|
|
$adapter = $adapter->getAdapter(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$this->container = $adapter->getContainer(); |
65
|
|
|
} catch (InvalidArgumentException $e) { |
66
|
|
|
$this->error($e->getMessage()); |
67
|
|
|
|
68
|
|
|
return; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$this->containerMeta = $this->container->getMetadata(); |
72
|
|
|
|
73
|
|
|
if ($this->option('force') || $this->askIfShouldOverrideExistingKey()) { |
74
|
|
|
$this->setContainerKey(); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* If there's no existing Temp URL Key present in the Container, continue. |
80
|
|
|
* |
81
|
|
|
* Otherwise, if there's already an existing Temp URL Key present in the |
82
|
|
|
* Container, the User will be prompted to choose if we should override it |
83
|
|
|
* or not. |
84
|
|
|
* |
85
|
|
|
* @return bool |
86
|
|
|
*/ |
87
|
|
|
protected function askIfShouldOverrideExistingKey(): bool |
88
|
|
|
{ |
89
|
|
|
if (!array_key_exists('Temp-Url-Key', $this->containerMeta)) { |
90
|
|
|
return true; // Yeah, override the non-existing key. |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $this->confirm( |
94
|
|
|
'A Temp URL Key already exists in your container, would you like to override it?', |
95
|
|
|
false |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Generates a random Temp URL Key. |
101
|
|
|
* |
102
|
|
|
* For more details, please refer to: |
103
|
|
|
* - https://docs.ovh.com/gb/en/public-cloud/share_an_object_via_a_temporary_url/#generate-the-temporary-address-tempurl |
104
|
|
|
* |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
|
|
protected function getRandomKey(): string |
108
|
|
|
{ |
109
|
|
|
return hash('sha512', time()); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Updates the Temp URL Key for the Container. |
114
|
|
|
* |
115
|
|
|
* @return void |
116
|
|
|
*/ |
117
|
|
|
protected function setContainerKey(): void |
118
|
|
|
{ |
119
|
|
|
$key = $this->option('key') ?? $this->getRandomKey(); |
120
|
|
|
$meta = $this->getMeta(); |
121
|
|
|
|
122
|
|
|
try { |
123
|
|
|
$this->container->resetMetadata($meta + ['Temp-Url-Key' => $key]); |
124
|
|
|
|
125
|
|
|
$this->info('Successfully set Temp URL Key to: '.$key); |
126
|
|
|
} catch (Exception $e) { |
127
|
|
|
$this->error($e->getMessage()); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* If other meta keys exist, get them. |
133
|
|
|
* |
134
|
|
|
* @return array |
135
|
|
|
*/ |
136
|
|
|
protected function getMeta(): array |
137
|
|
|
{ |
138
|
|
|
$meta = []; |
139
|
|
|
$metaKeys = ['Access-Control-Allow-Origin', 'Access-Control-Max-Age']; |
140
|
|
|
|
141
|
|
|
foreach ($metaKeys as $key) { |
142
|
|
|
if (array_key_exists($key, $this->containerMeta)) { |
143
|
|
|
$meta += [$key => $this->containerMeta[$key]]; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return $meta; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Check if selected disk is correct. If not, provide options to user. |
152
|
|
|
* |
153
|
|
|
* @return string |
154
|
|
|
*/ |
155
|
|
|
public function getDisk(): string |
156
|
|
|
{ |
157
|
|
|
$available = array_keys(array_filter(Config::get('filesystems.disks'), function ($d) { |
158
|
|
|
return $d['driver'] === 'ovh'; |
159
|
|
|
})); |
160
|
|
|
|
161
|
|
|
$selected = $this->option('disk'); |
162
|
|
|
|
163
|
|
|
if (in_array($selected, $available)) { |
164
|
|
|
return $selected; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
return $this->choice( |
168
|
|
|
'Selected disk not correct. Please choose from below options:', |
169
|
|
|
$available, |
170
|
|
|
); |
|
|
|
|
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|