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
|
|
|
{--key= : The key you want to set up on your container} |
19
|
|
|
{--force : Forcibly set a new key on the container}'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The console command description. |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $description = 'Set temp url key on the private container, making the use of Storage::temporaryUrl() possible'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The Object Storage Container. |
30
|
|
|
* |
31
|
|
|
* @var Container |
32
|
|
|
*/ |
33
|
|
|
protected $container; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Create a new command instance. |
37
|
|
|
* |
38
|
|
|
* @return void |
|
|
|
|
39
|
|
|
*/ |
40
|
|
|
public function __construct() |
41
|
|
|
{ |
42
|
|
|
parent::__construct(); |
43
|
|
|
|
44
|
|
|
$this->container = Storage::disk('ovh')->getAdapter()->getContainer(); |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Execute the console command. |
49
|
|
|
* |
50
|
|
|
* If the '--force' flag is provided, a new Temp URL Key will be generated and |
51
|
|
|
* forcefully set in the Container's metadata, overriding any existing keys. |
52
|
|
|
* |
53
|
|
|
* If the command is not forced and there's an existing key, the User will be |
54
|
|
|
* prompted to override the existing keys. |
55
|
|
|
* |
56
|
|
|
* @return void |
57
|
|
|
*/ |
58
|
|
|
public function handle(): void |
59
|
|
|
{ |
60
|
|
|
if ($this->hasOption('force') || $this->askIfShouldOverrideExistingKey()) { |
61
|
|
|
$this->setContainerKey(); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* If there's no existing Temp URL Key present in the Container, continue. |
67
|
|
|
* |
68
|
|
|
* Otherwise, if there's already an existing Temp URL Key present in the |
69
|
|
|
* Container, the User will be prompted to choose if we should override it |
70
|
|
|
* or not. |
71
|
|
|
* |
72
|
|
|
* @return bool |
73
|
|
|
*/ |
74
|
|
|
protected function askIfShouldOverrideExistingKey(): bool |
75
|
|
|
{ |
76
|
|
|
if (!array_key_exists('Temp-Url-Key', $this->container->getMetadata())) { |
77
|
|
|
return true; // Yeah, override the non-existing key. |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $this->confirm( |
81
|
|
|
'A Temp URL Key already exists in your container, would you like to override it?', |
82
|
|
|
false |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Generates a random Temp URL Key. |
88
|
|
|
* |
89
|
|
|
* For more details, please refer to: |
90
|
|
|
* - https://docs.ovh.com/gb/en/public-cloud/share_an_object_via_a_temporary_url/#generate-the-temporary-address-tempurl |
91
|
|
|
* |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
|
|
protected function getRandomKey(): string |
95
|
|
|
{ |
96
|
|
|
return hash('sha512', time()); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Updates the Temp URL Key for the Container. |
101
|
|
|
* |
102
|
|
|
* @return void |
103
|
|
|
*/ |
104
|
|
|
protected function setContainerKey(): void |
105
|
|
|
{ |
106
|
|
|
$key = $this->option('key') ?? $this->getRandomKey(); |
107
|
|
|
|
108
|
|
|
try { |
109
|
|
|
$this->container->resetMetadata(['Temp-Url-Key' => $key]); |
110
|
|
|
} catch (Exception $e) { |
111
|
|
|
$this->error($e->getMessage()); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$this->info('Successfully set Temp URL Key to: '.$key); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.