From f1d1f3df624f04a8a91f1d0c40965f655ed34c37 Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 8 Jun 2017 18:06:07 +0200 Subject: [PATCH] Improve ImageLoader --- src/AppBundle/Service/Loader/BaseLoader.php | 2 +- src/AppBundle/Service/Loader/ImageLoader.php | 39 +++++++++++++------- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/src/AppBundle/Service/Loader/BaseLoader.php b/src/AppBundle/Service/Loader/BaseLoader.php index 30d3678..b79ef1f 100644 --- a/src/AppBundle/Service/Loader/BaseLoader.php +++ b/src/AppBundle/Service/Loader/BaseLoader.php @@ -117,7 +117,7 @@ abstract class BaseLoader if (false === file_put_contents($temp, fopen($url, 'r', 0, $ctx))) { throw new WriteErrorException($temp); } - } catch (ContextErrorException $e) { + } catch (\ErrorException $e) { throw new FileNotFoundException($url); } catch (\Exception $e) { throw new LogicException($e); diff --git a/src/AppBundle/Service/Loader/ImageLoader.php b/src/AppBundle/Service/Loader/ImageLoader.php index 599f88e..9f91300 100644 --- a/src/AppBundle/Service/Loader/ImageLoader.php +++ b/src/AppBundle/Service/Loader/ImageLoader.php @@ -29,8 +29,9 @@ class ImageLoader extends BaseLoader } /** - * @param $color - * @param null $path + * Download ZIP file with part images from rebrickable and unzip file to filesystem + * + * @param integer $color color id used by rebrickable */ public function loadColorFromRebrickable($color) { @@ -47,34 +48,46 @@ class ImageLoader extends BaseLoader $zip->close(); $this->output->writeln(['Done!']); } else { - $this->output->writeln(['Extraction of file failed!']); + $this->logger->error('Extraction of file failed!'); } } /** - * Load images of models. + * Load missing images of models */ public function loadMissingModelImages() { + // Get models without image + $missing = []; $models = $this->em->getRepository(Model::class)->findAll(); - - $this->initProgressBar(count($models)); foreach ($models as $model) { - $this->progressBar->setMessage($model->getId()); if (!$this->mediaFilesystem->has('images'.DIRECTORY_SEPARATOR.'-1'.DIRECTORY_SEPARATOR.$model->getId().'.png')) { - try { - $this->loadModelImage($this->mediaFilesystem->getAdapter()->getPathPrefix().$model->getPath()); - } catch (\Exception $e) { - dump($e->getMessage()); - } + $missing[] = $model; + } + } + unset($models); + + // Render images + $this->output->writeln([ + "Rendering missing images of models", + ]); + $this->initProgressBar(count($missing)); + foreach ($missing as $model) { + $this->progressBar->setMessage($model->getId()); + + try { + $this->loadModelImage($this->mediaFilesystem->getAdapter()->getPathPrefix().$model->getPath()); + } catch (\Exception $e) { + $this->logger->error('Error rendering model '.$model->getId().' image', $e); } $this->progressBar->advance(); } + $this->progressBar->finish(); } /** - * Render model and save image into co. + * Render model and save image into * * @param $file */