Files
fewo-stommer/app/Console/Commands/ImportPhotos.php

119 lines
4.2 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Console\Commands;
use App\Enums\PhotoCategory;
use App\Models\Photo;
use App\Models\Property;
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} {--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<array{file: string, category: PhotoCategory, alt_text: string}>|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 null;
}
$files = glob($directory.'/*.{jpg,jpeg,png,webp,JPG,JPEG,PNG,WEBP}', GLOB_BRACE) ?: [];
return array_map(fn (string $file): array => [
'file' => $file,
'category' => $category,
'alt_text' => str(pathinfo($file, PATHINFO_FILENAME))->replace(['_', '-'], ' ')->squish()->ucfirst()->toString(),
], $files);
}
/**
* @return list<array{file: string, category: PhotoCategory, alt_text: string}>|null
*/
private function manifestEntries(string $manifest, string $directory): ?array
{
if (! is_file($manifest)) {
$this->error("Liste nicht gefunden: {$manifest}");
return null;
}
$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));
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;
}
}