From 979985759bc04dfc932bd3d49ffab2b52f82b871 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20H=C3=BCbner?= Date: Tue, 14 Mar 2017 18:56:14 +0100 Subject: [PATCH] Load png image of models Load images of model with LDView --- app/config/config.yml | 10 +- app/config/services.yml | 2 +- .../Controller/DownloadController.php | 43 -------- src/AppBundle/Controller/MediaController.php | 70 ++++++++++++ src/AppBundle/Service/LDViewService.php | 102 ++++++++++++++---- 5 files changed, 155 insertions(+), 72 deletions(-) delete mode 100644 src/AppBundle/Controller/DownloadController.php create mode 100644 src/AppBundle/Controller/MediaController.php diff --git a/app/config/config.yml b/app/config/config.yml index daadc77..9a35a1e 100644 --- a/app/config/config.yml +++ b/app/config/config.yml @@ -82,7 +82,7 @@ knp_menu: default_renderer: twig knp_paginator: - page_range: 5 # default page range used in pagination control + page_range: 10 # default page range used in pagination control default_options: page_name: page # page query parameter name sort_field_name: sort # sort field query parameter name @@ -94,9 +94,9 @@ knp_paginator: oneup_flysystem: adapters: - ldraw_adapter: + media_adapter: local: - directory: "%kernel.root_dir%/../var/data/ldraw" + directory: "%kernel.root_dir%/../var/media/" filesystems: - ldraw: - adapter: ldraw_adapter \ No newline at end of file + media: + adapter: media_adapter \ No newline at end of file diff --git a/app/config/services.yml b/app/config/services.yml index 6449a17..7a23fce 100644 --- a/app/config/services.yml +++ b/app/config/services.yml @@ -24,7 +24,7 @@ services: service.ldview: class: AppBundle\Service\LDViewService - arguments: ['%ldview_bin%', '@oneup_flysystem.ldraw_filesystem'] + arguments: ['%ldview_bin%', '@oneup_flysystem.media_filesystem'] loader.rebrickable: class: AppBundle\Loader\RebrickableLoader diff --git a/src/AppBundle/Controller/DownloadController.php b/src/AppBundle/Controller/DownloadController.php deleted file mode 100644 index ce28760..0000000 --- a/src/AppBundle/Controller/DownloadController.php +++ /dev/null @@ -1,43 +0,0 @@ -get('oneup_flysystem.ldraw_filesystem'); - - if ($ldraw_filesystem->has($model->getFile())) { - $response = new BinaryFileResponse($ldraw_filesystem->getAdapter()->getPathPrefix().$model->getFile()); - $response->headers->set('Content-Type', 'application/vnd.ms-pki.stl'); - - // Create the disposition of the file - $disposition = $response->headers->makeDisposition( - ResponseHeaderBag::DISPOSITION_ATTACHMENT, - $model->getFile() - ); - - $response->headers->set('Content-Disposition', $disposition); - - return $response; - } - throw new FileNotFoundException($model->getFile()); - } -} diff --git a/src/AppBundle/Controller/MediaController.php b/src/AppBundle/Controller/MediaController.php new file mode 100644 index 0000000..958fab8 --- /dev/null +++ b/src/AppBundle/Controller/MediaController.php @@ -0,0 +1,70 @@ +get('oneup_flysystem.media_filesystem'); + + if ($mediaFilesystem->has($model->getFile())) { + $response = new BinaryFileResponse($mediaFilesystem->getAdapter()->getPathPrefix().DIRECTORY_SEPARATOR.$model->getFile()); + $response->headers->set('Content-Type', 'application/vnd.ms-pki.stl'); + + // Create the disposition of the file + $disposition = $response->headers->makeDisposition( + ResponseHeaderBag::DISPOSITION_ATTACHMENT, + $model->getId().'.stl' + ); + + $response->headers->set('Content-Disposition', $disposition); + + return $response; + } + throw new FileNotFoundException($model->getFile()); + } + + /** + * @Route("/part/{id}", name="part_image") + * + * @return Response + */ + public function PartImageAction(Part $part) + { + $mediaFilesystem = $this->get('oneup_flysystem.media_filesystem'); + + if ($mediaFilesystem->has('ldraw'.DIRECTORY_SEPARATOR.'images'.DIRECTORY_SEPARATOR.$part->getId().'.png')) { + $response = new BinaryFileResponse($mediaFilesystem->getAdapter()->getPathPrefix().DIRECTORY_SEPARATOR.'ldraw'.DIRECTORY_SEPARATOR.'images'.DIRECTORY_SEPARATOR.$part->getId().'.png'); + $response->headers->set('Content-Type', 'image/png'); + + // Create the disposition of the file + $disposition = $response->headers->makeDisposition( + ResponseHeaderBag::DISPOSITION_ATTACHMENT, + $part->getId().'png' + ); + + $response->headers->set('Content-Disposition', $disposition); + + return $response; + } + throw new FileNotFoundException($part->getId().'png'); + } +} diff --git a/src/AppBundle/Service/LDViewService.php b/src/AppBundle/Service/LDViewService.php index 719eddd..a5f18fd 100644 --- a/src/AppBundle/Service/LDViewService.php +++ b/src/AppBundle/Service/LDViewService.php @@ -7,6 +7,7 @@ use League\Flysystem\Filesystem; use Symfony\Component\Asset\Exception\LogicException; use Symfony\Component\Process\ProcessBuilder; +//TODO enable file overwrite class LDViewService { /** @@ -17,23 +18,23 @@ class LDViewService /** * @var \League\Flysystem\Filesystem */ - private $stlStorage; + private $mediaFilesystem; /** * LDViewService constructor. * - * @param string $ldview Path to LDView OSMesa binary file - * @param Filesystem $stlStorage Filesystem for generated stl model files + * @param string $ldview Path to LDView OSMesa binary file + * @param Filesystem $mediaFilesystem Filesystem for generated web assets */ - public function __construct($ldview, $stlStorage) + public function __construct($ldview, $mediaFilesystem) { $this->ldview = $ldview; - $this->stlStorage = $stlStorage; + $this->mediaFilesystem = $mediaFilesystem; } /** * Convert LDraw model from .dat format to .stl by using LDView - * stores created file to $stlStorage filesystem + * stores created file to $stlStorage filesystem. * * @param Filesystem $LDrawDir * @@ -41,28 +42,83 @@ class LDViewService */ public function datToStl($file, $LDrawDir) { - $stlFilename = $file['filename'].'.stl'; + if(!$this->mediaFilesystem->has('ldraw'.DIRECTORY_SEPARATOR.'models')) { + $this->mediaFilesystem->createDir('ldraw' . DIRECTORY_SEPARATOR . 'models'); + } - if (!$this->stlStorage->has($stlFilename)) { - $builder = new ProcessBuilder(); - $process = $builder - ->setPrefix($this->ldview) - ->setArguments([ - $LDrawDir->getAdapter()->getPathPrefix().$file['path'], - '-LDrawDir='.$LDrawDir->getAdapter()->getPathPrefix(), - '-ExportFile='.$this->stlStorage->getAdapter()->getPathPrefix().$stlFilename, - ]) - ->getProcess(); + $newFile = 'ldraw'.DIRECTORY_SEPARATOR.'models'.DIRECTORY_SEPARATOR.$file['filename'].'.stl'; - $process->run(); + if (!$this->mediaFilesystem->has($newFile)) { + $this->runLDView([ + $LDrawDir->getAdapter()->getPathPrefix().$file['path'], + '-LDrawDir='.$LDrawDir->getAdapter()->getPathPrefix(), + '-ExportFiles=1', + '-ExportSuffix=.stl', + '-ExportsDir='.$this->mediaFilesystem->getAdapter()->getPathPrefix().'ldraw'.DIRECTORY_SEPARATOR.'models', + ]); - if (!$this->stlStorage->has($stlFilename)) { - throw new LogicException($file['basename'].': new file not found'); //TODO - } elseif (!$process->isSuccessful()) { - throw new LogicException($file['basename'].' : '.$process->getOutput()); //TODO + // Check if file created successfully + if (!$this->mediaFilesystem->has($newFile)) { + throw new LogicException($newFile.': new file not found'); //TODO } } - return $this->stlStorage->get($stlFilename); + return $this->mediaFilesystem->get($newFile); + } + + /** + * Convert LDraw model from .dat format to .stl by using LDView + * stores created file to $stlStorage filesystem. + * + * @param Filesystem $LDrawDir + * + * @return File + */ + public function datToPng($file, $LDrawDir) + { + if(!$this->mediaFilesystem->has('ldraw'.DIRECTORY_SEPARATOR.'images')) { + $this->mediaFilesystem->createDir('ldraw' . DIRECTORY_SEPARATOR . 'images'); + } + + $newFile = 'ldraw'.DIRECTORY_SEPARATOR.'images'.DIRECTORY_SEPARATOR.$file['filename'].'.png'; + + if (!$this->mediaFilesystem->has($newFile)) { + $this->runLDView([ + $LDrawDir->getAdapter()->getPathPrefix().$file['path'], + '-LDrawDir='.$LDrawDir->getAdapter()->getPathPrefix(), + '-AutoCrop=1', + '-SaveAlpha=0', + '-SnapshotSuffix=.png', + '-SaveDir='.$this->mediaFilesystem->getAdapter()->getPathPrefix().'ldraw'.DIRECTORY_SEPARATOR.'images', + '-SaveSnapshots=1', + ]); + + // Check if file created successfully + if (!$this->mediaFilesystem->has($newFile)) { + throw new LogicException($newFile.': new file not found'); //TODO + } + } + + return $this->mediaFilesystem->get($newFile); + } + + /** + * Call LDView process with $arguments. + * + * @param array $arguments + */ + private function runLDView(array $arguments) + { + $builder = new ProcessBuilder(); + $process = $builder + ->setPrefix($this->ldview) + ->setArguments($arguments) + ->getProcess(); + + $process->run(); + + if (!$process->isSuccessful()) { + throw new LogicException($process->getOutput()); //TODO + } } }