1
0
mirror of https://github.com/ToxicCrack/PrintABrick.git synced 2025-05-18 21:20:09 -07:00

Load png image of models

Load images of model with LDView
This commit is contained in:
David Hübner 2017-03-14 18:56:14 +01:00
parent 15089022f1
commit 979985759b
5 changed files with 155 additions and 72 deletions

View File

@ -82,7 +82,7 @@ knp_menu:
default_renderer: twig default_renderer: twig
knp_paginator: knp_paginator:
page_range: 5 # default page range used in pagination control page_range: 10 # default page range used in pagination control
default_options: default_options:
page_name: page # page query parameter name page_name: page # page query parameter name
sort_field_name: sort # sort field query parameter name sort_field_name: sort # sort field query parameter name
@ -94,9 +94,9 @@ knp_paginator:
oneup_flysystem: oneup_flysystem:
adapters: adapters:
ldraw_adapter: media_adapter:
local: local:
directory: "%kernel.root_dir%/../var/data/ldraw" directory: "%kernel.root_dir%/../var/media/"
filesystems: filesystems:
ldraw: media:
adapter: ldraw_adapter adapter: media_adapter

View File

@ -24,7 +24,7 @@ services:
service.ldview: service.ldview:
class: AppBundle\Service\LDViewService class: AppBundle\Service\LDViewService
arguments: ['%ldview_bin%', '@oneup_flysystem.ldraw_filesystem'] arguments: ['%ldview_bin%', '@oneup_flysystem.media_filesystem']
loader.rebrickable: loader.rebrickable:
class: AppBundle\Loader\RebrickableLoader class: AppBundle\Loader\RebrickableLoader

View File

@ -1,43 +0,0 @@
<?php
namespace AppBundle\Controller;
use AppBundle\Entity\LDraw\Model;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/download")
*/
class DownloadController extends Controller
{
/**
* @Route("/model/{id}", name="download_model")
*
* @return Response
*/
public function modelAction(Model $model)
{
$ldraw_filesystem = $this->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());
}
}

View File

@ -0,0 +1,70 @@
<?php
namespace AppBundle\Controller;
use AppBundle\Entity\LDraw\Model;
use AppBundle\Entity\LDraw\Part;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/media")
*/
class MediaController extends Controller
{
/**
* @Route("/model/{id}", name="model_stl")
*
* @return Response
*/
public function modelAction(Model $model)
{
$mediaFilesystem = $this->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');
}
}

View File

@ -7,6 +7,7 @@ use League\Flysystem\Filesystem;
use Symfony\Component\Asset\Exception\LogicException; use Symfony\Component\Asset\Exception\LogicException;
use Symfony\Component\Process\ProcessBuilder; use Symfony\Component\Process\ProcessBuilder;
//TODO enable file overwrite
class LDViewService class LDViewService
{ {
/** /**
@ -17,23 +18,23 @@ class LDViewService
/** /**
* @var \League\Flysystem\Filesystem * @var \League\Flysystem\Filesystem
*/ */
private $stlStorage; private $mediaFilesystem;
/** /**
* LDViewService constructor. * LDViewService constructor.
* *
* @param string $ldview Path to LDView OSMesa binary file * @param string $ldview Path to LDView OSMesa binary file
* @param Filesystem $stlStorage Filesystem for generated stl model files * @param Filesystem $mediaFilesystem Filesystem for generated web assets
*/ */
public function __construct($ldview, $stlStorage) public function __construct($ldview, $mediaFilesystem)
{ {
$this->ldview = $ldview; $this->ldview = $ldview;
$this->stlStorage = $stlStorage; $this->mediaFilesystem = $mediaFilesystem;
} }
/** /**
* Convert LDraw model from .dat format to .stl by using LDView * 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 * @param Filesystem $LDrawDir
* *
@ -41,28 +42,83 @@ class LDViewService
*/ */
public function datToStl($file, $LDrawDir) 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)) { $newFile = 'ldraw'.DIRECTORY_SEPARATOR.'models'.DIRECTORY_SEPARATOR.$file['filename'].'.stl';
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',
]);
// Check if file created successfully
if (!$this->mediaFilesystem->has($newFile)) {
throw new LogicException($newFile.': new file not found'); //TODO
}
}
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(); $builder = new ProcessBuilder();
$process = $builder $process = $builder
->setPrefix($this->ldview) ->setPrefix($this->ldview)
->setArguments([ ->setArguments($arguments)
$LDrawDir->getAdapter()->getPathPrefix().$file['path'],
'-LDrawDir='.$LDrawDir->getAdapter()->getPathPrefix(),
'-ExportFile='.$this->stlStorage->getAdapter()->getPathPrefix().$stlFilename,
])
->getProcess(); ->getProcess();
$process->run(); $process->run();
if (!$this->stlStorage->has($stlFilename)) { if (!$process->isSuccessful()) {
throw new LogicException($file['basename'].': new file not found'); //TODO throw new LogicException($process->getOutput()); //TODO
} elseif (!$process->isSuccessful()) {
throw new LogicException($file['basename'].' : '.$process->getOutput()); //TODO
} }
} }
return $this->stlStorage->get($stlFilename);
}
} }