diff --git a/app/Console/Commands/ImportPhotos.php b/app/Console/Commands/ImportPhotos.php index 6ef42a2..a21c094 100644 --- a/app/Console/Commands/ImportPhotos.php +++ b/app/Console/Commands/ImportPhotos.php @@ -8,41 +8,111 @@ use Illuminate\Console\Attributes\Description; use Illuminate\Console\Attributes\Signature; use Illuminate\Console\Command; +use Spatie\MediaLibrary\MediaCollections\Models\Media; -#[Signature('photos:import {directory : Folder with JPG/PNG/WebP files} {--category=apartment : apartment, outdoor or area}')] -#[Description('Import all images of a folder as website photos (e.g. when migrating from the old website)')] +#[Signature('photos:import {directory : Folder with JPG/PNG/WebP files} {--category=apartment : apartment, outdoor or area} {--manifest= : CSV "file;category;alt_text" with paths relative to the folder (chooses and describes the photos)}')] +#[Description('Import images as website photos (e.g. when migrating from the old website); the source files are only read')] class ImportPhotos extends Command { /** * Execute the console command. */ public function handle(): int + { + $directory = rtrim((string) $this->argument('directory'), '/\\'); + $entries = $this->option('manifest') !== null + ? $this->manifestEntries((string) $this->option('manifest'), $directory) + : $this->folderEntries($directory); + + if ($entries === null) { + return self::FAILURE; + } + + $propertyId = Property::query()->value('id'); + $sort = (int) Photo::max('sort_order'); + $imported = 0; + + foreach ($entries as ['file' => $file, 'category' => $category, 'alt_text' => $altText]) { + $source = str_replace('\\', '/', substr($file, strlen($directory) + 1)); + + if (! is_file($file)) { + $this->warn("Fehlt: {$source}"); + + continue; + } + + if (Media::query()->where('collection_name', Photo::COLLECTION)->where('custom_properties->source', $source)->exists()) { + $this->line("– bereits vorhanden: {$source}"); + + continue; + } + + $photo = Photo::create([ + 'property_id' => $category === PhotoCategory::Area ? null : $propertyId, + 'category' => $category, + 'alt_text' => $altText, + 'sort_order' => ++$sort, + ]); + $photo->addMedia($file) + ->preservingOriginal() + ->withCustomProperties(['source' => $source]) + ->toMediaCollection(Photo::COLLECTION); + + $imported++; + $this->line("✓ {$photo->alt_text}"); + } + + $this->info("{$imported} Bilder importiert."); + + return self::SUCCESS; + } + + /** + * @return list|null + */ + private function folderEntries(string $directory): ?array { $category = PhotoCategory::tryFrom((string) $this->option('category')); if ($category === null) { $this->error('Unbekannte Kategorie. Erlaubt: '.implode(', ', array_column(PhotoCategory::cases(), 'value'))); - return self::FAILURE; + return null; } - $files = glob(rtrim((string) $this->argument('directory'), '/\\').'/*.{jpg,jpeg,png,webp,JPG,JPEG,PNG,WEBP}', GLOB_BRACE) ?: []; - $propertyId = $category === PhotoCategory::Area ? null : Property::query()->value('id'); - $sort = (int) Photo::max('sort_order'); + $files = glob($directory.'/*.{jpg,jpeg,png,webp,JPG,JPEG,PNG,WEBP}', GLOB_BRACE) ?: []; - foreach ($files as $file) { - $photo = Photo::create([ - 'property_id' => $propertyId, - 'category' => $category, - 'alt_text' => str(pathinfo($file, PATHINFO_FILENAME))->replace(['_', '-'], ' ')->squish()->ucfirst()->toString(), - 'sort_order' => ++$sort, - ]); - $photo->addMedia($file)->preservingOriginal()->toMediaCollection(Photo::COLLECTION); + return array_map(fn (string $file): array => [ + 'file' => $file, + 'category' => $category, + 'alt_text' => str(pathinfo($file, PATHINFO_FILENAME))->replace(['_', '-'], ' ')->squish()->ucfirst()->toString(), + ], $files); + } - $this->line("✓ {$photo->alt_text}"); + /** + * @return list|null + */ + private function manifestEntries(string $manifest, string $directory): ?array + { + if (! is_file($manifest)) { + $this->error("Liste nicht gefunden: {$manifest}"); + + return null; } - $this->info(count($files).' Bilder importiert.'); + $entries = []; + foreach (array_slice(file($manifest, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) ?: [], 1) as $number => $line) { + [$file, $category, $altText] = array_pad(str_getcsv($line, ';', '"', ''), 3, ''); + $category = PhotoCategory::tryFrom(trim($category)); - return self::SUCCESS; + if ($category === null || trim($file) === '' || trim($altText) === '') { + $this->error('Ungültige Zeile '.($number + 2).": {$line}"); + + return null; + } + + $entries[] = ['file' => $directory.'/'.ltrim(trim($file), '/'), 'category' => $category, 'alt_text' => trim($altText)]; + } + + return $entries; } } diff --git a/database/data/wordpress-photos.csv b/database/data/wordpress-photos.csv new file mode 100644 index 0000000..22dfeb3 --- /dev/null +++ b/database/data/wordpress-photos.csv @@ -0,0 +1,40 @@ +file;category;alt_text +2024/05/IMG_4041-scaled.jpg;apartment;Wohnzimmer mit blauer Ledercouch und Blick in die Küche +2024/05/IMG_4046-scaled.jpg;apartment;Wohnzimmer mit Fensterfront zur Terrasse +2023/11/01_wohn_web.jpg;apartment;Wohn- und Essbereich +2023/11/02_wohn_web.jpg;apartment;Esstisch mit vier Stühlen im Wohnbereich +2023/11/03_wohn_web.jpg;apartment;Sitzecke mit Relaxsessel und Fernseher +2023/11/06_wohn_web.jpg;apartment;Ledersofa vor der Terrassentür +2023/11/08_wohn_web.jpg;apartment;Blick vom Wohnbereich in Küche und Flur +2023/11/10_kueche_web.jpg;apartment;Einbauküche mit Essplatz +2023/11/11_kueche_web.jpg;apartment;Küchenzeile mit Backofen und Mikrowelle +2023/11/12_kueche_web.jpg;apartment;Küche mit Ceranfeld und Dunstabzug +2023/11/09_kueche_web.jpg;apartment;Flur mit Garderobe und offener Küche +2023/02/DSC00181-scaled.jpg;apartment;Schlafzimmer mit Boxspringbett +2023/11/14_schlaf_web.jpg;apartment;Schlafzimmer mit Doppelbett und Leseecke +2023/11/16_schlaf_web.jpg;apartment;Doppelbett im Schlafzimmer +2023/11/13_schlaf_web.jpg;apartment;Flur mit Blick ins Schlafzimmer und Bad +2023/12/Bad-scaled.jpg;apartment;Bad mit bodenebener Glasdusche +2023/11/18_bad_web.jpg;apartment;Bad mit Waschtisch und WC +2023/11/19_bad_web.jpg;apartment;Waschtisch mit Spiegelschrank +2025/02/Schreibtisch-1-2-scaled.jpg;apartment;Arbeitsplatz mit Schreibtisch +2023/02/bild_bg_9_gr.jpg;apartment;Grundriss der Wohnung +2024/05/IMG_4052-scaled.jpg;outdoor;Überdachte Terrasse mit Hortensien +2024/06/IMG_4045.jpg;outdoor;Terrasse mit Sitzgruppe und Blumen +2023/11/20_garten_web.jpg;outdoor;Sitzplatz im Garten der Wohnanlage +2023/11/21_garten_web.jpg;outdoor;Gartenweg zwischen den Häusern +2023/11/22_garten_web.jpg;outdoor;Garten mit Rhododendren vor den Terrassen +2023/11/23_garten_web.jpg;outdoor;Gartenteich in der Wohnanlage +2023/02/Sauna_gr.jpg;outdoor;Ruhebereich der Sauna im Haus +2024/05/DJI_0047-scaled.jpg;outdoor;Luftbild der Wohnanlage +2024/05/DJI_0050-scaled.jpg;outdoor;Luftbild mit Wald und Ostsee +2024/05/DJI_0054-scaled.jpg;outdoor;Luftbild von Kühlungsborn Richtung Ostsee +2023/02/bild_bg_10_gr.jpg;area;Strandkörbe am Ostseestrand +2023/02/fjord-gro.jpg;area;Bootshafen Kühlungsborn +2023/02/bild_bg_11_gr.jpg;area;Dampflok der Bäderbahn Molli +2023/02/geisterwald-gro.jpg;area;Gespensterwald bei Nienhagen +2023/02/hausostsee-gro.jpg;area;Villa in Bäderarchitektur +2023/02/bder4stil-gro.jpg;area;Strand mit Bäderarchitektur +2023/02/ostseealle-gro.jpg;area;Ostseeallee mit Villen +2023/02/kirche-gro.jpg;area;Backsteinkirche Kühlungsborn +2023/02/Einkaufen_gr.jpg;area;Einkaufen in Kühlungsborn diff --git a/docs/BETRIEB.md b/docs/BETRIEB.md index 210d214..926f10d 100644 --- a/docs/BETRIEB.md +++ b/docs/BETRIEB.md @@ -81,7 +81,17 @@ ## 6. Erledigt / offen im Testbetrieb - **Cronjob** (Kundenlogin › Cronjobs), jede Minute: `/opt/RZphp84/bin/php-cli /mnt/web508/e3/26/51539426/htdocs/test.fewo-stommer.de/current/artisan schedule:run` Falls Strato nicht minütlich erlaubt: n8n ruft jede Minute `https://stommer.info/automation/scheduler?token=` auf. -- Fotos importieren, Bankdaten, Vertrags- und AGB-Texte, Rechtstexte prüfen. +- Bankdaten, Vertrags- und AGB-Texte, Rechtstexte prüfen. + +### Fotos aus dem alten WordPress übernehmen + +Die Auswahl (39 aktuelle Fotos: Wohnung, Garten/Terrasse/Luftbilder, Umgebung – ohne Theme-Platzhalter, Logos, Vorschaugrößen und veraltete Einrichtung) mit Kategorie und Bildbeschreibung steht in [database/data/wordpress-photos.csv](../database/data/wordpress-photos.csv). Der Import **liest** die Dateien nur aus dem WordPress-Ordner (dort wird nichts verändert), legt Original + WebP-Größen in `shared/storage/app/public` ab und überspringt bereits importierte Fotos (wiederholbar): + +```bash +bash scripts/deploy.sh staging artisan photos:import /mnt/web508/e3/26/51539426/htdocs/www.fewo-stommer.de/wp-content/uploads --manifest=database/data/wordpress-photos.csv +``` + +Danach in der Verwaltung (Webseite › Fotos) Reihenfolge, Sichtbarkeit und Texte prüfen. - Gitea-Runner für automatisches Deployment (DEPLOYMENT.md §5). ## 7. Zugang zur Verwaltung (`/admin`) diff --git a/tests/Feature/PhotoImportTest.php b/tests/Feature/PhotoImportTest.php new file mode 100644 index 0000000..a0a6bc2 --- /dev/null +++ b/tests/Feature/PhotoImportTest.php @@ -0,0 +1,93 @@ +sourceDirectory = storage_path('framework/testing/photo-import'); + File::ensureDirectoryExists($this->sourceDirectory.'/2023/11'); + foreach (['2023/11/01_wohn_web.jpg', '2023/11/20_garten_web.jpg'] as $file) { + $image = UploadedFile::fake()->image(basename($file), 1200, 800); + File::copy($image->getPathname(), $this->sourceDirectory.'/'.$file); + } + } + + protected function tearDown(): void + { + File::deleteDirectory($this->sourceDirectory); + + parent::tearDown(); + } + + public function test_manifest_import_uses_categories_and_descriptions_and_keeps_the_sources(): void + { + $property = Property::factory()->create(); + $manifest = $this->manifest([ + '2023/11/01_wohn_web.jpg;apartment;Wohn- und Essbereich', + '2023/11/20_garten_web.jpg;outdoor;Sitzplatz im Garten', + '2023/11/fehlt.jpg;area;Gibt es nicht', + ]); + + $this->artisan('photos:import', ['directory' => $this->sourceDirectory, '--manifest' => $manifest]) + ->expectsOutputToContain('Fehlt: 2023/11/fehlt.jpg') + ->expectsOutputToContain('2 Bilder importiert.') + ->assertSuccessful(); + + $photos = Photo::orderBy('sort_order')->get(); + $this->assertSame(['Wohn- und Essbereich', 'Sitzplatz im Garten'], $photos->pluck('alt_text')->all()); + $this->assertSame([PhotoCategory::Apartment, PhotoCategory::Outdoor], $photos->pluck('category')->all()); + $this->assertTrue($photos->every(fn (Photo $photo): bool => $photo->property_id === $property->id)); + $this->assertFileExists($this->sourceDirectory.'/2023/11/01_wohn_web.jpg'); + } + + public function test_running_the_import_again_skips_photos_already_imported(): void + { + Property::factory()->create(); + $manifest = $this->manifest(['2023/11/01_wohn_web.jpg;apartment;Wohn- und Essbereich']); + + $this->artisan('photos:import', ['directory' => $this->sourceDirectory, '--manifest' => $manifest])->assertSuccessful(); + $this->artisan('photos:import', ['directory' => $this->sourceDirectory, '--manifest' => $manifest]) + ->expectsOutputToContain('bereits vorhanden: 2023/11/01_wohn_web.jpg') + ->assertSuccessful(); + + $this->assertSame(1, Photo::count()); + } + + public function test_invalid_manifest_line_aborts_before_importing(): void + { + $manifest = $this->manifest(['2023/11/01_wohn_web.jpg;keller;Unbekannte Kategorie']); + + $this->artisan('photos:import', ['directory' => $this->sourceDirectory, '--manifest' => $manifest])->assertFailed(); + + $this->assertSame(0, Photo::count()); + } + + /** + * @param list $lines + */ + private function manifest(array $lines): string + { + $path = $this->sourceDirectory.'/photos.csv'; + File::put($path, implode("\n", ['file;category;alt_text', ...$lines])."\n"); + + return $path; + } +}