argument('path'))) { $directories[] = $this->argument('path'); } else { // Trouver tous les dossiers contenant un fichier `index.html` $directories = $disk->listContents('/', true) ->filter(fn (StorageAttributes $attributes) => ($attributes->isDir() && $disk->exists($attributes->path() . '/index.html'))) ->map(fn (StorageAttributes $attributes) => $attributes->path()) ->toArray(); } // Afficher les résultats $this->info('Répertoires contenant un index.html :'); foreach ($directories as $folder) { $this->processIndexHtml($disk, $folder); } $this->newLine(); $this->line('Récapitulatif'); $this->newLine(); foreach ($this->noBodyButAttachments as $path => $attachments) { $this->newLine(); $this->line($path); foreach ($attachments as $index => $file) { $this->line(sprintf(' > %s', $file)); } } } private function loadArticleMetadata($folder) { $jsonContent = Storage::disk(env('CONTENT_DISK'))->get($folder . '/data/index.json'); $data = json_decode($jsonContent, true); return $data ?? []; // Retourne la référence de l'image d'en-tête si disponible } private function findAttachmentDetails($folder, $reference) { $types = ['images', 'videos', 'sounds']; // Les différents types d'attachments foreach ($types as $type) { $jsonPath = $folder . '/data/' . $type . '.json'; // Chemin vers le fichier JSON if (Storage::disk(env('CONTENT_DISK'))->exists($jsonPath)) { $jsonContent = Storage::disk(env('CONTENT_DISK'))->get($jsonPath); $data = json_decode($jsonContent, true); if (isset($data['files'][$reference])) { return $data['files'][$reference]['filename']; // Retourne le 'filename' si la référence est trouvée } } } return null; // Retourne null si la référence n'est trouvée dans aucun fichier } private function extractReferencesFromMarkdown($markdownContent) { preg_match_all('//', $markdownContent, $matches); return $matches[1]; // Retourne un tableau des références trouvées } private function processIndexHtml($disk, $folder) { $htmlContent = $disk->get($folder . '/index.html'); $markdownContent = Storage::disk(env('CONTENT_DISK'))->get($folder . '/index.md'); $hasNoBody = false; $hasAttachments = false; $attachments = []; $this->newLine(); $this->line($folder); if (empty($markdownContent)) { $this->comment(' > Article has no body'); $hasNoBody = true; } $references = $this->extractReferencesFromMarkdown($markdownContent); $cover = $this->loadArticleMetadata($folder)['cover'] ?? null; $logo = $this->loadArticleMetadata($folder)['logo'] ?? null; if ($cover) { array_unshift($references, $cover); } if ($logo) { array_push($references, $logo); } $dom = new \DOMDocument(); // Charger le HTML et supprimer les erreurs pour éviter les avertissements @$dom->loadHTML($htmlContent); $xpath = new \DOMXPath($dom); $figureNodes = $xpath->query('//figure'); $index = 0; foreach ($figureNodes as $index => $figure) { // Traiter les images $imageLinks = (new \DOMXPath($dom))->query('.//a[starts-with(@href, "/storage")]', $figure); $this->processLinks($imageLinks, $references, $index, $folder, $attachments, 'image'); // Traiter les vidéos $videoSources = (new \DOMXPath($dom))->query('.//video/source[starts-with(@src, "/storage")]', $figure); $this->processLinks($videoSources, $references, $index, $folder, $attachments, 'video'); // Traiter les sons $audioSources = (new \DOMXPath($dom))->query('.//audio/source[starts-with(@src, "/storage")]', $figure); $this->processLinks($audioSources, $references, $index, $folder, $attachments, 'audio'); } foreach ($attachments as $source => $destination) { $copied = false; $contents = file_get_contents($source); if (!empty($contents)) { $copied = Storage::disk(env('CONTENT_DISK'))->put($destination, $contents); } if ($copied) { $this->info(sprintf(' > %s > %s', $source, $destination)); } else { $this->error(sprintf(' > %s > %s', $source, $destination)); } } if ($hasAttachments && $hasNoBody) { $this->noBodyButAttachments[$folder] = $attachments; } } private function processLinks($nodes, $references, &$index, $folder, &$attachments, $type) { foreach ($nodes as $node) { $pathAttribute = ($type === 'image' ? 'href' : 'src'); $href = $node->getAttribute($pathAttribute); $realPath = Str::replace('/storage', 'storage/app/public', $href); $reference = $references[$index] ?? 'Unknown'; if ($reference !== 'Unknown') { $filename = $this->findAttachmentDetails($folder, $reference); $filename = $folder . '/data/' . $filename; } else { $filename = $folder . '/data/attachments/' . $type . 's/' . basename($realPath); } // if (empty($attachments[$realPath]) || !in_array($realPath, $attachments[$reference])) { $attachments[$realPath] = $filename; // } $index++; } } }