From 012adcadfbf64277a5fc74834ef8af9c20acb227 Mon Sep 17 00:00:00 2001 From: ScuffedNewt Date: Mon, 25 Nov 2024 12:28:02 +0000 Subject: [PATCH] fix: make hash appear after id --- app/Console/Commands/AddImageHashes.php | 4 +- app/Console/Commands/FixImageHashes.php | 105 +++++++++++++++++++++ app/Models/Character/CharacterCategory.php | 2 +- app/Models/Currency/Currency.php | 2 +- app/Models/Feature/Feature.php | 2 +- app/Models/Feature/FeatureCategory.php | 2 +- app/Models/Item/Item.php | 2 +- app/Models/Item/ItemCategory.php | 2 +- app/Models/Prompt/Prompt.php | 2 +- app/Models/Prompt/PromptCategory.php | 2 +- app/Models/Rarity.php | 2 +- app/Models/Shop/Shop.php | 2 +- app/Models/Species/Species.php | 2 +- app/Models/Species/Subtype.php | 2 +- 14 files changed, 119 insertions(+), 14 deletions(-) create mode 100644 app/Console/Commands/FixImageHashes.php diff --git a/app/Console/Commands/AddImageHashes.php b/app/Console/Commands/AddImageHashes.php index c266e933aa..70b0727254 100644 --- a/app/Console/Commands/AddImageHashes.php +++ b/app/Console/Commands/AddImageHashes.php @@ -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 ) ) { @@ -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 ) ) { diff --git a/app/Console/Commands/FixImageHashes.php b/app/Console/Commands/FixImageHashes.php new file mode 100644 index 0000000000..a21d84f812 --- /dev/null +++ b/app/Console/Commands/FixImageHashes.php @@ -0,0 +1,105 @@ +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!'); + } + } +} diff --git a/app/Models/Character/CharacterCategory.php b/app/Models/Character/CharacterCategory.php index e5c8dbeb7a..d96991fb83 100644 --- a/app/Models/Character/CharacterCategory.php +++ b/app/Models/Character/CharacterCategory.php @@ -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'; } /** diff --git a/app/Models/Currency/Currency.php b/app/Models/Currency/Currency.php index 1a4846ccf9..8ae00fc28d 100644 --- a/app/Models/Currency/Currency.php +++ b/app/Models/Currency/Currency.php @@ -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'; } /** diff --git a/app/Models/Feature/Feature.php b/app/Models/Feature/Feature.php index b13dfc8729..a5e64b9276 100644 --- a/app/Models/Feature/Feature.php +++ b/app/Models/Feature/Feature.php @@ -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'; } /** diff --git a/app/Models/Feature/FeatureCategory.php b/app/Models/Feature/FeatureCategory.php index babda9e390..b691273974 100644 --- a/app/Models/Feature/FeatureCategory.php +++ b/app/Models/Feature/FeatureCategory.php @@ -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'; } /** diff --git a/app/Models/Item/Item.php b/app/Models/Item/Item.php index a780b2d642..0010ac6871 100644 --- a/app/Models/Item/Item.php +++ b/app/Models/Item/Item.php @@ -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'; } /** diff --git a/app/Models/Item/ItemCategory.php b/app/Models/Item/ItemCategory.php index fc48cd19bf..63fd762b42 100644 --- a/app/Models/Item/ItemCategory.php +++ b/app/Models/Item/ItemCategory.php @@ -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'; } /** diff --git a/app/Models/Prompt/Prompt.php b/app/Models/Prompt/Prompt.php index bd78af27ef..c680e6a432 100644 --- a/app/Models/Prompt/Prompt.php +++ b/app/Models/Prompt/Prompt.php @@ -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'; } /** diff --git a/app/Models/Prompt/PromptCategory.php b/app/Models/Prompt/PromptCategory.php index 6f0924eb2d..0b0f98efe6 100644 --- a/app/Models/Prompt/PromptCategory.php +++ b/app/Models/Prompt/PromptCategory.php @@ -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'; } /** diff --git a/app/Models/Rarity.php b/app/Models/Rarity.php index ad4017b076..daad06fa50 100644 --- a/app/Models/Rarity.php +++ b/app/Models/Rarity.php @@ -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'; } /** diff --git a/app/Models/Shop/Shop.php b/app/Models/Shop/Shop.php index 4f1b9457fc..ca7987fdcd 100644 --- a/app/Models/Shop/Shop.php +++ b/app/Models/Shop/Shop.php @@ -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'; } /** diff --git a/app/Models/Species/Species.php b/app/Models/Species/Species.php index 5656e890eb..4a5fc497ab 100644 --- a/app/Models/Species/Species.php +++ b/app/Models/Species/Species.php @@ -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'; } /** diff --git a/app/Models/Species/Subtype.php b/app/Models/Species/Subtype.php index 2c64599e6c..d1cc05d300 100644 --- a/app/Models/Species/Subtype.php +++ b/app/Models/Species/Subtype.php @@ -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'; } /**