koel/app/Console/Commands/Storage/SetupDropboxStorageCommand.php

101 lines
3.4 KiB
PHP
Raw Normal View History

<?php
2024-02-25 09:11:15 +00:00
namespace App\Console\Commands\Storage;
use App\Facades\License;
use App\Services\SongStorages\DropboxStorage;
use Illuminate\Console\Command;
2024-02-25 09:11:15 +00:00
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;
use Jackiedo\DotenvEditor\DotenvEditor;
use Throwable;
2024-02-25 09:11:15 +00:00
class SetupDropboxStorageCommand extends Command
{
2024-02-25 09:11:15 +00:00
protected $signature = 'koel:storage:dropbox';
protected $description = 'Set up Dropbox as the storage driver for Koel';
2024-02-25 09:11:15 +00:00
public function __construct(private DotenvEditor $dotenvEditor)
{
parent::__construct();
}
public function handle(): int
{
if (!License::isPlus()) {
2024-02-25 09:11:15 +00:00
$this->components->error('Dropbox as a storage driver is only available in Koel Plus.');
return self::FAILURE;
}
2024-02-25 09:11:15 +00:00
$this->components->info('Setting up Dropbox as the storage driver for Koel.');
$this->components->warn('Changing the storage configuration can cause irreversible data loss.');
$this->components->warn('Consider backing up your data before proceeding.');
2024-02-25 09:11:15 +00:00
$config = ['STORAGE_DRIVER' => 'dropbox'];
$config['DROPBOX_APP_KEY'] = $this->ask('Enter your Dropbox app key');
$config['DROPBOX_APP_SECRET'] = $this->ask('Enter your Dropbox app secret');
$cacheKey = Str::uuid()->toString();
2024-02-25 09:11:15 +00:00
Cache::put(
$cacheKey,
['app_key' => $config['DROPBOX_APP_KEY'], 'app_secret' => $config['DROPBOX_APP_SECRET']],
now()->addMinutes(15)
);
$tmpUrl = route('dropbox.authorize', ['state' => $cacheKey]);
$this->comment('Please visit the following link to authorize Koel to access your Dropbox account:');
$this->info($tmpUrl);
$this->comment('The link will expire in 15 minutes.');
2024-02-25 09:11:15 +00:00
$this->comment('After you have authorized Koel, enter the access code below.');
$accessCode = $this->ask('Enter the access code');
$response = Http::asForm()
2024-03-28 09:18:14 +00:00
->withBasicAuth($config['DROPBOX_APP_KEY'], $config['DROPBOX_APP_SECRET'])
->post('https://api.dropboxapi.com/oauth2/token', [
'code' => $accessCode,
'grant_type' => 'authorization_code',
]);
if ($response->failed()) {
$this->error(
'Failed to authorize with Dropbox. The server said: ' . $response->json('error_description') . '.'
);
return self::FAILURE;
}
2024-02-25 09:11:15 +00:00
$config['DROPBOX_REFRESH_TOKEN'] = $response->json('refresh_token');
2024-02-25 09:11:15 +00:00
$this->dotenvEditor->setKeys($config);
$this->dotenvEditor->save();
2024-02-25 09:11:15 +00:00
Artisan::call('config:clear', ['--quiet' => true]);
2024-02-25 09:11:15 +00:00
$this->comment('Uploading a test file to make sure everything is working...');
try {
/** @var DropboxStorage $storage */
$storage = app(DropboxStorage::class);
$storage->testSetup();
} catch (Throwable $e) {
2024-02-25 09:11:15 +00:00
$this->error('Failed to upload test file: ' . $e->getMessage() . '.');
$this->comment('Please make sure the app has the correct permissions and try again.');
2024-02-25 09:11:15 +00:00
$this->dotenvEditor->restore();
Artisan::call('config:clear', ['--quiet' => true]);
return self::FAILURE;
}
2024-02-25 09:11:15 +00:00
$this->components->info('All done!');
Cache::forget($cacheKey);
return self::SUCCESS;
}
}