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 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|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; } }