From 3e2f33242d11daefea205e0eefccf3b282e43e8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20H=C3=BCbner?= Date: Fri, 5 May 2017 19:58:53 +0200 Subject: [PATCH] Add LD library download method --- app/config/config.yml | 2 ++ app/config/service/loader.yml | 2 +- src/AppBundle/Command/InitDataCommand.php | 8 ++---- src/AppBundle/Command/LoadModelsCommand.php | 6 ++-- src/AppBundle/Service/Loader/ModelLoader.php | 29 ++++++++++++++++++++ 5 files changed, 39 insertions(+), 8 deletions(-) diff --git a/app/config/config.yml b/app/config/config.yml index 906d45c..4a83fc1 100644 --- a/app/config/config.yml +++ b/app/config/config.yml @@ -9,6 +9,8 @@ parameters: locale: en # rebrickable csv files root URL (http://rebrickable.com/media/downloads/ or local dir containing csv files) app.rebrickable_downloads_url: 'http://rebrickable.com/media/downloads/' + app.ld_library_download_url: 'http://www.ldraw.org/library/updates/completeCA.zip' + app.media_root: "%kernel.root_dir%/../var/media/" framework: diff --git a/app/config/service/loader.yml b/app/config/service/loader.yml index f3b4a5b..315d173 100644 --- a/app/config/service/loader.yml +++ b/app/config/service/loader.yml @@ -12,7 +12,7 @@ services: service.loader.model: class: AppBundle\Service\Loader\ModelLoader - arguments: ['@service.ldview', '@app.relation.mapper'] + arguments: ['@service.stl.converter', '@app.relation.mapper', '%app.ld_library_download_url%'] parent: service.loader.base service.loader.relation: diff --git a/src/AppBundle/Command/InitDataCommand.php b/src/AppBundle/Command/InitDataCommand.php index 81e5e02..f8c51bc 100644 --- a/src/AppBundle/Command/InitDataCommand.php +++ b/src/AppBundle/Command/InitDataCommand.php @@ -19,7 +19,7 @@ class InitDataCommand extends ContainerAwareCommand ->setHelp('This command allows you to load relation between models and parts into database.') ->setDefinition( new InputDefinition([ - new InputArgument('ldraw', InputArgument::REQUIRED, 'Path to LDraw library directory'), + new InputArgument('ldraw', InputArgument::OPTIONAL, 'Path to LDraw library directory'), ]) ); } @@ -39,8 +39,7 @@ class InitDataCommand extends ContainerAwareCommand } $loadRebrickableCommad = $this->getApplication()->find('app:load:rebrickable'); - $returnCode = $loadRebrickableCommad->run(new ArrayInput([ - 'command' => 'app:load:rebrickable', ]), $output); + $returnCode = $loadRebrickableCommad->run(new ArrayInput(['command' => 'app:load:rebrickable']), $output); if ($returnCode) { return 1; @@ -48,8 +47,7 @@ class InitDataCommand extends ContainerAwareCommand $loadRelationsCommand = $this->getApplication()->find('app:load:relations'); - $returnCode = $loadRelationsCommand->run(new ArrayInput([ - 'command' => 'app:load:relations', ]), $output); + $returnCode = $loadRelationsCommand->run(new ArrayInput(['command' => 'app:load:relations']), $output); if ($returnCode) { return 1; diff --git a/src/AppBundle/Command/LoadModelsCommand.php b/src/AppBundle/Command/LoadModelsCommand.php index 9c869a3..f33ad7c 100644 --- a/src/AppBundle/Command/LoadModelsCommand.php +++ b/src/AppBundle/Command/LoadModelsCommand.php @@ -22,7 +22,7 @@ class LoadModelsCommand extends ContainerAwareCommand ->setHelp('This command allows you to load LDraw library models into database while converting .dat files to .stl format.') ->setDefinition( new InputDefinition([ - new InputArgument('ldraw', InputArgument::REQUIRED, 'Path to LDraw library directory'), + new InputArgument('ldraw', InputArgument::OPTIONAL, 'Path to LDraw library directory'), new InputOption('all', 'a', InputOption::VALUE_NONE, 'Load all models from LDraw libary folder (/parts directory)'), new InputOption('file', 'f', InputOption::VALUE_REQUIRED, 'Load single modle into database'), new InputOption('update', 'u', InputOption::VALUE_NONE, 'Overwrite already loaded models'), @@ -42,7 +42,9 @@ class LoadModelsCommand extends ContainerAwareCommand $modelLoader->setOutput($output); $modelLoader->setRewite($input->getOption('update')); - $ldraw = $input->getArgument('ldraw'); + if (!($ldraw = $input->getArgument('ldraw'))) { + $ldraw = $modelLoader->downloadLibrary(); + } if (!$input->getOption('file') && !$input->getOption('all')) { $output->writeln('Either the --all or --file option is required'); diff --git a/src/AppBundle/Service/Loader/ModelLoader.php b/src/AppBundle/Service/Loader/ModelLoader.php index da51324..3bff293 100644 --- a/src/AppBundle/Service/Loader/ModelLoader.php +++ b/src/AppBundle/Service/Loader/ModelLoader.php @@ -79,6 +79,35 @@ class ModelLoader extends BaseLoader } } + public function downloadLibrary() + { + $this->writeOutput([ + '------------------------------------------------------------------------------', + 'Downloading LDraw library', + '------------------------------------------------------------------------------', + ]); + + $libraryZip = $this->downloadFile($this->LDLibraryUrl); + + $temp_dir = tempnam(sys_get_temp_dir(), 'printabrick.'); + if (file_exists($temp_dir)) { + unlink($temp_dir); + } + mkdir($temp_dir); + + $zip = new \ZipArchive(); + if ($zip->open($libraryZip) != 'true') { + echo 'Error :- Unable to open the Zip File'; + } + $zip->extractTo($temp_dir); + $zip->close(); + unlink($libraryZip); + + $this->writeOutput(['LDraw libary downloaded']); + + return $temp_dir; + } + public function loadOne($file) { $connection = $this->em->getConnection();