1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sausin\LaravelOvh\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Illuminate\Support\Facades\Storage; |
7
|
|
|
|
8
|
|
|
class SetTempUrlKey extends Command |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The name and signature of the console command. |
12
|
|
|
* |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $signature = 'ovh:set-temp-url-key |
16
|
|
|
{--key= : The key you want to setup on your container} |
17
|
|
|
{--force : Forcibly set a new key on the container}'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The console command description. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $description = 'Set temp url key on private container, making the use of Storage::temporaryUrl() possible'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Create a new command instance. |
28
|
|
|
* |
29
|
|
|
* @return void |
|
|
|
|
30
|
|
|
*/ |
31
|
|
|
public function __construct() |
32
|
|
|
{ |
33
|
|
|
parent::__construct(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Execute the console command. |
38
|
|
|
* |
39
|
|
|
* @return mixed |
40
|
|
|
*/ |
41
|
|
|
public function handle() |
42
|
|
|
{ |
43
|
|
|
if ($this->option('force')) { |
44
|
|
|
$this->setContainerKey(); |
45
|
|
|
|
46
|
|
|
return 0; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if ($this->checkContainerHasKey()) { |
50
|
|
|
$this->info('Container already has a key'); |
51
|
|
|
|
52
|
|
|
return 1; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$this->setContainerKey(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
protected function setContainerKey() |
59
|
|
|
{ |
60
|
|
|
$key = $this->option('key') ?? $this->getRandomKey(); |
61
|
|
|
|
62
|
|
|
try { |
63
|
|
|
Storage::disk('ovh') |
|
|
|
|
64
|
|
|
->getAdapter() |
65
|
|
|
->getContainer() |
66
|
|
|
->resetMetadata(['Temp-Url-Key' => $key]); |
67
|
|
|
|
68
|
|
|
$this->info('Success! The key has been set as: ' . $key); |
69
|
|
|
return 0; |
70
|
|
|
} catch (\Exception $e) { |
71
|
|
|
$this->info($e->getMessage()); |
72
|
|
|
|
73
|
|
|
return 1; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
protected function getRandomKey() |
78
|
|
|
{ |
79
|
|
|
return hash('sha512', time()); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
protected function checkContainerHasKey() |
83
|
|
|
{ |
84
|
|
|
$data = Storage::disk('ovh')->getAdapter()->getContainer()->getMetaData(); |
|
|
|
|
85
|
|
|
|
86
|
|
|
return array_key_exists('Temp-Url-Key', $data); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
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.