Files
getgreen-backend/app/Console/Commands/StorageHealthCheck.php
2026-04-28 16:21:25 +05:00

57 lines
2.1 KiB
PHP

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;
use Throwable;
class StorageHealthCheck extends Command
{
protected $signature = 'storage:health-check
{disk? : Disk name, defaults to filesystems.default}
{--keep : Keep the test file so the public URL can be checked in a browser}';
protected $description = 'Check current storage disk, write/read/delete a test file, and print its public URL.';
public function handle(): int
{
$disk = $this->argument('disk') ?: config('filesystems.default');
$path = 'healthcheck/' . now()->format('YmdHis') . '-' . uniqid() . '.txt';
$this->line('default=' . config('filesystems.default'));
$this->line('disk=' . $disk);
$this->line('endpoint=' . (config("filesystems.disks.{$disk}.endpoint") ?: '-'));
$this->line('url=' . (config("filesystems.disks.{$disk}.url") ?: '-'));
$this->line('bucket=' . (config("filesystems.disks.{$disk}.bucket") ?: '-'));
$this->line('path_style=' . json_encode(config("filesystems.disks.{$disk}.use_path_style_endpoint")));
try {
$stored = Storage::disk($disk)->put($path, 'ok');
if (!$stored) {
$this->error("put=false path={$path}");
return self::FAILURE;
}
$exists = Storage::disk($disk)->exists($path);
$this->line('put=ok');
$this->line('exists=' . ($exists ? 'yes' : 'no'));
$this->line('public_url=' . Storage::disk($disk)->url($path));
if ($this->option('keep')) {
$this->warn('delete=skipped');
$this->warn('Open public_url in browser or curl it from outside the container.');
} else {
Storage::disk($disk)->delete($path);
$this->line('delete=ok');
}
} catch (Throwable $e) {
$this->error(get_class($e) . ': ' . $e->getMessage());
return self::FAILURE;
}
return self::SUCCESS;
}
}