84 lines
2.4 KiB
PHP
84 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\DB;
|
|
use App\Services\AvatarService;
|
|
|
|
class AvatarsMigrate extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'avatars:migrate {--force}';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Migrate legacy avatars to new WebP avatar storage';
|
|
|
|
protected $service;
|
|
|
|
public function __construct(AvatarService $service)
|
|
{
|
|
parent::__construct();
|
|
$this->service = $service;
|
|
}
|
|
|
|
public function handle()
|
|
{
|
|
$this->info('Starting avatar migration...');
|
|
|
|
// Try to read legacy data from user_profiles.avatar_legacy or users.avatar_legacy or users.icon
|
|
$rows = DB::table('user_profiles')->select('user_id', 'avatar_legacy')->whereNotNull('avatar_legacy')->get();
|
|
|
|
if ($rows->isEmpty()) {
|
|
// fallback to users table
|
|
$rows = DB::table('users')->select('user_id', 'icon as avatar_legacy')->whereNotNull('icon')->get();
|
|
}
|
|
|
|
$count = 0;
|
|
foreach ($rows as $row) {
|
|
$userId = $row->user_id;
|
|
$legacy = $row->avatar_legacy ?? null;
|
|
if (!$legacy) {
|
|
continue;
|
|
}
|
|
|
|
// Try common legacy paths
|
|
$candidates = [
|
|
public_path('user-picture/' . $legacy),
|
|
public_path('avatar/' . $userId . '/' . $legacy),
|
|
storage_path('app/public/user-picture/' . $legacy),
|
|
storage_path('app/public/avatar/' . $userId . '/' . $legacy),
|
|
];
|
|
|
|
$found = false;
|
|
foreach ($candidates as $p) {
|
|
if (file_exists($p) && is_readable($p)) {
|
|
$this->info("Processing user {$userId} from {$p}");
|
|
$hash = $this->service->storeFromLegacyFile($userId, $p);
|
|
if ($hash) {
|
|
$this->info(" -> migrated, hash={$hash}");
|
|
$count++;
|
|
$found = true;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!$found) {
|
|
$this->warn("Legacy file not found for user {$userId}, filename={$legacy}");
|
|
}
|
|
}
|
|
|
|
$this->info("Migration complete. Processed: {$count}");
|
|
return 0;
|
|
}
|
|
}
|