Skip to content

NOW MANAGED ON CODEBERG

Fixed shelf covers being stored as 'cover_book'

Dan Brownf28ed0ef0b21
diff --git a/app/Entities/Models/Bookshelf.php b/app/Entities/Models/Bookshelf.php
index b2dab252a..cdc6648f9 100644
--- a/app/Entities/Models/Bookshelf.php
+++ b/app/Entities/Models/Bookshelf.php
@@ -86,7 +86,7 @@ class Bookshelf extends Entity implements HasCoverImage
*/
public function coverImageTypeKey(): string
{
- return 'cover_shelf';
+ return 'cover_bookshelf';
}
/**
diff --git a/app/Entities/Repos/BaseRepo.php b/app/Entities/Repos/BaseRepo.php
index 747d1b176..da939e102 100644
--- a/app/Entities/Repos/BaseRepo.php
+++ b/app/Entities/Repos/BaseRepo.php
@@ -86,8 +86,9 @@ class BaseRepo
public function updateCoverImage($entity, ?UploadedFile $coverImage, bool $removeImage = false)
{
if ($coverImage) {
+ $imageType = $entity->coverImageTypeKey();
$this->imageRepo->destroyImage($entity->cover);
- $image = $this->imageRepo->saveNew($coverImage, 'cover_book', $entity->id, 512, 512, true);
+ $image = $this->imageRepo->saveNew($coverImage, $imageType, $entity->id, 512, 512, true);
$entity->cover()->associate($image);
$entity->save();
}
diff --git a/database/migrations/2022_09_02_082910_fix_shelf_cover_image_types.php b/database/migrations/2022_09_02_082910_fix_shelf_cover_image_types.php
new file mode 100644
index 000000000..837bdfe24
--- /dev/null
+++ b/database/migrations/2022_09_02_082910_fix_shelf_cover_image_types.php
@@ -0,0 +1,42 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Support\Facades\DB;
+
+class FixShelfCoverImageTypes extends Migration
+{
+ /**
+ * Run the migrations.
+ *
+ * @return void
+ */
+ public function up()
+ {
+ // This updates the 'type' field for images, uploaded as shelf cover images,
+ // to be cover_bookshelf instead of cover_book.
+ // This does not fix their paths, since fixing that requires a more complicated operation,
+ // but their path does not affect functionality at time of this fix.
+
+ $shelfImageIds = DB::table('bookshelves')
+ ->whereNotNull('image_id')
+ ->pluck('image_id')
+ ->values()->all();
+
+ DB::table('images')
+ ->where('type', '=', 'cover_book')
+ ->whereIn('id', $shelfImageIds)
+ ->update(['type' => 'cover_bookshelf']);
+ }
+
+ /**
+ * Reverse the migrations.
+ *
+ * @return void
+ */
+ public function down()
+ {
+ DB::table('images')
+ ->where('type', '=', 'cover_bookshelf')
+ ->update(['type' => 'cover_book']);
+ }
+}
diff --git a/tests/Entity/BookShelfTest.php b/tests/Entity/BookShelfTest.php
index 5a7107ff0..44d30a548 100644
--- a/tests/Entity/BookShelfTest.php
+++ b/tests/Entity/BookShelfTest.php
@@ -125,6 +125,7 @@ class BookShelfTest extends TestCase
'image_id' => $lastImage->id,
]);
$this->assertEquals($lastImage->id, $shelf->cover->id);
+ $this->assertEquals('cover_bookshelf', $lastImage->type);
}
public function test_shelf_view()