generate pdf subdef

This commit is contained in:
aina-esokia
2018-06-08 15:49:46 +04:00
parent feaeae1714
commit 9180bc88ef
9 changed files with 186 additions and 13 deletions

View File

@@ -10,6 +10,7 @@
namespace Alchemy\Phrasea\Filesystem; namespace Alchemy\Phrasea\Filesystem;
use Alchemy\Phrasea\Media\Subdef\Specification\PdfSpecification;
use Alchemy\Phrasea\Model\RecordInterface; use Alchemy\Phrasea\Model\RecordInterface;
use MediaAlchemyst\Specification\SpecificationInterface; use MediaAlchemyst\Specification\SpecificationInterface;
@@ -163,6 +164,8 @@ class FilesystemService
return $this->getExtensionFromVideoCodec($spec->getVideoCodec()); return $this->getExtensionFromVideoCodec($spec->getVideoCodec());
case SpecificationInterface::TYPE_SWF: case SpecificationInterface::TYPE_SWF:
return 'swf'; return 'swf';
case PdfSpecification::TYPE_PDF:
return 'pdf';
} }
return null; return null;
@@ -205,8 +208,6 @@ class FilesystemService
return 'jpg'; return 'jpg';
case 'png': case 'png':
return 'png'; return 'png';
case 'pdf':
return 'pdf';
} }
return null; return null;

View File

@@ -34,7 +34,7 @@ class Image extends Provider
$this->registerOption(new OptionType\Boolean($this->translator->trans('Remove ICC Profile'), self::OPTION_STRIP, false)); $this->registerOption(new OptionType\Boolean($this->translator->trans('Remove ICC Profile'), self::OPTION_STRIP, false));
$this->registerOption(new OptionType\Boolean($this->translator->trans('Flatten layers'), self::OPTION_FLATTEN, false)); $this->registerOption(new OptionType\Boolean($this->translator->trans('Flatten layers'), self::OPTION_FLATTEN, false));
$this->registerOption(new OptionType\Range($this->translator->trans('Quality'), self::OPTION_QUALITY, 0, 100, 75)); $this->registerOption(new OptionType\Range($this->translator->trans('Quality'), self::OPTION_QUALITY, 0, 100, 75));
$this->registerOption(new OptionType\Enum('Image Codec', self::OPTION_ICODEC, array('jpeg', 'png', 'tiff', 'pdf'), 'jpeg')); $this->registerOption(new OptionType\Enum('Image Codec', self::OPTION_ICODEC, array('jpeg', 'png', 'tiff'), 'jpeg'));
} }
public function getType() public function getType()

View File

@@ -0,0 +1,36 @@
<?php
namespace Alchemy\Phrasea\Media\Subdef;
use Symfony\Component\Translation\TranslatorInterface;
use Alchemy\Phrasea\Media\Subdef\Specification\PdfSpecification;
class Pdf extends Provider
{
protected $options = [];
public function __construct(TranslatorInterface $translator)
{
$this->translator = $translator;
}
public function getType()
{
return self::TYPE_PDF;
}
public function getDescription()
{
return $this->translator->trans('Generates a pdf file');
}
public function getMediaAlchemystSpec()
{
if (! $this->spec) {
$this->spec = new PdfSpecification();
}
return $this->spec;
}
}

View File

@@ -0,0 +1,15 @@
<?php
namespace Alchemy\Phrasea\Media\Subdef\Specification;
use MediaAlchemyst\Specification\AbstractSpecification;
class PdfSpecification extends AbstractSpecification
{
const TYPE_PDF = 'pdf';
public function getType()
{
return self::TYPE_PDF;
}
}

View File

