From 229c80fe74791d332e4da67dc62a35ec82a82f0e Mon Sep 17 00:00:00 2001 From: Jean-Yves Gaulier Date: Thu, 3 Nov 2016 15:27:57 +0100 Subject: [PATCH] PHRAS-1260_INDEX-WIDTH-HEIGHT - new : added size (filesize) of document to es - new : methods getWidth(),... to record_adapter - fix : template --- .../Model/Entities/ElasticsearchRecord.php | 39 +++++++++++++++ lib/Alchemy/Phrasea/Model/RecordInterface.php | 21 ++++++++ .../Elastic/ElasticsearchRecordHydrator.php | 1 + .../Elastic/Indexer/Record/Fetcher.php | 2 +- .../Indexer/Record/Hydrator/CoreHydrator.php | 3 ++ .../Elastic/Indexer/RecordIndex.php | 1 + lib/classes/record/adapter.php | 45 +++++++++++++++++ .../web/common/technical_datas.html.twig | 48 +++++++++++-------- 8 files changed, 140 insertions(+), 20 deletions(-) diff --git a/lib/Alchemy/Phrasea/Model/Entities/ElasticsearchRecord.php b/lib/Alchemy/Phrasea/Model/Entities/ElasticsearchRecord.php index 101e63401b..7f077d02f5 100644 --- a/lib/Alchemy/Phrasea/Model/Entities/ElasticsearchRecord.php +++ b/lib/Alchemy/Phrasea/Model/Entities/ElasticsearchRecord.php @@ -38,6 +38,9 @@ class ElasticsearchRecord implements RecordInterface, MutableRecordInterface private $updated; private $created; private $sha256; + private $width; + private $height; + private $size; private $uuid; private $position; private $type; @@ -232,6 +235,42 @@ class ElasticsearchRecord implements RecordInterface, MutableRecordInterface $this->sha256 = $sha256; } + /** {@inheritdoc} */ + public function getWidth() + { + return $this->width; + } + + /** {@inheritdoc} */ + public function setWidth($width) + { + $this->width = $width; + } + + /** {@inheritdoc} */ + public function getHeight() + { + return $this->height; + } + + /** {@inheritdoc} */ + public function setHeight($height) + { + $this->height = $height; + } + + /** {@inheritdoc} */ + public function getSize() + { + return $this->size; + } + + /** {@inheritdoc} */ + public function setSize($size) + { + $this->size = $size; + } + /** * @param string|null $locale * diff --git a/lib/Alchemy/Phrasea/Model/RecordInterface.php b/lib/Alchemy/Phrasea/Model/RecordInterface.php index fb2e47fe0b..d839a38dac 100644 --- a/lib/Alchemy/Phrasea/Model/RecordInterface.php +++ b/lib/Alchemy/Phrasea/Model/RecordInterface.php @@ -48,6 +48,27 @@ interface RecordInterface extends RecordReferenceInterface /** @return array */ public function getExif(); + /** + * The width of the 'document' subdef + * + * @return integer|null + */ + public function getWidth(); + + /** + * The height of the 'document' subdef + * + * @return integer|null + */ + public function getHeight(); + + /** + * The size (filesize) of the 'document' subdef + * + * @return integer|null + */ + public function getSize(); + /** * Get Caption with requested fields if exists. * @param array $fields Returns only public fields when null diff --git a/lib/Alchemy/Phrasea/SearchEngine/Elastic/ElasticsearchRecordHydrator.php b/lib/Alchemy/Phrasea/SearchEngine/Elastic/ElasticsearchRecordHydrator.php index 9dd72e7a3f..3c6003b6fe 100644 --- a/lib/Alchemy/Phrasea/SearchEngine/Elastic/ElasticsearchRecordHydrator.php +++ b/lib/Alchemy/Phrasea/SearchEngine/Elastic/ElasticsearchRecordHydrator.php @@ -53,6 +53,7 @@ class ElasticsearchRecordHydrator $record->setSha256(igorw\get_in($data, ['sha256'], '')); $record->setWidth(igorw\get_in($data, ['width'], 0)); $record->setHeight(igorw\get_in($data, ['height'], 0)); + $record->setSize(igorw\get_in($data, ['size'], 0)); $record->setType(igorw\get_in($data, ['type'], 'unknown')); $updatedOn = igorw\get_in($data, ['updated_on']); $record->setUpdated($updatedOn ? new \DateTime($updatedOn) : $updatedOn); diff --git a/lib/Alchemy/Phrasea/SearchEngine/Elastic/Indexer/Record/Fetcher.php b/lib/Alchemy/Phrasea/SearchEngine/Elastic/Indexer/Record/Fetcher.php index fab2e099c4..ab90cb4a73 100644 --- a/lib/Alchemy/Phrasea/SearchEngine/Elastic/Indexer/Record/Fetcher.php +++ b/lib/Alchemy/Phrasea/SearchEngine/Elastic/Indexer/Record/Fetcher.php @@ -132,7 +132,7 @@ class Fetcher . ", r.sha256" // -- TODO rename in "hash" . ", r.originalname AS original_name" . ", r.mime, r.type, r.parent_record_id, r.credate AS created_on, r.moddate AS updated_on" - . ", subdef.width, subdef.height" + . ", subdef.width, subdef.height, subdef.size" . " FROM (record r INNER JOIN coll c ON (c.coll_id = r.coll_id))" . " LEFT JOIN subdef ON subdef.record_id=r.record_id AND subdef.name='document'" . " -- WHERE" diff --git a/lib/Alchemy/Phrasea/SearchEngine/Elastic/Indexer/Record/Hydrator/CoreHydrator.php b/lib/Alchemy/Phrasea/SearchEngine/Elastic/Indexer/Record/Hydrator/CoreHydrator.php index 135cf854e8..7dd92b567e 100644 --- a/lib/Alchemy/Phrasea/SearchEngine/Elastic/Indexer/Record/Hydrator/CoreHydrator.php +++ b/lib/Alchemy/Phrasea/SearchEngine/Elastic/Indexer/Record/Hydrator/CoreHydrator.php @@ -45,6 +45,9 @@ class CoreHydrator implements HydratorInterface $record['base_id'] = $this->helper->getUniqueCollectionId($this->databox_id, $record['collection_id']); $record['databox_id'] = $this->databox_id; $record['databox_name'] = $this->databox_name; + $record['width'] = (int) $record['width']; + $record['height'] = (int) $record['height']; + $record['size'] = (int) $record['size']; $record['record_type'] = ((int) $record['parent_record_id'] === 1) ? SearchEngineInterface::GEM_TYPE_STORY diff --git a/lib/Alchemy/Phrasea/SearchEngine/Elastic/Indexer/RecordIndex.php b/lib/Alchemy/Phrasea/SearchEngine/Elastic/Indexer/RecordIndex.php index 52e8e8c0b5..8f3ca67283 100644 --- a/lib/Alchemy/Phrasea/SearchEngine/Elastic/Indexer/RecordIndex.php +++ b/lib/Alchemy/Phrasea/SearchEngine/Elastic/Indexer/RecordIndex.php @@ -68,6 +68,7 @@ class RecordIndex implements MappingProvider $mapping->addIntegerField('width')->disableIndexing(); $mapping->addIntegerField('height')->disableIndexing(); + $mapping->addIntegerField('size')->disableIndexing(); $mapping->addDateField('created_on', FieldMapping::DATE_FORMAT_MYSQL_OR_CAPTION); $mapping->addDateField('updated_on', FieldMapping::DATE_FORMAT_MYSQL_OR_CAPTION); diff --git a/lib/classes/record/adapter.php b/lib/classes/record/adapter.php index 0c72b464f6..a1f15f3c45 100644 --- a/lib/classes/record/adapter.php +++ b/lib/classes/record/adapter.php @@ -98,6 +98,13 @@ class record_adapter implements RecordInterface, cache_cacheableInterface /** @var DateTime */ private $updated; + /** @var bool|null|integer */ + private $width; + /** @var bool|null|integer */ + private $height; + /** @var bool|null|integer */ + private $size; + /** * @param Application $app * @param integer $sbas_id @@ -111,6 +118,8 @@ class record_adapter implements RecordInterface, cache_cacheableInterface $this->reference = RecordReference::createFromDataboxIdAndRecordId($sbas_id, $record_id); $this->number = (int)$number; + $this->width = $this->height = $this->size = false; // means unknown for now + if ($load) { $this->load(); } @@ -171,6 +180,42 @@ class record_adapter implements RecordInterface, cache_cacheableInterface return $this->uuid; } + public function getWidth() + { + $this->getDocInfos(); + + return $this->width; + } + + public function getHeight() + { + $this->getDocInfos(); + + return $this->height; + } + + public function getSize() + { + $this->getDocInfos(); + + return $this->size; + } + + private function getDocInfos() + { + if($this->width === false) { // strict false means unknown + try { + $doc = $this->get_subdef('document'); + $this->width = $doc->get_width(); + $this->height = $doc->get_height(); + $this->size = $doc->get_size(); + } catch (\Exception $e) { + // failing once is failing ever + $this->width = $this->height = $this->size = null; + } + } + } + /** * @return DateTime * @deprecated use {@link self::getUpdated} instead diff --git a/templates/web/common/technical_datas.html.twig b/templates/web/common/technical_datas.html.twig index e010e7037b..b9c8156cbe 100644 --- a/templates/web/common/technical_datas.html.twig +++ b/templates/web/common/technical_datas.html.twig @@ -2,7 +2,7 @@ {% if record.story %}
{{ 'Story_id' | trans }}
{{ record.recordId }}
{% else %} -
{{ 'Record_id' | trans }}
{{ record.recordId }}
+
{{ 'Record_id' | trans }}
{{ record.recordId }}
{% endif %} {% if not record.story %} @@ -15,27 +15,37 @@ {% endblock %} {% block td_weight %} - {# @todo we should index document weight as well #} + {% set size = record.getSize()/1024.0 %} + {% set unit = "Ko" %} + {% if size > 1000 %} + {% set size = size/1024.0 %} + {% set unit = "Mo" %} + {% endif %} + {% if size > 1000 %} + {% set size = size/1024.0 %} + {% set unit = "Go" %} + {% endif %} +
Weight
+
{{ record.getSize() }} ({{ size|round(2) }} {{ unit }})
{% endblock %} {% block td_size %} -
{{ 'Size' | trans }}
{% set width = record.exif[constant('media_subdef::TC_DATA_WIDTH')]|default - (null) %} - {% set height = record.exif[constant('media_subdef::TC_DATA_HEIGHT')]|default(null) %} - {% if width is not none and height is not none %} - {{ width ~ " x " ~ height }} - {% endif %}
- {% set document = record.get_subdef('document') %} - {% if document and document.get_width() and document.get_height() %} -
 
-
{% if record.get_type() == 'image' and document.get_width() and document.get_height() %} - {% set size_w = (document.get_width() * (254/100) / 300) %} - {% set size_h = (document.get_height() * (254/100) / 300) %} - {{ size_w|round(1) }} x {{ size_h|round(1) }} cm (300dpi) - {% set size_w = (document.get_width() * (254/100) / 72) %}
- {% set size_h = (document.get_height() * (254/100) / 72) %} - {{ size_w|round(1) }} x {{ size_h|round(1) }} cm (72dpi) - {% endif %}
+ {% if record.getWidth() and record.getHeight() %} + {% set width = record.getWidth() %} + {% set height = record.getHeight() %} + {% if width is not none and height is not none %} +
{{ 'Size' | trans }}
+
{{ width }} x {{ height }} + {% if record.getType() == 'image' %} + {% set size_w = width * 2.54 %} + {% set size_h = height * 2.54 %} +
+ {{ (size_w/300)|round(1) }} x {{ (size_h/300)|round(1) }} cm (300 dpi) +
+ {{ (size_w/72)|round(1) }} x {{ (size_h/72)|round(1) }} cm (72 dpi) + {% endif %} +
+ {% endif %} {% endif %} {% endblock %}