Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make hash appear after id #1138

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/Console/Commands/AddImageHashes.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function handle() {
(new FeatureService)->handleImage(
null,
public_path($image->imageDirectory),
$image->hash.$image->id.'-image.png',
$image->id.'-'.$image->hash.'-image.png',
$oldName
)
) {
Expand All @@ -87,7 +87,7 @@ public function handle() {
(new FeatureService)->handleImage(
null,
public_path($image->imageDirectory),
$image->hash.$image->id.'-icon.png',
$image->id.'-'.$image->hash.'-icon.png',
$oldName
)
) {
Expand Down
105 changes: 105 additions & 0 deletions app/Console/Commands/FixImageHashes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

namespace App\Console\Commands;

use App\Models\Character\CharacterCategory;
use App\Models\Currency\Currency;
use App\Models\Feature\Feature;
use App\Models\Feature\FeatureCategory;
use App\Models\Item\Item;
use App\Models\Item\ItemCategory;
use App\Models\Prompt\Prompt;
use App\Models\Prompt\PromptCategory;
use App\Models\Rarity;
use App\Models\Shop\Shop;
use App\Models\Species\Species;
use App\Models\Species\Subtype;
use App\Services\FeatureService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;

class FixImageHashes extends Command {
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'fix-image-hashes';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Corrects existing images where the hash appears before the id.';

/**
* Create a new command instance.
*/
public function __construct() {
parent::__construct();
}

/**
* Execute the console command.
*
* @return mixed
*/
public function handle() {
$images = CharacterCategory::where('has_image', 1)->whereNotNull('hash')->get();
$images = $images->concat(Currency::where('has_image', 1)->whereNotNull('hash')->orWhere('has_icon', 1)->whereNotNull('hash')->get());
$images = $images->concat(Feature::where('has_image', 1)->whereNotNull('hash')->get());
$images = $images->concat(FeatureCategory::where('has_image', 1)->whereNotNull('hash')->get());
$images = $images->concat(Item::where('has_image', 1)->whereNotNull('hash')->get());
$images = $images->concat(ItemCategory::where('has_image', 1)->whereNotNull('hash')->get());
$images = $images->concat(Prompt::where('has_image', 1)->whereNotNull('hash')->get());
$images = $images->concat(PromptCategory::where('has_image', 1)->whereNotNull('hash')->get());
$images = $images->concat(Rarity::where('has_image', 1)->whereNotNull('hash')->get());
$images = $images->concat(Shop::where('has_image', 1)->whereNotNull('hash')->get());
$images = $images->concat(Species::where('has_image', 1)->whereNotNull('hash')->get());
$images = $images->concat(Subtype::where('has_image', 1)->whereNull('hash')->get());

if ($images->count()) {
$this->line('Updating images...');
foreach ($images as $image) {
$oldName = $image->hash.$image->id.'-image.png';
$image->hash = randomString(10);
// Any service works, I can't use the abstract one
if (
File::exists(public_path($image->imageDirectory).'/'.$oldName) &&
(new FeatureService)->handleImage(
null,
public_path($image->imageDirectory),
$image->id.'-'.$image->hash.'-image.png',
$oldName
)
) {
$image->save();
} else {
$this->info('Didn\'t add hash to '.get_class($image).', this could be expected or an error, id '.$image->id);
}

// Just for currency icons
if ($image instanceof Currency) {
$oldName = $image->hash.$image->id.'-icon.png';
if (
File::exists(public_path($image->imageDirectory).'/'.$oldName) &&
(new FeatureService)->handleImage(
null,
public_path($image->imageDirectory),
$image->id.'-'.$image->hash.'-icon.png',
$oldName
)
) {
$image->save();
} else {
$this->info('Didn\'t add hash to currency icon image, this could be expected or an error, id '.$image->id);
}
}
}
$this->info('Updated images.');
} else {
$this->line('No images need updating!');
}
}
}
2 changes: 1 addition & 1 deletion app/Models/Character/CharacterCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public function getImageDirectoryAttribute() {
* @return string
*/
public function getCategoryImageFileNameAttribute() {
return $this->hash.$this->id.'-image.png';
return $this->id.'-'.$this->hash.'-image.png';
}

/**
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Currency/Currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function getImageDirectoryAttribute() {
* @return string
*/
public function getCurrencyImageFileNameAttribute() {
return $this->hash.$this->id.'-image.png';
return $this->id.'-'.$this->hash.'-image.png';
}

/**
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Feature/Feature.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public function getImageDirectoryAttribute() {
* @return string
*/
public function getImageFileNameAttribute() {
return $this->hash.$this->id.'-image.png';
return $this->id.'-'.$this->hash.'-image.png';
}

/**
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Feature/FeatureCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function getImageDirectoryAttribute() {
* @return string
*/
public function getCategoryImageFileNameAttribute() {
return $this->hash.$this->id.'-image.png';
return $this->id.'-'.$this->hash.'-image.png';
}

/**
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Item/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public function getImageDirectoryAttribute() {
* @return string
*/
public function getImageFileNameAttribute() {
return $this->hash.$this->id.'-image.png';
return $this->id.'-'.$this->hash.'-image.png';
}

/**
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Item/ItemCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function getImageDirectoryAttribute() {
* @return string
*/
public function getCategoryImageFileNameAttribute() {
return $this->hash.$this->id.'-image.png';
return $this->id.'-'.$this->hash.'-image.png';
}

/**
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Prompt/Prompt.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ public function getImageDirectoryAttribute() {
* @return string
*/
public function getImageFileNameAttribute() {
return $this->hash.$this->id.'-image.png';
return $this->id.'-'.$this->hash.'-image.png';
}

/**
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Prompt/PromptCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function getImageDirectoryAttribute() {
* @return string
*/
public function getCategoryImageFileNameAttribute() {
return $this->hash.$this->id.'-image.png';
return $this->id.'-'.$this->hash.'-image.png';
}

/**
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Rarity.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function getImageDirectoryAttribute() {
* @return string
*/
public function getRarityImageFileNameAttribute() {
return $this->hash.$this->id.'-image.png';
return $this->id.'-'.$this->hash.'-image.png';
}

/**
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Shop/Shop.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function getImageDirectoryAttribute() {
* @return string
*/
public function getShopImageFileNameAttribute() {
return $this->hash.$this->id.'-image.png';
return $this->id.'-'.$this->hash.'-image.png';
}

/**
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Species/Species.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function getImageDirectoryAttribute() {
* @return string
*/
public function getSpeciesImageFileNameAttribute() {
return $this->hash.$this->id.'-image.png';
return $this->id.'-'.$this->hash.'-image.png';
}

/**
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Species/Subtype.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public function getImageDirectoryAttribute() {
* @return string
*/
public function getSubtypeImageFileNameAttribute() {
return $this->hash.$this->id.'-image.png';
return $this->id.'-'.$this->hash.'-image.png';
}

/**
Expand Down