From 58fbad4730f1dd552ee90b0008bc99cef77d644b Mon Sep 17 00:00:00 2001 From: Nazar Klovanych Date: Mon, 17 Aug 2020 21:23:21 +0300 Subject: [PATCH] Improve metadata types && isExifSegment() verification for jpeg --- .../Model/GetIptcMetadata.php | 7 +++---- .../Model/Jpeg/Segment/ReadExif.php | 11 +++++------ .../Model/Jpeg/Segment/ReadIptc.php | 6 +++--- .../Model/Jpeg/Segment/ReadXmp.php | 6 +++--- .../Model/Png/Segment/ReadExif.php | 1 - .../Model/Png/Segment/ReadXmp.php | 6 +++--- .../Test/_files/empty_iptc.jpeg | Bin 19416 -> 19416 bytes 7 files changed, 17 insertions(+), 20 deletions(-) diff --git a/app/code/Magento/MediaGalleryMetadata/Model/GetIptcMetadata.php b/app/code/Magento/MediaGalleryMetadata/Model/GetIptcMetadata.php index d7290f31ee34e..e100a7f852e42 100644 --- a/app/code/Magento/MediaGalleryMetadata/Model/GetIptcMetadata.php +++ b/app/code/Magento/MediaGalleryMetadata/Model/GetIptcMetadata.php @@ -9,7 +9,6 @@ use Magento\MediaGalleryMetadataApi\Api\Data\MetadataInterface; use Magento\MediaGalleryMetadataApi\Api\Data\MetadataInterfaceFactory; -use Magento\MediaGalleryMetadataApi\Model\SegmentInterface; /** * Get metadata from IPTC block @@ -42,8 +41,8 @@ public function __construct( */ public function execute(string $data): MetadataInterface { - $title = ''; - $description = ''; + $title = null; + $description = null; $keywords = []; if (is_callable('iptcparse')) { @@ -65,7 +64,7 @@ public function execute(string $data): MetadataInterface return $this->metadataFactory->create([ 'title' => $title, 'description' => $description, - 'keywords' => $keywords + 'keywords' => !empty($keywords) ? $keywords : null ]); } } diff --git a/app/code/Magento/MediaGalleryMetadata/Model/Jpeg/Segment/ReadExif.php b/app/code/Magento/MediaGalleryMetadata/Model/Jpeg/Segment/ReadExif.php index edacd8bb65cec..b6c32296f3f7d 100644 --- a/app/code/Magento/MediaGalleryMetadata/Model/Jpeg/Segment/ReadExif.php +++ b/app/code/Magento/MediaGalleryMetadata/Model/Jpeg/Segment/ReadExif.php @@ -70,20 +70,19 @@ private function getExifData(string $filePath): MetadataInterface { $title = null; $description = null; - $keywords = []; + $keywords = null; $data = exif_read_data($filePath); - if ($data) { + if (!empty($data)) { $title = isset($data['DocumentName']) ? $data['DocumentName'] : null; $description = isset($data['ImageDescription']) ? $data['ImageDescription'] : null; - $keywords = ''; } return $this->metadataFactory->create([ 'title' => $title, 'description' => $description, - 'keywords' => !empty($keywords) ? $keywords : null + 'keywords' => $keywords ]); } @@ -97,9 +96,9 @@ private function isExifSegment(SegmentInterface $segment): bool { return $segment->getName() === self::EXIF_SEGMENT_NAME && strncmp( - substr($segment->getData(), self::EXIF_DATA_START_POSITION, 4), + substr($segment->getData(), self::EXIF_DATA_START_POSITION, 5), self::EXIF_SEGMENT_START, - self::EXIF_DATA_START_POSITION + 5 ) == 0; } } diff --git a/app/code/Magento/MediaGalleryMetadata/Model/Jpeg/Segment/ReadIptc.php b/app/code/Magento/MediaGalleryMetadata/Model/Jpeg/Segment/ReadIptc.php index 94ccb400e5e0a..e56993528a041 100644 --- a/app/code/Magento/MediaGalleryMetadata/Model/Jpeg/Segment/ReadIptc.php +++ b/app/code/Magento/MediaGalleryMetadata/Model/Jpeg/Segment/ReadIptc.php @@ -56,9 +56,9 @@ public function execute(FileInterface $file): MetadataInterface } } return $this->metadataFactory->create([ - 'title' => '', - 'description' => '', - 'keywords' => [] + 'title' => null, + 'description' => null, + 'keywords' => null ]); } diff --git a/app/code/Magento/MediaGalleryMetadata/Model/Jpeg/Segment/ReadXmp.php b/app/code/Magento/MediaGalleryMetadata/Model/Jpeg/Segment/ReadXmp.php index 81ff7200c3475..e68c86d35eb97 100644 --- a/app/code/Magento/MediaGalleryMetadata/Model/Jpeg/Segment/ReadXmp.php +++ b/app/code/Magento/MediaGalleryMetadata/Model/Jpeg/Segment/ReadXmp.php @@ -54,9 +54,9 @@ public function execute(FileInterface $file): MetadataInterface } } return $this->metadataFactory->create([ - 'title' => '', - 'description' => '', - 'keywords' => [] + 'title' => null, + 'description' => null, + 'keywords' => null ]); } diff --git a/app/code/Magento/MediaGalleryMetadata/Model/Png/Segment/ReadExif.php b/app/code/Magento/MediaGalleryMetadata/Model/Png/Segment/ReadExif.php index a0e70eef62a0b..81ecf57efaf32 100644 --- a/app/code/Magento/MediaGalleryMetadata/Model/Png/Segment/ReadExif.php +++ b/app/code/Magento/MediaGalleryMetadata/Model/Png/Segment/ReadExif.php @@ -75,7 +75,6 @@ private function getExifData(SegmentInterface $segment): MetadataInterface if ($data) { $title = isset($data['DocumentName']) ? $data['DocumentName'] : null; $description = isset($data['ImageDescription']) ? $data['ImageDescription'] : null; - $keywords = ''; } return $this->metadataFactory->create([ diff --git a/app/code/Magento/MediaGalleryMetadata/Model/Png/Segment/ReadXmp.php b/app/code/Magento/MediaGalleryMetadata/Model/Png/Segment/ReadXmp.php index 83ba554f7bf5d..518697d421474 100644 --- a/app/code/Magento/MediaGalleryMetadata/Model/Png/Segment/ReadXmp.php +++ b/app/code/Magento/MediaGalleryMetadata/Model/Png/Segment/ReadXmp.php @@ -55,9 +55,9 @@ public function execute(FileInterface $file): MetadataInterface } } return $this->metadataFactory->create([ - 'title' => '', - 'description' => '', - 'keywords' => [] + 'title' => null, + 'description' => null, + 'keywords' => null ]); } diff --git a/app/code/Magento/MediaGalleryMetadata/Test/_files/empty_iptc.jpeg b/app/code/Magento/MediaGalleryMetadata/Test/_files/empty_iptc.jpeg index 144a56dac2d3e748d47cf56e0fdde0bab2f49def..1a345c2d33fdddfb2a66fee2f40d822d8604d029 100644 GIT binary patch delta 29 XcmcaHo$d5O8H8@1MX0|0OW2QL5s