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 OpenStack\ObjectStore\v1\Models\Container; |
9
|
|
|
|
10
|
|
|
class SetTempUrlKey extends Command |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* The name and signature of the console command. |
14
|
|
|
* |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $signature = 'ovh:set-temp-url-key |
18
|
|
|
{--disk=ovh : The disk using your OVH container} |
19
|
|
|
{--key= : The key you want to set up on your container} |
20
|
|
|
{--force : Forcibly set a new key on the container}'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The console command description. |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $description = 'Set temp url key on the private container, making the use of Storage::temporaryUrl() possible'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Execute the console command. |
31
|
|
|
* |
32
|
|
|
* If the '--force' flag is provided, a new Temp URL Key will be generated and |
33
|
|
|
* forcefully set in the Container's metadata, overriding any existing keys. |
34
|
|
|
* |
35
|
|
|
* If the command is not forced and there's an existing key, the User will be |
36
|
|
|
* prompted to override the existing keys. |
37
|
|
|
* |
38
|
|
|
* @return void |
39
|
|
|
*/ |
40
|
|
|
public function handle(): void |
41
|
|
|
{ |
42
|
|
|
$container = Storage::disk($this->option('disk'))->getAdapter()->getContainer(); |
|
|
|
|
43
|
|
|
|
44
|
|
|
if ($this->hasOption('force') || $this->askIfShouldOverrideExistingKey($container)) { |
45
|
|
|
$this->setContainerKey($container); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* If there's no existing Temp URL Key present in the Container, continue. |
51
|
|
|
* |
52
|
|
|
* Otherwise, if there's already an existing Temp URL Key present in the |
53
|
|
|
* Container, the User will be prompted to choose if we should override it |
54
|
|
|
* or not. |
55
|
|
|
* |
56
|
|
|
* @param Container $container |
57
|
|
|
* @return bool |
58
|
|
|
*/ |
59
|
|
|
protected function askIfShouldOverrideExistingKey(Container $container): bool |
60
|
|
|
{ |
61
|
|
|
if (!array_key_exists('Temp-Url-Key', $container->getMetadata())) { |
62
|
|
|
return true; // Yeah, override the non-existing key. |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $this->confirm( |
66
|
|
|
'A Temp URL Key already exists in your container, would you like to override it?', |
67
|
|
|
false |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Generates a random Temp URL Key. |
73
|
|
|
* |
74
|
|
|
* For more details, please refer to: |
75
|
|
|
* - https://docs.ovh.com/gb/en/public-cloud/share_an_object_via_a_temporary_url/#generate-the-temporary-address-tempurl |
76
|
|
|
* |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
protected function getRandomKey(): string |
80
|
|
|
{ |
81
|
|
|
return hash('sha512', time()); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Updates the Temp URL Key for the Container. |
86
|
|
|
* |
87
|
|
|
* @param Container $container |
88
|
|
|
* @return void |
89
|
|
|
*/ |
90
|
|
|
protected function setContainerKey(Container $container): void |
91
|
|
|
{ |
92
|
|
|
$key = $this->option('key') ?? $this->getRandomKey(); |
93
|
|
|
|
94
|
|
|
try { |
95
|
|
|
$container->resetMetadata(['Temp-Url-Key' => $key]); |
96
|
|
|
} catch (Exception $e) { |
97
|
|
|
$this->error($e->getMessage()); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$this->info('Successfully set Temp URL Key to: '.$key); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.