2024-02-05 21:17:41 +00:00
|
|
|
<?php
|
|
|
|
|
2024-02-25 09:11:15 +00:00
|
|
|
namespace App\Console\Commands\Storage;
|
2024-02-05 21:17:41 +00:00
|
|
|
|
|
|
|
use App\Facades\License;
|
2024-02-23 18:36:02 +00:00
|
|
|
use App\Services\SongStorages\DropboxStorage;
|
2024-02-05 21:17:41 +00:00
|
|
|
use Illuminate\Console\Command;
|
2024-02-25 09:11:15 +00:00
|
|
|
use Illuminate\Support\Facades\Artisan;
|
2024-02-05 21:17:41 +00:00
|
|
|
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-05 21:17:41 +00:00
|
|
|
{
|
2024-02-25 09:11:15 +00:00
|
|
|
protected $signature = 'koel:storage:dropbox';
|
2024-02-05 21:17:41 +00:00
|
|
|
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)
|
2024-02-05 21:17:41 +00:00
|
|
|
{
|
|
|
|
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.');
|
2024-02-05 21:17:41 +00:00
|
|
|
|
|
|
|
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-05 21:17:41 +00:00
|
|
|
|
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');
|
2024-02-05 21:17:41 +00:00
|
|
|
|
|
|
|
$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)
|
|
|
|
);
|
2024-02-05 21:17:41 +00:00
|
|
|
|
|
|
|
$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.');
|
2024-02-05 21:17:41 +00:00
|
|
|
$accessCode = $this->ask('Enter the access code');
|
|
|
|
|
|
|
|
$response = Http::asForm()
|
|
|
|
->withBasicAuth($appKey, $appSecret)
|
|
|
|
->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-05 21:17:41 +00:00
|
|
|
|
2024-02-25 09:11:15 +00:00
|
|
|
$this->dotenvEditor->setKeys($config);
|
2024-02-05 21:17:41 +00:00
|
|
|
$this->dotenvEditor->save();
|
2024-02-25 09:11:15 +00:00
|
|
|
Artisan::call('config:clear', ['--quiet' => true]);
|
2024-02-05 21:17:41 +00:00
|
|
|
|
2024-02-25 09:11:15 +00:00
|
|
|
$this->comment('Uploading a test file to make sure everything is working...');
|
2024-02-05 21:17:41 +00:00
|
|
|
|
|
|
|
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() . '.');
|
2024-02-05 21:17:41 +00:00
|
|
|
$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]);
|
|
|
|
|
2024-02-05 21:17:41 +00:00
|
|
|
return self::FAILURE;
|
|
|
|
}
|
|
|
|
|
2024-02-25 09:11:15 +00:00
|
|
|
$this->components->info('All done!');
|
2024-02-05 21:17:41 +00:00
|
|
|
|
|
|
|
Cache::forget($cacheKey);
|
|
|
|
|
|
|
|
return self::SUCCESS;
|
|
|
|
}
|
|
|
|
}
|