|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Console\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
|
6
|
|
|
use Illuminate\Support\Facades\File; |
|
7
|
|
|
|
|
8
|
|
|
class MigrateAnimeCovers extends Command |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* The name and signature of the console command. |
|
12
|
|
|
* |
|
13
|
|
|
* @var string |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $signature = 'anime:migrate-covers {--dry-run : Show what would be renamed without actually renaming}'; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* The console command description. |
|
19
|
|
|
* |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $description = 'Migrate anime cover files from old format ({id}.jpg) to new format ({id}-cover.jpg)'; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Execute the console command. |
|
26
|
|
|
*/ |
|
27
|
|
|
public function handle(): int |
|
28
|
|
|
{ |
|
29
|
|
|
$animeCoverPath = storage_path('covers/anime/'); |
|
30
|
|
|
$dryRun = $this->option('dry-run'); |
|
31
|
|
|
|
|
32
|
|
|
if (!is_dir($animeCoverPath)) { |
|
33
|
|
|
$this->error("Anime covers directory does not exist: {$animeCoverPath}"); |
|
34
|
|
|
return 1; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
$this->info("Scanning anime covers directory: {$animeCoverPath}"); |
|
38
|
|
|
|
|
39
|
|
|
$files = File::files($animeCoverPath); |
|
40
|
|
|
$renamed = 0; |
|
41
|
|
|
$skipped = 0; |
|
42
|
|
|
|
|
43
|
|
|
foreach ($files as $file) { |
|
44
|
|
|
$filename = $file->getFilename(); |
|
45
|
|
|
|
|
46
|
|
|
// Check if file matches old format: {id}.jpg (where id is numeric) |
|
47
|
|
|
if (preg_match('/^(\d+)\.jpg$/', $filename, $matches)) { |
|
48
|
|
|
$anidbid = $matches[1]; |
|
49
|
|
|
$newFilename = "{$anidbid}-cover.jpg"; |
|
50
|
|
|
$newPath = $animeCoverPath . $newFilename; |
|
51
|
|
|
|
|
52
|
|
|
// Skip if new format already exists |
|
53
|
|
|
if (file_exists($newPath)) { |
|
54
|
|
|
$this->warn("Skipping {$filename} - {$newFilename} already exists"); |
|
55
|
|
|
$skipped++; |
|
56
|
|
|
continue; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
if ($dryRun) { |
|
60
|
|
|
$this->line("Would rename: {$filename} -> {$newFilename}"); |
|
61
|
|
|
$renamed++; |
|
62
|
|
|
} else { |
|
63
|
|
|
if (rename($file->getPathname(), $newPath)) { |
|
64
|
|
|
$this->info("Renamed: {$filename} -> {$newFilename}"); |
|
65
|
|
|
$renamed++; |
|
66
|
|
|
} else { |
|
67
|
|
|
$this->error("Failed to rename: {$filename}"); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
if ($dryRun) { |
|
74
|
|
|
$this->info("\nDry run complete. Would rename {$renamed} file(s), skipped {$skipped}."); |
|
75
|
|
|
$this->info("Run without --dry-run to perform the migration."); |
|
76
|
|
|
} else { |
|
77
|
|
|
$this->info("\nMigration complete. Renamed {$renamed} file(s), skipped {$skipped}."); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return 0; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
|