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