From 14d45da473c6bd42adca71a3f3157b9e4a620e4a Mon Sep 17 00:00:00 2001 From: mondrake Date: Thu, 25 Jan 2024 19:27:16 +0100 Subject: [PATCH] d --- specs/Tiff/Ifd0.yaml | 4 +-- specs/Tiff/Ifd1.yaml | 2 +- src/Collection/Jpeg/Exif.php | 2 ++ src/Collection/Jpeg/Segment.php | 2 ++ src/Collection/Jpeg/SegmentApp1.php | 2 ++ src/Collection/Jpeg/SegmentCom.php | 2 ++ src/Collection/Tiff/Ifd0.php | 6 ++-- src/Collection/Tiff/Ifd1.php | 4 ++- src/Collection/Tiff/IfdAny.php | 2 ++ src/Collection/Tiff/IfdExif.php | 2 ++ src/Collection/Tiff/IfdGps.php | 2 ++ src/Collection/Tiff/IfdInteroperability.php | 2 ++ src/Model/BlockBase.php | 5 +-- src/Model/ElementBase.php | 5 +++ src/Parser/ParserBase.php | 16 +++++++++ src/Parser/Tiff/Ifd.php | 39 +++++++++++---------- 16 files changed, 69 insertions(+), 28 deletions(-) diff --git a/specs/Tiff/Ifd0.yaml b/specs/Tiff/Ifd0.yaml index 128f93ba0..ebe654898 100644 --- a/specs/Tiff/Ifd0.yaml +++ b/specs/Tiff/Ifd0.yaml @@ -10,8 +10,8 @@ alias: - '0' - Main postParse: - - 'FileEye\MediaProbe\Block\Tiff\Ifd::thumbnailToBlock' - - 'FileEye\MediaProbe\Block\Tiff\Ifd::makerNoteToBlock' + - 'FileEye\MediaProbe\Parser\Tiff\Ifd::thumbnailToBlock' + - 'FileEye\MediaProbe\Parser\Tiff\Ifd::makerNoteToBlock' compiler: exiftool: g1: IFD0 diff --git a/specs/Tiff/Ifd1.yaml b/specs/Tiff/Ifd1.yaml index 37bc79063..dbe99c6be 100644 --- a/specs/Tiff/Ifd1.yaml +++ b/specs/Tiff/Ifd1.yaml @@ -10,7 +10,7 @@ alias: - '1' - Thumbnail postParse: - - 'FileEye\MediaProbe\Block\Tiff\Ifd::thumbnailToBlock' + - 'FileEye\MediaProbe\Parser\Tiff\Ifd::thumbnailToBlock' compiler: exiftool: g1: IFD1 diff --git a/src/Collection/Jpeg/Exif.php b/src/Collection/Jpeg/Exif.php index ef7593b9d..2c3db34fd 100644 --- a/src/Collection/Jpeg/Exif.php +++ b/src/Collection/Jpeg/Exif.php @@ -15,6 +15,8 @@ class Exif extends CollectionBase { protected static $map = array ( 'title' => 'JPEG Exif data', 'class' => 'FileEye\\MediaProbe\\Block\\Jpeg\\Exif', + 'parser' => 'FileEye\\MediaProbe\\Parser\\Jpeg\\Exif', + 'writer' => 'FileEye\\MediaProbe\\Writer\\Jpeg\\Exif', 'DOMNode' => 'exif', 'id' => 'Jpeg\\Exif', 'items' => diff --git a/src/Collection/Jpeg/Segment.php b/src/Collection/Jpeg/Segment.php index 81312ceba..e6dca801b 100644 --- a/src/Collection/Jpeg/Segment.php +++ b/src/Collection/Jpeg/Segment.php @@ -15,6 +15,8 @@ class Segment extends CollectionBase { protected static $map = array ( 'title' => 'Generic JPEG data segment', 'class' => 'FileEye\\MediaProbe\\Block\\Jpeg\\Segment', + 'parser' => 'FileEye\\MediaProbe\\Parser\\Jpeg\\Segment', + 'writer' => 'FileEye\\MediaProbe\\Writer\\Jpeg\\Segment', 'DOMNode' => 'jpegSegment', 'id' => 'Jpeg\\Segment', ); diff --git a/src/Collection/Jpeg/SegmentApp1.php b/src/Collection/Jpeg/SegmentApp1.php index 1ecc609b1..ec9cf310a 100644 --- a/src/Collection/Jpeg/SegmentApp1.php +++ b/src/Collection/Jpeg/SegmentApp1.php @@ -17,6 +17,8 @@ class SegmentApp1 extends CollectionBase { 'title' => 'JPEG Application segment 1', 'payload' => 'variable', 'class' => 'FileEye\\MediaProbe\\Block\\Jpeg\\SegmentApp1', + 'parser' => 'FileEye\\MediaProbe\\Parser\\Jpeg\\SegmentApp1', + 'writer' => 'FileEye\\MediaProbe\\Writer\\Jpeg\\SegmentApp1', 'DOMNode' => 'jpegSegment', 'id' => 'Jpeg\\SegmentApp1', 'items' => diff --git a/src/Collection/Jpeg/SegmentCom.php b/src/Collection/Jpeg/SegmentCom.php index 4d2c88d9f..f6413959f 100644 --- a/src/Collection/Jpeg/SegmentCom.php +++ b/src/Collection/Jpeg/SegmentCom.php @@ -17,6 +17,8 @@ class SegmentCom extends CollectionBase { 'title' => 'JPEG Comment', 'payload' => 'variable', 'class' => 'FileEye\\MediaProbe\\Block\\Jpeg\\SegmentCom', + 'parser' => 'FileEye\\MediaProbe\\Parser\\Jpeg\\SegmentCom', + 'writer' => 'FileEye\\MediaProbe\\Writer\\Jpeg\\SegmentCom', 'DOMNode' => 'jpegSegment', 'id' => 'Jpeg\\SegmentCom', ); diff --git a/src/Collection/Tiff/Ifd0.php b/src/Collection/Tiff/Ifd0.php index e13828be0..b81093931 100644 --- a/src/Collection/Tiff/Ifd0.php +++ b/src/Collection/Tiff/Ifd0.php @@ -16,6 +16,8 @@ class Ifd0 extends CollectionBase { 'name' => 'IFD0', 'title' => 'IFD0', 'class' => 'FileEye\\MediaProbe\\Block\\Tiff\\Ifd', + 'parser' => 'FileEye\\MediaProbe\\Parser\\Tiff\\Ifd', + 'writer' => 'FileEye\\MediaProbe\\Writer\\Tiff\\Ifd', 'DOMNode' => 'ifd', 'defaultItemCollection' => 'Tiff\\Tag', 'alias' => @@ -25,8 +27,8 @@ class Ifd0 extends CollectionBase { ), 'postParse' => array ( - 0 => 'FileEye\\MediaProbe\\Block\\Tiff\\Ifd::thumbnailToBlock', - 1 => 'FileEye\\MediaProbe\\Block\\Tiff\\Ifd::makerNoteToBlock', + 0 => 'FileEye\\MediaProbe\\Parser\\Tiff\\Ifd::thumbnailToBlock', + 1 => 'FileEye\\MediaProbe\\Parser\\Tiff\\Ifd::makerNoteToBlock', ), 'id' => 'Tiff\\Ifd0', 'itemsByName' => diff --git a/src/Collection/Tiff/Ifd1.php b/src/Collection/Tiff/Ifd1.php index a5d5d47c2..f2df87b2f 100644 --- a/src/Collection/Tiff/Ifd1.php +++ b/src/Collection/Tiff/Ifd1.php @@ -16,6 +16,8 @@ class Ifd1 extends CollectionBase { 'name' => 'IFD1', 'title' => 'IFD1', 'class' => 'FileEye\\MediaProbe\\Block\\Tiff\\Ifd', + 'parser' => 'FileEye\\MediaProbe\\Parser\\Tiff\\Ifd', + 'writer' => 'FileEye\\MediaProbe\\Writer\\Tiff\\Ifd', 'DOMNode' => 'ifd', 'defaultItemCollection' => 'Tiff\\Tag', 'alias' => @@ -25,7 +27,7 @@ class Ifd1 extends CollectionBase { ), 'postParse' => array ( - 0 => 'FileEye\\MediaProbe\\Block\\Tiff\\Ifd::thumbnailToBlock', + 0 => 'FileEye\\MediaProbe\\Parser\\Tiff\\Ifd::thumbnailToBlock', ), 'id' => 'Tiff\\Ifd1', 'itemsByName' => diff --git a/src/Collection/Tiff/IfdAny.php b/src/Collection/Tiff/IfdAny.php index dca70f79e..365fc8013 100644 --- a/src/Collection/Tiff/IfdAny.php +++ b/src/Collection/Tiff/IfdAny.php @@ -15,6 +15,8 @@ class IfdAny extends CollectionBase { protected static $map = array ( 'title' => 'A catchall generic IFD definition, used as a fallback to describe tags that are not expected in a specific IFD.', 'class' => 'FileEye\\MediaProbe\\Block\\Tiff\\Ifd', + 'parser' => 'FileEye\\MediaProbe\\Parser\\Tiff\\Ifd', + 'writer' => 'FileEye\\MediaProbe\\Writer\\Tiff\\Ifd', 'defaultItemCollection' => 'Tiff\\Tag', 'id' => 'Tiff\\IfdAny', 'itemsByName' => diff --git a/src/Collection/Tiff/IfdExif.php b/src/Collection/Tiff/IfdExif.php index ab03fe66d..a40d79284 100644 --- a/src/Collection/Tiff/IfdExif.php +++ b/src/Collection/Tiff/IfdExif.php @@ -16,6 +16,8 @@ class IfdExif extends CollectionBase { 'name' => 'ExifIFD', 'title' => 'Exif IFD', 'class' => 'FileEye\\MediaProbe\\Block\\Tiff\\Ifd', + 'parser' => 'FileEye\\MediaProbe\\Parser\\Tiff\\Ifd', + 'writer' => 'FileEye\\MediaProbe\\Writer\\Tiff\\Ifd', 'DOMNode' => 'ifd', 'defaultItemCollection' => 'Tiff\\Tag', 'id' => 'Tiff\\IfdExif', diff --git a/src/Collection/Tiff/IfdGps.php b/src/Collection/Tiff/IfdGps.php index 9615bedde..ef3baf51c 100644 --- a/src/Collection/Tiff/IfdGps.php +++ b/src/Collection/Tiff/IfdGps.php @@ -16,6 +16,8 @@ class IfdGps extends CollectionBase { 'name' => 'GPS', 'title' => 'GPS IFD', 'class' => 'FileEye\\MediaProbe\\Block\\Tiff\\Ifd', + 'parser' => 'FileEye\\MediaProbe\\Parser\\Tiff\\Ifd', + 'writer' => 'FileEye\\MediaProbe\\Writer\\Tiff\\Ifd', 'DOMNode' => 'ifd', 'defaultItemCollection' => 'Tiff\\Tag', 'id' => 'Tiff\\IfdGps', diff --git a/src/Collection/Tiff/IfdInteroperability.php b/src/Collection/Tiff/IfdInteroperability.php index e86c80f0e..417bffe7b 100644 --- a/src/Collection/Tiff/IfdInteroperability.php +++ b/src/Collection/Tiff/IfdInteroperability.php @@ -16,6 +16,8 @@ class IfdInteroperability extends CollectionBase { 'name' => 'InteropIFD', 'title' => 'Interoperability IFD', 'class' => 'FileEye\\MediaProbe\\Block\\Tiff\\Ifd', + 'parser' => 'FileEye\\MediaProbe\\Parser\\Tiff\\Ifd', + 'writer' => 'FileEye\\MediaProbe\\Writer\\Tiff\\Ifd', 'DOMNode' => 'ifd', 'alias' => array ( diff --git a/src/Model/BlockBase.php b/src/Model/BlockBase.php index 9050caf0d..941b10d03 100644 --- a/src/Model/BlockBase.php +++ b/src/Model/BlockBase.php @@ -119,10 +119,7 @@ public function parseData(DataElement $dataElement, int $start = 0, ?int $size = } /** - * Invoke post-parse callbacks. - * - * @param \FileEye\MediaProbe\Data\DataElement $dataElement - * @todo + * @deprecated */ protected function executePostParseCallbacks(DataElement $dataElement): static { diff --git a/src/Model/ElementBase.php b/src/Model/ElementBase.php index ebe3e2c6f..d90f6df7e 100644 --- a/src/Model/ElementBase.php +++ b/src/Model/ElementBase.php @@ -166,6 +166,11 @@ public function isValid(): bool return $this->valid; } + public function setValid(bool $valid): void + { + $this->valid = $valid; + } + /** * @todo */ diff --git a/src/Parser/ParserBase.php b/src/Parser/ParserBase.php index fe7a6f8ba..1536dfed7 100644 --- a/src/Parser/ParserBase.php +++ b/src/Parser/ParserBase.php @@ -13,4 +13,20 @@ public function __construct( } abstract public function parseData(DataElement $data): void; + + /** + * Invoke post-parse callbacks. + * + * @param \FileEye\MediaProbe\Data\DataElement $dataElement + * @todo + */ + protected function executePostParseCallbacks(DataElement $dataElement): void + { + $post_load_callbacks = $this->block->getCollection()->getPropertyValue('postParse'); + if (!empty($post_load_callbacks)) { + foreach ($post_load_callbacks as $callback) { + call_user_func($callback, $dataElement, $this->block); + } + } + } } diff --git a/src/Parser/Tiff/Ifd.php b/src/Parser/Tiff/Ifd.php index 6a0525a30..79927e3d9 100644 --- a/src/Parser/Tiff/Ifd.php +++ b/src/Parser/Tiff/Ifd.php @@ -2,11 +2,14 @@ namespace FileEye\MediaProbe\Parser\Tiff; +use FileEye\MediaProbe\Block\Jpeg\Jpeg as JpegBlock; +use FileEye\MediaProbe\Block\Tiff\Ifd as IfdBlock; use FileEye\MediaProbe\Block\Tiff\Tiff as TiffBlock; use FileEye\MediaProbe\Collection\CollectionFactory; use FileEye\MediaProbe\Data\DataElement; use FileEye\MediaProbe\Data\DataException; use FileEye\MediaProbe\Data\DataFormat; +use FileEye\MediaProbe\Data\DataWindow; use FileEye\MediaProbe\ItemDefinition; use FileEye\MediaProbe\Parser\ParserBase; use FileEye\MediaProbe\Utility\ConvertBytes; @@ -16,13 +19,13 @@ */ class Ifd extends ParserBase { - public function parseData(DataElement $data, int $start = 0, ?int $size = null, $xxx = 0): void + public function parseData(DataElement $dataElement, int $start = 0, ?int $size = null, $xxx = 0): void { - $offset = $this->getDefinition()->getDataOffset(); + $offset = $this->block->getDefinition()->getDataOffset(); // Get the number of entries. $n = $this->getItemsCountFromData($dataElement, $offset); - assert($this->debugInfo(['dataElement' => $dataElement, 'itemsCount' => $n])); + assert($this->block->debugInfo(['dataElement' => $dataElement, 'itemsCount' => $n])); // Parse the items. for ($i = 0; $i < $n; $i++) { @@ -32,16 +35,16 @@ public function parseData(DataElement $data, int $start = 0, ?int $size = null, // Check data is accessible, warn otherwise. if ($item_definition->getDataOffset() >= $dataElement->getSize()) { - $this->warning( + $this->block->warning( 'Could not access value for item {item} in \'{ifd}\', overflow', [ 'item' => MediaProbe::dumpIntHex($item_definition->getCollection()->getPropertyValue('name') ?? 'n/a'), - 'ifd' => $this->getAttribute('name'), + 'ifd' => $this->block->getAttribute('name'), ] ); continue; } -/* $this->debug( + /* $this->debug( 'Item Offset {o} Components {c} Format {f} Formatsize {fs} Size {s} DataElement Size {des}', [ 'o' => MediaProbe::dumpIntHex($dataElement->getAbsoluteOffset($item_definition->getDataOffset())), 'c' => $item_definition->getValuesCount(), @@ -52,18 +55,18 @@ public function parseData(DataElement $data, int $start = 0, ?int $size = null, ] );*/ if ($item_definition->getDataOffset() + $item_definition->getSize() > $dataElement->getSize()) { - $this->warning( + $this->block->warning( 'Could not get value for item {item} in \'{ifd}\', not enough data', [ 'item' => MediaProbe::dumpIntHex($item_definition->getCollection()->getPropertyValue('name') ?? 'n/a'), - 'ifd' => $this->getAttribute('name'), + 'ifd' => $this->block->getAttribute('name'), ] ); continue; } // Adds the item to the DOM. - $item = new $item_class($item_definition, $this); + $item = new $item_class($item_definition, $this->block); try { if (is_a($item_class, Ifd::class, true)) { $item->parseData($dataElement); @@ -76,7 +79,7 @@ public function parseData(DataElement $data, int $start = 0, ?int $size = null, } } catch (DataException $e) { $item->error($e->getMessage()); - $item->valid = false; + $item->setValid(false); } } @@ -106,8 +109,8 @@ protected function getItemsCountFromData(DataElement $dataElement, $offset): int // Check if we have enough data. if (2 + 12 * $entries_count > $dataElement->getSize()) { $entries_count = floor(($offset - $dataElement->getSize()) / 12); - $this->warning('Wrong number of IFD entries in ifd {ifdname}, adjusted to {tags}', [ - 'ifdname' => $this->getAttribute('name'), + $this->block->warning('Wrong number of IFD entries in ifd {ifdname}, adjusted to {tags}', [ + 'ifdname' => $this->block->getAttribute('name'), 'tags' => $entries_count, ]); } @@ -141,7 +144,7 @@ protected function getItemDefinitionFromData(int $seq, DataElement $dataElement, // Fall back to the generic IFD collection if the item is missing from // the appropriate one. try { - $item_collection = $this->getCollection()->getItemCollection($id); + $item_collection = $this->block->getCollection()->getItemCollection($id); } catch (MediaProbeException $e) { if ($fallback_collection_id !== null) { $item_collection = CollectionFactory::get($fallback_collection_id)->getItemCollection($id, 0, 'Tiff\UnknownTag', [ @@ -149,7 +152,7 @@ protected function getItemDefinitionFromData(int $seq, DataElement $dataElement, 'DOMNode' => 'tag', ]); } else { - $item_collection = $this->getCollection()->getItemCollection($id, 0, 'Tiff\UnknownTag', [ + $item_collection = $this->block->getCollection()->getItemCollection($id, 0, 'Tiff\UnknownTag', [ 'item' => $id, 'DOMNode' => 'tag', ]); @@ -188,7 +191,7 @@ protected function getItemDefinitionFromData(int $seq, DataElement $dataElement, * the data from which the thumbnail will be * extracted. */ - public static function thumbnailToBlock(DataElement $dataElement, Ifd $ifd): void + public static function thumbnailToBlock(DataElement $dataElement, IfdBlock $ifd): void { if (!$ifd->getElement("tag[@name='ThumbnailOffset']") || !$ifd->getElement("tag[@name='ThumbnailLength']")) { return; @@ -220,7 +223,7 @@ public static function thumbnailToBlock(DataElement $dataElement, Ifd $ifd): voi 'offset' => $offset, 'size' => $dataElement->getSize(), ]); - $ifd->valid = false; + $ifd->setValid(false); return; } @@ -241,7 +244,7 @@ public static function thumbnailToBlock(DataElement $dataElement, Ifd $ifd): voi $size = $dataxx->getSize(); // Now move backwards until we find the EOI JPEG marker. - while ($dataxx->getByte($size - 2) !== Jpeg::JPEG_DELIMITER || $dataxx->getByte($size - 1) != CollectionFactory::get('Jpeg\Jpeg')->getItemCollectionByName('EOI')->getPropertyValue('item')) { + while ($dataxx->getByte($size - 2) !== JpegBlock::JPEG_DELIMITER || $dataxx->getByte($size - 1) != CollectionFactory::get('Jpeg\Jpeg')->getItemCollectionByName('EOI')->getPropertyValue('item')) { $size --; } if ($size != $dataxx->getSize()) { @@ -267,7 +270,7 @@ public static function thumbnailToBlock(DataElement $dataElement, Ifd $ifd): voi * @param Ifd $ifd * the root Ifd object. */ - public static function makerNoteToBlock(DataElement $d, Ifd $ifd): void + public static function makerNoteToBlock(DataElement $d, IfdBlock $ifd): void { // Get the Exif subIfd if existing. if (!$exif_ifd = $ifd->getElement("ifd[@name='ExifIFD']")) {