Files
laravel-manager-packagist/src/Console/StorageCheckCommand.php
T
2026-12-04 15:10:03 +00:00

67 lines
2.2 KiB
PHP

<?php
namespace IriksIT\Manager\Console;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;
use IriksIT\Manager\Exceptions\MediaStorageException;
use Throwable;
/**
* Tries the disk of the media library the way a real upload would: writes a small file, reads it back, checks that it is
* the same and deletes it again. Leaves nothing behind (when the delete fails, it says which file is left).
*
* php artisan manager:storage-check [--disk=iriksit]
*/
class StorageCheckCommand extends Command
{
protected $signature = 'manager:storage-check {--disk= : The disk to try; default: the disk of the media library}';
protected $description = 'Write, read and delete a test file on the disk of the media library';
public function handle(): int
{
$name = (string) ($this->option('disk') ?: config('manager.media.disk'));
$disk = Storage::disk($name);
$location = 'manager-check/'.bin2hex(random_bytes(6)).'.txt';
$content = 'manager storage check '.now()->toIso8601String();
$this->line("Disk: {$name}");
try {
$step = 'write';
if (! $disk->put($location, $content)) {
$this->components->error('The file could not be written. See the log of the app for the reason.');
return self::FAILURE;
}
$this->components->info('Written.');
$step = 'read';
$back = $disk->get($location);
if ($back !== $content) {
$this->components->error('What was read back is not what was written.');
return self::FAILURE;
}
$this->components->info('Read back: the same.');
$step = 'delete';
if (! $disk->delete($location)) {
$this->components->error("The test file could not be deleted: {$location}");
return self::FAILURE;
}
$this->components->info('Deleted.');
} catch (MediaStorageException|Throwable $e) {
$this->components->error("Could not {$step}: ".$e->getMessage());
return self::FAILURE;
}
$this->components->info('The storage works.');
return self::SUCCESS;
}
}