Photo import from a manifest (category + alt text per file, idempotent); curated list of the WordPress photos
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
7355466231
commit
bc77b64ea7
4 files changed
+231
-18
No files matched your search
@@ -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<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 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<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;
|
||||
}
|
||||
|
||||
$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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user