From 9df5e0c01c4d88525d4f1b58d4aa19b202dbb91b Mon Sep 17 00:00:00 2001 From: Michael RICOIS Date: Tue, 26 Jul 2016 12:01:05 +0200 Subject: [PATCH] Fallback method --- .../Application/Controller/FileController.php | 201 +++++++++++++++++- 1 file changed, 197 insertions(+), 4 deletions(-) diff --git a/module/Application/src/Application/Controller/FileController.php b/module/Application/src/Application/Controller/FileController.php index 2cbfb8b..563776b 100644 --- a/module/Application/src/Application/Controller/FileController.php +++ b/module/Application/src/Application/Controller/FileController.php @@ -55,7 +55,7 @@ class FileController extends AbstractActionController /** * Fichier SD => Client - * Depot d'un fichier dans le repertoire client du FTP (recv) + * Depot de fichiers dans le repertoire client du FTP (recv) * Traitement par lot */ public function recvAction() @@ -207,7 +207,6 @@ class FileController extends AbstractActionController } else { $this->output("Impossible de copier le fichier $source vers $dest"); } - $this->output("", $verbose); } } } @@ -738,7 +737,7 @@ class FileController extends AbstractActionController // Set directory $fluxBasePath .= '/'.$repositoryDir; - // --- Use ".fin" or ".end" files to do something + // Use ".fin" or ".end" files to do something $runExtensions = array('fin', 'end'); if (in_array($extension, $runExtensions)) { if ($optionsRunWithEndFile) { @@ -904,13 +903,207 @@ class FileController extends AbstractActionController } /** - * flux_filein + * Fallback pour distribuer les fichiers si la gestion par event n'as pas fonctionné. + * Client => SD */ public function fallbackAction() { $request = $this->getRequest(); $verbose = $request->getParam('verbose', false) || $request->getParam('v', false); $dryrun = $request->getParam('dry-run', false) || $request->getParam('n', false); + + // Get config + $pathConfig = array(); + if (array_key_exists('path', $this->appConfig)) { + $pathConfig = $this->appConfig['path']; + } + + // Get definition + $reposConfig = array(); + if (array_key_exists('repository', $this->appConfig)) { + $reposConfig = $this->appConfig['repository']; + } + + if (count($reposConfig) == 0 || count($pathConfig) == 0) { + $this->output("Aucune configuration."); + } + else { + foreach ($reposConfig as $client => $prestations) { + if (count($prestations) > 0) { + foreach($prestations as $p) { + + $prestation = $p['name']; + $type = $p['type']; + + // Base path, type and repository + if ($type == 'SFTP') { + $fluxBasePath = $pathConfig['sftp'] . '/' . $client; + } + elseif ($type == 'FTP') { + $fluxBasePath = $pathConfig['ftp'] . '/' . $client; + } + + // Repository dir + $repositoryDir = 'send'; + if (array_key_exists('directory', $p) && !empty($p['directory'])) { + $repositoryDir = $p['directory']; + } + $fluxBasePath = $fluxBasePath . '/' . $repositoryDir; + + // Set options + $optionsCopyAddDate = false; + $optionsCopyDeleteAfter = false; + $optionsRunWithEndFile = false; + $optionsLog = true; + $optionsRoute = array(); + $optionsFilterName = false; + + if (array_key_exists('in', $p) && count($p['in']) > 0) { + foreach ($p['in'] as $option => $value) { + ${'options'.$option} = $value; + } + } + + // Get files + $it = new \FilesystemIterator($fluxBasePath); + foreach ($it as $fileinfo) { + $filenameIn = $fileinfo->getFilename(); + $extension = $fileinfo->getExtension(); + + // --- Don't play with *.tck files + if ($extension == 'tck') { + continue; + } + + // --- Filters + if ($optionsFilterName === true) { + if ( strpos($filenameIn, $prestation) === false ) { + $prestation = null; continue; + } + } + if (is_string($optionsFilterName) && strlen($optionsFilterName) > 0) { + if (strpos($filenameIn, $optionsFilterName) === false) { + $prestation = null; continue; + } + } + + // Use ".fin" or ".end" files to do something + $runExtensions = array('fin', 'end'); + if (in_array($extension, $runExtensions)) { + if ($optionsRunWithEndFile) { + $extToDelete = $extension; + if (file_exists($fluxBasePath . '/' . $filenameIn)) { + $pathParts = pathinfo($fluxBasePath . '/' . $filenameIn); + $filenameIn = $pathParts['basename']; + } + else { + $this->output("Fichier fin inexistant $filenameIn"); + continue; + } + } + else { + // Do nothing + continue; + } + } + + // Get the realname of file IN or exit + if ($optionsRunWithEndFile) { + if (in_array($extension, $runExtensions)) { + $extensionLength = strlen($extension)+1; + $filenameIn = substr($filenameIn, 0, strlen($filenameIn) - $extensionLength); + $extension = ''; + } + else { + continue; + } + } + + // Detail du fichier + $nbLines = 0; + if ( strtolower(substr($filenameIn, -3)) == 'csv' ) { + $lines = file($fluxBasePath . '/' . $filenameIn); + $nbLines = count($lines); + } + $size = filesize($fluxBasePath . '/' . $filenameIn); + $dateFile = date('YmdHis', filectime($fluxBasePath . '/' . $filenameIn)); + + // Define default out filename + $filenameOut = $filenameIn; + + // Add date to filename + if ($optionsCopyAddDate) { + $extensionLength = 0; + if ($extension != '') { + $extensionLength = strlen($extension)+1; + $filenameOut = substr($filenameIn, 0, strlen($filenameIn) - $extensionLength); + $filenameOut = $filenameOut . '_' . date('YmdHis') . '.' . $extension; + } + else { + $filenameOut = $filenameIn . '_' . date('YmdHis'); + } + } + + // Before store the file send to another repository + if (count($optionsRoute) > 0) { + foreach ($optionsRoute as $tr => $value) { + switch($tr) { + case 'cp': + if (copy($fluxBasePath . '/' . $filenameIn, $value . '/' . $filenameOut)) { + $this->output("Copie du fichier $filenameIn dans $value"); + } + break; + } + } + } + + // Copy file + $destDir = $pathConfig['storage'] . '/' . $client . '/send'; + + if (!is_dir($destDir)) { + mkdir($destDir, 0755, true); + } + + if (copy($fluxBasePath . '/' . $filenameIn, $destDir. '/' . $filenameOut)) { + $this->output("Copie du fichier $filenameIn dans $destDir"); + + // Execute + if ($optionsLog === true) { + try { + $fluxM = new FluxFilein; + $fluxM->setClient($client); + $fluxM->setName($prestation); + $fluxM->setDepottype($type); + $fluxM->setDepotdate(\DateTime::createFromFormat('YmdHis', $dateFile)); + $fluxM->setDepotfile($filenameOut); + $fluxM->setNblines($nbLines); + $fluxM->setDepotfilesize($size); + $fluxM->setDateinsert(new \DateTime()); + $this->entityManager->persist($fluxM); + $this->entityManager->flush(); + $this->output("Enregistrement client:$client fichier:$filenameOut"); + } catch (\Doctrine\ORM\ORMException $e) { + $this->output("ERREUR Enregistrement client:$client fichier:$filenameOut"); + } + } + + // Suppression des fichiers + if ($optionsCopyDeleteAfter) { + unlink( $fluxBasePath . '/' . $filenameIn ); + if ($optionsRunWithEndFile) { + unlink( $fluxBasePath . '/' . $filenameIn . '.' . $extToDelete ); + } + } + } + else { + $this->output("ERREUR Copie du fichier $filenameIn dans $destDir"); + } + + } + } + } + } + } } /**