@@ -22,6 +22,7 @@ interface Subdef
const TYPE_VIDEO = 'video'; const TYPE_VIDEO = 'video';
const TYPE_AUDIO = 'audio'; const TYPE_AUDIO = 'audio';
const TYPE_FLEXPAPER = 'flexpaper'; const TYPE_FLEXPAPER = 'flexpaper';
const TYPE_PDF = 'pdf';
const TYPE_UNKNOWN = 'unknown'; const TYPE_UNKNOWN = 'unknown';
/** /**

View File

@@ -19,11 +19,16 @@ use Alchemy\Phrasea\Core\Event\Record\SubDefinitionsCreationEvent;
use Alchemy\Phrasea\Core\Event\Record\SubDefinitionCreationFailedEvent; use Alchemy\Phrasea\Core\Event\Record\SubDefinitionCreationFailedEvent;
use Alchemy\Phrasea\Core\Event\Record\RecordEvents; use Alchemy\Phrasea\Core\Event\Record\RecordEvents;
use Alchemy\Phrasea\Filesystem\FilesystemService; use Alchemy\Phrasea\Filesystem\FilesystemService;
use Alchemy\Phrasea\Media\Subdef\Specification\PdfSpecification;
use MediaAlchemyst\Alchemyst; use MediaAlchemyst\Alchemyst;
use MediaAlchemyst\Specification\Image;
use MediaVorus\MediaVorus; use MediaVorus\MediaVorus;
use MediaAlchemyst\Exception\ExceptionInterface as MediaAlchemystException; use MediaAlchemyst\Exception\ExceptionInterface as MediaAlchemystException;
use Neutron\TemporaryFilesystem\Manager; use Neutron\TemporaryFilesystem\Manager;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Unoconv\Exception\ExceptionInterface as UnoconvException;
use Unoconv\Exception\RuntimeException;
use Unoconv\Unoconv;
class SubdefGenerator class SubdefGenerator
{ {
@@ -70,12 +75,8 @@ class SubdefGenerator
} }
} }
$recordType = $record->getType();
if(isset($this->tmpFilePath) && $recordType != 'image'){
$recordType = 'image';
}
if (null === $subdefs = $record->getDatabox()->get_subdef_structure()->getSubdefGroup($recordType)) { if (null === $subdefs = $record->getDatabox()->get_subdef_structure()->getSubdefGroup($record->getType())) {
$this->logger->info(sprintf('Nothing to do for %s', $record->getType())); $this->logger->info(sprintf('Nothing to do for %s', $record->getType()));
$subdefs = []; $subdefs = [];
} }
@@ -169,14 +170,48 @@ class SubdefGenerator
return; return;
} }
if(isset($this->tmpFilePath)){
$this->alchemyst->turnInto($this->tmpFilePath , $pathdest, $subdef_class->getSpecs()); if (isset($this->tmpFilePath) && $subdef_class->getSpecs() instanceof Image) {
}else{
$this->alchemyst->turnInto($this->tmpFilePath, $pathdest, $subdef_class->getSpecs());
} elseif ($subdef_class->getSpecs() instanceof PdfSpecification){
$this->generatePdfSubdef($record->get_hd_file()->getPathname(), $pathdest);
} else {
$this->alchemyst->turnInto($record->get_hd_file()->getPathname(), $pathdest, $subdef_class->getSpecs()); $this->alchemyst->turnInto($record->get_hd_file()->getPathname(), $pathdest, $subdef_class->getSpecs());
} }
} catch (MediaAlchemystException $e) { } catch (MediaAlchemystException $e) {
$this->logger->error(sprintf('Subdef generation failed for record %d with message %s', $record->getRecordId(), $e->getMessage())); $this->logger->error(sprintf('Subdef generation failed for record %d with message %s', $record->getRecordId(), $e->getMessage()));
} }
} }
private function generatePdfSubdef($source, $pathdest)
{
try {
$mediafile = $this->app['mediavorus']->guess($source);
} catch (MediaVorusFileNotFoundException $e) {
throw new FileNotFoundException(sprintf('File %s not found', $source));
}
try {
if ($mediafile->getFile()->getMimeType() != 'application/pdf') {
$this->app['unoconv']->transcode(
$mediafile->getFile()->getPathname(), Unoconv::FORMAT_PDF, $pathdest
);
} else {
copy($mediafile->getFile()->getPathname(), $pathdest);
}
} catch (UnoconvException $e) {
throw new RuntimeException('Unable to transmute document to pdf due to Unoconv', null, $e);
} catch (\Exception $e) {
throw $e;
}
}
} }

View File

@@ -13,6 +13,7 @@ use Alchemy\Phrasea\Media\Subdef\Video;
use Alchemy\Phrasea\Media\Subdef\FlexPaper; use Alchemy\Phrasea\Media\Subdef\FlexPaper;
use Alchemy\Phrasea\Media\Subdef\Gif; use Alchemy\Phrasea\Media\Subdef\Gif;
use Alchemy\Phrasea\Media\Subdef\Unknown; use Alchemy\Phrasea\Media\Subdef\Unknown;
use Alchemy\Phrasea\Media\Subdef\Pdf;
use Alchemy\Phrasea\Media\Subdef\Subdef as SubdefSpecs; use Alchemy\Phrasea\Media\Subdef\Subdef as SubdefSpecs;
use Alchemy\Phrasea\Media\Type\Type as SubdefType; use Alchemy\Phrasea\Media\Type\Type as SubdefType;
use MediaAlchemyst\Specification\SpecificationInterface; use MediaAlchemyst\Specification\SpecificationInterface;
@@ -40,9 +41,9 @@ class databox_subdef
protected $translator; protected $translator;
protected static $mediaTypeToSubdefTypes = [ protected static $mediaTypeToSubdefTypes = [
SubdefType::TYPE_AUDIO => [SubdefSpecs::TYPE_IMAGE, SubdefSpecs::TYPE_AUDIO], SubdefType::TYPE_AUDIO => [SubdefSpecs::TYPE_IMAGE, SubdefSpecs::TYPE_AUDIO],
SubdefType::TYPE_DOCUMENT => [SubdefSpecs::TYPE_IMAGE, SubdefSpecs::TYPE_FLEXPAPER], SubdefType::TYPE_DOCUMENT => [SubdefSpecs::TYPE_IMAGE, SubdefSpecs::TYPE_FLEXPAPER, SubdefSpecs::TYPE_PDF],
SubdefType::TYPE_FLASH => [SubdefSpecs::TYPE_IMAGE], SubdefType::TYPE_FLASH => [SubdefSpecs::TYPE_IMAGE],
SubdefType::TYPE_IMAGE => [SubdefSpecs::TYPE_IMAGE], SubdefType::TYPE_IMAGE => [SubdefSpecs::TYPE_IMAGE, SubdefSpecs::TYPE_PDF],
SubdefType::TYPE_VIDEO => [SubdefSpecs::TYPE_IMAGE, SubdefSpecs::TYPE_VIDEO, SubdefSpecs::TYPE_ANIMATION], SubdefType::TYPE_VIDEO => [SubdefSpecs::TYPE_IMAGE, SubdefSpecs::TYPE_VIDEO, SubdefSpecs::TYPE_ANIMATION],
SubdefType::TYPE_UNKNOWN => [SubdefSpecs::TYPE_IMAGE], SubdefType::TYPE_UNKNOWN => [SubdefSpecs::TYPE_IMAGE],
]; ];
@@ -100,6 +101,9 @@ class databox_subdef
case SubdefSpecs::TYPE_FLEXPAPER: case SubdefSpecs::TYPE_FLEXPAPER:
$this->subdef_type = $this->buildFlexPaperSubdef($sd); $this->subdef_type = $this->buildFlexPaperSubdef($sd);
break; break;
case SubdefSpecs::TYPE_PDF:
$this->subdef_type = $this->buildPdfSubdef($sd);
break;
case SubdefSpecs::TYPE_UNKNOWN: case SubdefSpecs::TYPE_UNKNOWN:
$this->subdef_type = $this->buildImageSubdef($sd); $this->subdef_type = $this->buildImageSubdef($sd);
break; break;
@@ -216,6 +220,16 @@ class databox_subdef
{ {
return new FlexPaper($this->translator); return new FlexPaper($this->translator);
} }
/**
* Build Pdf Subdef object depending the SimpleXMLElement
*
* @param SimpleXMLElement $sd
* @return Pdf
*/
protected function buildPdfSubdef(SimpleXMLElement $sd)
{
return new Pdf($this->translator);
}
/** /**
* *
* @return string * @return string
@@ -360,6 +374,9 @@ class databox_subdef
case SubdefSpecs::TYPE_VIDEO: case SubdefSpecs::TYPE_VIDEO:
$mediatype_obj = new Video($this->translator); $mediatype_obj = new Video($this->translator);
break; break;
case SubdefSpecs::TYPE_PDF:
$mediatype_obj = new Pdf($this->translator);
break;
case SubdefSpecs::TYPE_UNKNOWN: case SubdefSpecs::TYPE_UNKNOWN:
$mediatype_obj = new Unknown($this->translator); $mediatype_obj = new Unknown($this->translator);
break; break;

View File

@@ -0,0 +1,51 @@
<?php
namespace Alchemy\Tests\Phrasea\Media\Subdef;
use Alchemy\Phrasea\Media\Subdef\Pdf;
use Alchemy\Phrasea\Media\Subdef\Subdef;
use Alchemy\Tests\Tools\TranslatorMockTrait;
/**
* @group functional
* @group legacy
*/
class PdfTest extends \PhraseanetTestCase
{
use TranslatorMockTrait;
/**
* @var Pdf
*/
protected $object;
public function setUp()
{
parent::setUp();
$this->object = new Pdf($this->createTranslatorMock());
}
/**
* @covers Alchemy\Phrasea\Media\Subdef\Pdf::getType
*/
public function testGetType()
{
$this->assertEquals(Subdef::TYPE_PDF, $this->object->getType());
}
/**
* @covers Alchemy\Phrasea\Media\Subdef\Pdf::getDescription
*/
public function testGetDescription()
{
$this->assertTrue(is_string($this->object->getDescription()));
}
/**
* @covers Alchemy\Phrasea\Media\Subdef\Pdf::getMediaAlchemystSpec
*/
public function testGetMediaAlchemystSpec()
{
$this->assertInstanceOf('Alchemy\\Phrasea\\Media\\Subdef\\Specification\\PdfSpecification', $this->object->getMediaAlchemystSpec());
}
}

View File

@@ -0,0 +1,17 @@
<?php
namespace Alchemy\Tests\Phrasea\Media\Subdef\Specification;
use Alchemy\Phrasea\Media\Subdef\Specification\PdfSpecification;
class PdfTest extends \PHPUnit_Framework_TestCase
{
/**
* @covers Alchemy\Phrasea\Media\Subdef\Specification\PdfSpecification::getType
*/
public function testGetType()
{
$specs = new PdfSpecification();
$this->assertEquals(PdfSpecification::TYPE_PDF, $specs->getType());
}
}