diff --git a/lib/Alchemy/Phrasea/Filesystem/FilesystemService.php b/lib/Alchemy/Phrasea/Filesystem/FilesystemService.php index ad0ad11ceb..1a782400ad 100644 --- a/lib/Alchemy/Phrasea/Filesystem/FilesystemService.php +++ b/lib/Alchemy/Phrasea/Filesystem/FilesystemService.php @@ -10,6 +10,7 @@ namespace Alchemy\Phrasea\Filesystem; +use Alchemy\Phrasea\Media\Subdef\Specification\PdfSpecification; use Alchemy\Phrasea\Model\RecordInterface; use MediaAlchemyst\Specification\SpecificationInterface; @@ -163,6 +164,8 @@ class FilesystemService return $this->getExtensionFromVideoCodec($spec->getVideoCodec()); case SpecificationInterface::TYPE_SWF: return 'swf'; + case PdfSpecification::TYPE_PDF: + return 'pdf'; } return null; @@ -205,8 +208,6 @@ class FilesystemService return 'jpg'; case 'png': return 'png'; - case 'pdf': - return 'pdf'; } return null; diff --git a/lib/Alchemy/Phrasea/Media/Subdef/Image.php b/lib/Alchemy/Phrasea/Media/Subdef/Image.php index 921cd6c9d7..441f156b21 100644 --- a/lib/Alchemy/Phrasea/Media/Subdef/Image.php +++ b/lib/Alchemy/Phrasea/Media/Subdef/Image.php @@ -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('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\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() diff --git a/lib/Alchemy/Phrasea/Media/Subdef/Pdf.php b/lib/Alchemy/Phrasea/Media/Subdef/Pdf.php new file mode 100644 index 0000000000..4d0f087b8c --- /dev/null +++ b/lib/Alchemy/Phrasea/Media/Subdef/Pdf.php @@ -0,0 +1,36 @@ +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; + } + +} \ No newline at end of file diff --git a/lib/Alchemy/Phrasea/Media/Subdef/Specification/PdfSpecification.php b/lib/Alchemy/Phrasea/Media/Subdef/Specification/PdfSpecification.php new file mode 100644 index 0000000000..83cb5b2f3a --- /dev/null +++ b/lib/Alchemy/Phrasea/Media/Subdef/Specification/PdfSpecification.php @@ -0,0 +1,15 @@ +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())); $subdefs = []; } @@ -169,14 +170,48 @@ class SubdefGenerator return; } - if(isset($this->tmpFilePath)){ - $this->alchemyst->turnInto($this->tmpFilePath , $pathdest, $subdef_class->getSpecs()); - }else{ + + if (isset($this->tmpFilePath) && $subdef_class->getSpecs() instanceof Image) { + + $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()); + } } catch (MediaAlchemystException $e) { $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; + } + + } } diff --git a/lib/classes/databox/subdef.php b/lib/classes/databox/subdef.php index 8fe0be07f7..0dcab4ccc1 100644 --- a/lib/classes/databox/subdef.php +++ b/lib/classes/databox/subdef.php @@ -13,6 +13,7 @@ use Alchemy\Phrasea\Media\Subdef\Video; use Alchemy\Phrasea\Media\Subdef\FlexPaper; use Alchemy\Phrasea\Media\Subdef\Gif; use Alchemy\Phrasea\Media\Subdef\Unknown; +use Alchemy\Phrasea\Media\Subdef\Pdf; use Alchemy\Phrasea\Media\Subdef\Subdef as SubdefSpecs; use Alchemy\Phrasea\Media\Type\Type as SubdefType; use MediaAlchemyst\Specification\SpecificationInterface; @@ -40,9 +41,9 @@ class databox_subdef protected $translator; protected static $mediaTypeToSubdefTypes = [ 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_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_UNKNOWN => [SubdefSpecs::TYPE_IMAGE], ]; @@ -100,6 +101,9 @@ class databox_subdef case SubdefSpecs::TYPE_FLEXPAPER: $this->subdef_type = $this->buildFlexPaperSubdef($sd); break; + case SubdefSpecs::TYPE_PDF: + $this->subdef_type = $this->buildPdfSubdef($sd); + break; case SubdefSpecs::TYPE_UNKNOWN: $this->subdef_type = $this->buildImageSubdef($sd); break; @@ -216,6 +220,16 @@ class databox_subdef { 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 @@ -360,6 +374,9 @@ class databox_subdef case SubdefSpecs::TYPE_VIDEO: $mediatype_obj = new Video($this->translator); break; + case SubdefSpecs::TYPE_PDF: + $mediatype_obj = new Pdf($this->translator); + break; case SubdefSpecs::TYPE_UNKNOWN: $mediatype_obj = new Unknown($this->translator); break; diff --git a/tests/Alchemy/Tests/Phrasea/Media/Subdef/PdfTest.php b/tests/Alchemy/Tests/Phrasea/Media/Subdef/PdfTest.php new file mode 100644 index 0000000000..9edbf50c41 --- /dev/null +++ b/tests/Alchemy/Tests/Phrasea/Media/Subdef/PdfTest.php @@ -0,0 +1,51 @@ +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()); + } +} diff --git a/tests/Alchemy/Tests/Phrasea/Media/Subdef/Specification/PdfSpecificationTest.php b/tests/Alchemy/Tests/Phrasea/Media/Subdef/Specification/PdfSpecificationTest.php new file mode 100644 index 0000000000..bbe521c509 --- /dev/null +++ b/tests/Alchemy/Tests/Phrasea/Media/Subdef/Specification/PdfSpecificationTest.php @@ -0,0 +1,17 @@ +assertEquals(PdfSpecification::TYPE_PDF, $specs->getType()); + } +} \ No newline at end of file