From a5471955e69161111962c22b20945c121f5b2149 Mon Sep 17 00:00:00 2001 From: Vincent BRIDIER Date: Tue, 17 Sep 2019 16:55:16 +0200 Subject: [PATCH] Fix import media Arcadia --- app/Console/Commands/ImportMedia.php | 375 ++++++++++++++------------- app/Http/Traits/MediaTools.php | 2 + 2 files changed, 199 insertions(+), 178 deletions(-) diff --git a/app/Console/Commands/ImportMedia.php b/app/Console/Commands/ImportMedia.php index 43e60b0..0c5cd97 100644 --- a/app/Console/Commands/ImportMedia.php +++ b/app/Console/Commands/ImportMedia.php @@ -13,7 +13,7 @@ use App\Models\Dossier; class ImportMedia extends Command { - use MediaTools; + use MediaTools; /** * The name and signature of the console command. @@ -44,203 +44,222 @@ class ImportMedia extends Command * * @return mixed */ - public function handle() - { - $this->info('Démarrage du traitement'); - $files = Storage::disk('arcadia')->files(); + public function handle() + { + $this->info('Démarrage du traitement'); + + $files = File::glob(storage_path('export_arcadia') . "/DAM_MEDIA_VISUEL_*.*"); + + $this->line("-> Import du fichier DAM_MEDIA_VISUEL"); + // ---- Traitement de l'import des XML Visuel + foreach ($files as $filename) { + $path = explode("/", $filename); + $filename = $path[count($path) - 1]; + + // ---- Récupération du contenu + $oneFile = Storage::disk('arcadia')->get($filename); + $fileContent = simplexml_load_string($oneFile); + + // ---- traitement du fichier + $bar = $this->output->createProgressBar(count($fileContent->MEDIA_VISUEL)-1); + $bar->start(); + foreach ($fileContent->MEDIA_VISUEL as $row) { + $record = Media::where('arcadia_id', $row->code)->first(); + if (!$record) { // Insert new record + $record = new Media(); + $record->arcadia_id = $row->code; + if ($row->extension != "") { + $record->extension = $row->extension->typeFichier->libExtension; + } + + $record->libelle = $row->libelle; + $record->largeur = $row->nbPxLargeur; + $record->largeur_mm = $row->largeurMm; + $record->hauteur = $row->nbPxHauteur; + $record->hauteur_mm = $row->hauteurMm; + $record->resolution = $row->pppLargeur; + if ($row->version != "") { + $record->version = $row->version; + } else { + $record->version = 1; + } + + if ($row->codeTypeMedia != "") { + $record->typemedia_id = $row->codeTypeMedia; + } else { + $record->typemedia_id = 1; + } + if ($row->codeTypeVue != "") { + $record->typevue_id = $row->codeTypeVue; + } + $record->commentaire = $row->commentaire; + $record->sujet_id = $row->codeSujet; + if ($row->langue != "") { + $record->langue_id = $row->langue; + } + if ($row->copyright != "") { + $record->copyright_id = $row->copyright; + } + if ($row->codeVariante != "") { + $record->variante_id = $row->codeVariante; + } + if ($row->dateCreation != "") { + $row->dateCreation = substr($row->dateCreation, 0, 10); + $date = explode("/", $row->dateCreation); + $record->created_at = $date[2] . '-' . $date[1] . '-' . $date[0]; + } + if ($row->dateModification != "") { + $row->dateModification = substr($row->dateModification, 0, 10); + $date = explode("/", $row->dateModification); + $record->updated_at = $date[2] . '-' . $date[1] . '-' . $date[0]; + } + if ($row->datePublication != "") { + $row->datePublication = substr($row->datePublication, 0, 10); + $date = explode("/", $row->datePublication); + $record->published_at = $date[2] . '-' . $date[1] . '-' . $date[0]; + } + $record->statut = config('services.statut.public_etendu'); + + // Affectation d'un dossier et d'un uid + $destinationPath = $this->get_mediaFolder() . "/"; + $record->folder = $destinationPath; + $record->uid = sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0x0fff) | 0x4000, mt_rand(0, 0x3fff) | 0x8000, mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)); + $mediaFile = "/images/" . $record->arcadia_id . "." . $record->extension; + $fileExists = Storage::disk('arcadia')->exists($mediaFile); + if ($fileExists) { + // déplacement du média et renommage du fichier + $file = Storage::disk('arcadia')->get($mediaFile); + $full_path_source = Storage::disk('arcadia')->getDriver()->getAdapter()->applyPathPrefix($mediaFile); + $full_path_dest = Storage::disk('local')->getDriver()->getAdapter()->applyPathPrefix($destinationPath . $record->uid . '.' . $record->extension); + File::move($full_path_source, $full_path_dest); - $this->line("-> Import du fichier DAM_MEDIA_VISUEL"); - // ---- Traitement de l'import des XML Visuel - foreach ($files as $filename) { + // calcul de la taille du média + $record->taille = $this->formatBytes($this->get_file_size($full_path_dest)); - if($filename == ".gitignore") continue; + } else { + // on applique une image par défaut + $record->extension = "jpg"; + Storage::disk('local')->move("images/default.jpg", $destinationPath . $record->uid . '.' . $record->extension); - // ---- Récupération du contenu - $oneFile = Storage::disk('arcadia')->get($filename); - $fileContent = simplexml_load_string($oneFile); - - // ---- traitement du fichier - if (substr($filename, 0, 16) == "DAM_MEDIA_VISUEL") { - foreach ($fileContent->MEDIA_VISUEL as $row) { - $record = Media::where('arcadia_id', $row->code)->first(); - if(!$record){ // Insert new record - $record = new Media(); - $record->arcadia_id = $row->code; - if($row->extension != ""){ - $record->extension = $row->extension->typeFichier->libExtension; - } - - $record->libelle = $row->libelle; - $record->largeur = $row->nbPxLargeur; - $record->largeur_mm = $row->largeurMm; - $record->hauteur = $row->nbPxHauteur; - $record->hauteur_mm = $row->hauteurMm; - $record->resolution = $row->pppLargeur; - if($row->version != ""){ - $record->version = $row->version; - }else{ - $record->version = 1; - } - - if($row->codeTypeMedia != ""){ - $record->typemedia_id = $row->codeTypeMedia; - }else{ - $record->typemedia_id = 1; - } - if($row->codeTypeVue != ""){ - $record->typevue_id = $row->codeTypeVue; - } - $record->commentaire = $row->commentaire; - $record->sujet_id = $row->codeSujet; - if($row->langue != ""){ - $record->langue_id = $row->langue; - } - if($row->copyright != ""){ - $record->copyright_id = $row->copyright; - } - if($row->codeVariante != ""){ - $record->variante_id = $row->codeVariante; - } - if($row->dateCreation != ""){ - $row->dateCreation = substr($row->dateCreation, 0, 10); - $date = explode("/", $row->dateCreation); - $record->created_at = $date[2].'-'.$date[1].'-'.$date[0]; - } - if($row->dateModification != ""){ - $row->dateModification = substr($row->dateModification, 0, 10); - $date = explode("/", $row->dateModification); - $record->updated_at = $date[2].'-'.$date[1].'-'.$date[0]; - } - if($row->datePublication != ""){ - $row->datePublication = substr($row->datePublication, 0, 10); - $date = explode("/", $row->datePublication); - $record->published_at = $date[2].'-'.$date[1].'-'.$date[0]; - } - $record->statut = config('services.statut.public_etendu'); - - // Affectation d'un dossier et d'un uid - $destinationPath = $this->get_mediaFolder()."/"; - $record->folder = $destinationPath; - $record->uid = sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0x0fff) | 0x4000, mt_rand(0, 0x3fff) | 0x8000, mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)); + // calcul de la taille du média + $record->taille = $this->formatBytes($this->get_file_size("public/" . $destinationPath . $record->uid . '.' . $record->extension)); + } - $mediaFile = "/images/".$record->arcadia_id.".".$record->extension; - $fileExists = Storage::disk('arcadia')->exists($mediaFile); - if($fileExists){ - // déplacement du média et renommage du fichier - $file = Storage::disk('arcadia')->get($mediaFile); - $full_path_source = Storage::disk('arcadia')->getDriver()->getAdapter()->applyPathPrefix($mediaFile); - $full_path_dest = Storage::disk('local')->getDriver()->getAdapter()->applyPathPrefix($destinationPath.$record->uid. '.' .$record->extension); - File::move($full_path_source, $full_path_dest); + $record->save(); - // calcul de la taille du média - $record->taille = $this->formatBytes($this->get_file_size($full_path_dest)); + // ---- Creation des vignettes de base + $this->create_sys_thumb($record); + // ---- Creation des vignettes en lien avec le sujet du média + $this->create_thumb($record); + } else { + $this->line("\nDAM_MEDIA_VISUEL : le media n°" . $row->code . " existe déjà en base"); + } + $bar->advance(); + } + $bar->finish(); - }else{ - // on applique une image par défaut - $record->extension = "jpg"; - Storage::disk('local')->copy("images/default.jpg", $destinationPath.$record->uid. '.' .$record->extension); - - // calcul de la taille du média - $record->taille = $this->formatBytes($this->get_file_size("public/".$destinationPath.$record->uid. '.' .$record->extension)); - } + // ---- Suppression du fichier + //Storage::disk('arcadia')->delete($filename); + } - $record->save(); + // ---- Traitement de l'association media <-> article + $files = File::glob(storage_path('export_arcadia') . "/DAM_ASSO_ARTICLE_*.*"); + $this->line("-> Import du fichier DAM_ASSO_ARTICLE"); + foreach ($files as $filename) { + $path = explode("/", $filename); + $filename = $path[count($path) - 1]; - // ---- Creation des vignettes de base - $this->create_sys_thumb($record); - // ---- Creation des vignettes en lien avec le sujet du média - $this->create_thumb($record); - }else{ - $this->line("DAM_MEDIA_VISUEL : le media n°".$row->code. " existe déjà en base"); - } - } + // ---- Récupération du contenu + $oneFile = Storage::disk('arcadia')->get($filename); + $fileContent = simplexml_load_string($oneFile); - // ---- Suppression du fichier - //Storage::disk('arcadia')->delete($filename); + // ---- traitement du fichier + $bar = $this->output->createProgressBar(count($fileContent->mediaassoarticle)-1); + $bar->start(); + foreach ($fileContent->mediaassoarticle as $row) { + $record = Media::where('arcadia_id', $row->codeVisuel)->first(); + if ($record) { + $article = Article::where('noArt', $row->noArt)->first(); + if ($article) { + // attache l'article au média + $record->articles()->syncWithoutDetaching($article->id); + // recherche un dossier lié à la marque de l'article + $marque_id = $article->codMarq; + $dossier = Dossier::whereHas("marques", function ($query) use ($marque_id) { + $query->where("codMarq", $marque_id); + })->first(); - } - } + if ($dossier) { + // attache le dossier marque au Média + $record->dossier_id = $dossier->id; + $record->save(); + $this->line("Liason media n°" . $row->codeVisuel . " à la marque n°" . $article->libMarq); + } else { + $marque = Marque::where('codMarq', '=', $marque_id)->first(); + // On cree le dossier + $dossier = new Dossier(); + $dossier->libelle = $marque->libMarq; + $dossier->sujet_id = 1; + $dossier->system = 0; + $dossier->save(); + $dossier->marques()->syncWithoutDetaching($marque_id); + $dossier->save(); + $record->dossier_id = $dossier->id; + $record->save(); + //$this->line("Liason media n°" . $row->codeVisuel . " à la marque n°" . $article->libMarq); + } + } else { + $this->line("\nDAM_ASSO_ARTICLE : le media n°" . $row->codeVisuel . " ne peut être lié à l'article n°" . $row->noArt); + } + } else { + $this->line("\nDAM_ASSO_ARTICLE : le media n°" . $row->codeVisuel . " n'existe pas"); + } + $bar->advance(); + } + $bar->finish(); - // ---- Traitement de l'association media <-> article - $this->line("-> Import du fichier DAM_ASSO_ARTICLE"); - foreach ($files as $filename) { - if($filename == ".gitignore") continue; + // ---- Suppression du fichier + //Storage::disk('arcadia')->delete($filename); + } - // ---- Récupération du contenu - $oneFile = Storage::disk('arcadia')->get($filename); - $fileContent = simplexml_load_string($oneFile); + // ---- Traitement de l'association media <-> marque + $files = File::glob(storage_path('export_arcadia') . "/DAM_ASSO_MARQUE_*.*"); + $this->line("-> Import du fichier DAM_ASSO_MARQUE"); + foreach ($files as $filename) { + $path = explode("/", $filename); + $filename = $path[count($path) - 1]; - // ---- traitement du fichier - if (substr($filename, 0, 16) == "DAM_ASSO_ARTICLE") { - foreach ($fileContent->mediaassoarticle as $row) { - $record = Media::where('arcadia_id', $row->codeVisuel)->first(); - if($record){ - $article = Article::where('noArt', $row->noArt)->first(); - if($article){ - // attache l'article au média - $record->articles()->syncWithoutDetaching ($article->id); - // recherche un dossier lié à la marque de l'article - $marque_id = $article->codMarq; - $dossier = Dossier::whereHas("marques", function($query) use ($marque_id){ - $query->where("codMarq", $marque_id); - })->first(); + // ---- Récupération du contenu + $oneFile = Storage::disk('arcadia')->get($filename); + $fileContent = simplexml_load_string($oneFile); - if($dossier){ - // attache le dossier marque au Média - $record->dossier_id = $dossier->id; - $record->save(); - $this->line("Liason media n°".$row->codeVisuel. " à la marque n°".$article->libMarq); - }else{ - $this->line("pas de liaison de marque : le media n°".$row->codeVisuel. " ne peut être lié à l'article n°".$row->noArt); - } - }else{ - $this->line("DAM_ASSO_ARTICLE : le media n°".$row->codeVisuel. " ne peut être lié à l'article n°".$row->noArt); - } - }else{ - $this->line("DAM_ASSO_ARTICLE : le media n°".$row->codeVisuel. " n'existe pas"); - } - } + // ---- traitement du fichier + $bar = $this->output->createProgressBar(count($fileContent->mediaassomarque)-1); + $bar->start(); + foreach ($fileContent->mediaassomarque as $row) { + $record = Media::where('arcadia_id', $row->codeVisuel)->first(); + if ($record) { + // attache la marque au média + $record->marques()->attach($row->codMarq); + } else { + $this->line("\nDAM_ASSO_MARQUE : le media n°" . $row->codeVisuel . " n'existe pas"); + } + $bar->advance(); + } + $bar->finish(); - // ---- Suppression du fichier - //Storage::disk('arcadia')->delete($filename); - } - } + // ---- Suppression du fichier + //Storage::disk('arcadia')->delete($filename); + } + // TODO : Import de l'association des sociétés - // ---- Traitement de l'association media <-> marque - $this->line("-> Import du fichier DAM_ASSO_MARQUE"); - foreach ($files as $filename) { - if($filename == ".gitignore") continue; - - // ---- Récupération du contenu - $oneFile = Storage::disk('arcadia')->get($filename); - $fileContent = simplexml_load_string($oneFile); - - // ---- traitement du fichier - if (substr($filename, 0, 15) == "DAM_ASSO_MARQUE") { - foreach ($fileContent->mediaassomarque as $row) { - $record = Media::where('arcadia_id', $row->codeVisuel)->first(); - if($record){ - // attache la marque au média - $record->marques()->attach($row->codMarq); - }else{ - $this->line("DAM_ASSO_MARQUE : le media n°".$row->codeVisuel. " n'existe pas"); - } - } - - // ---- Suppression du fichier - //Storage::disk('arcadia')->delete($filename); - } - } - - // TODO : Import de l'association des sociétés - - $this->line(""); - $this->info('Fin du traitement'); - } + $this->line(""); + $this->info('Fin du traitement'); + } } diff --git a/app/Http/Traits/MediaTools.php b/app/Http/Traits/MediaTools.php index 7c7f0a0..08ee93a 100644 --- a/app/Http/Traits/MediaTools.php +++ b/app/Http/Traits/MediaTools.php @@ -38,6 +38,7 @@ trait MediaTools { $im->writeImage($fp); } $im->destroy(); + unset($im); } @@ -82,6 +83,7 @@ trait MediaTools { $im->thumbnailImage(200, 200 , true); $im->writeImage($fp); $im->destroy(); + unset($im); } public function get_mediaFolder() {