PHRAS-1260_INDEX-WIDTH-HEIGHT

- new : added size (filesize) of document to es
 - new : methods getWidth(),... to record_adapter
 - fix : template
This commit is contained in:
Jean-Yves Gaulier
2016-11-03 15:27:57 +01:00
parent 68ab86d9d4
commit 229c80fe74
8 changed files with 140 additions and 20 deletions

View File

@@ -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
*

View File

@@ -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

View File

@@ -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);

View File

@@ -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"

View File

@@ -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

View File

@@ -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);

View File

@@ -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

View File

@@ -2,7 +2,7 @@
{% if record.story %}
<dt>{{ 'Story_id' | trans }}</dt><dd>{{ record.recordId }}</dd>
{% else %}
<dt>{{ 'Record_id' | trans }}</dt><dd>{{ record.recordId }}</dd>
<dt>{{ 'Record_id' | trans }}</dt><dd>{{ record.recordId }}</dd>
{% 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 %}
<dt>Weight</dt>
<dd>{{ record.getSize() }}&nbsp;({{ size|round(2) }}&nbsp;{{ unit }})</dd>
{% endblock %}
{% block td_size %}
<dt>{{ 'Size' | trans }}</dt><dd>{% 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 %}</dd>
{% set document = record.get_subdef('document') %}
{% if document and document.get_width() and document.get_height() %}
<dt>&nbsp;</dt>
<dd>{% 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) %}<br/>
{% set size_h = (document.get_height() * (254/100) / 72) %}
{{ size_w|round(1) }} x {{ size_h|round(1) }} cm (72dpi)
{% endif %}</dd>
{% if record.getWidth() and record.getHeight() %}
{% set width = record.getWidth() %}
{% set height = record.getHeight() %}
{% if width is not none and height is not none %}
<dt>{{ 'Size' | trans }}</dt>
<dd>{{ width }}&nbsp;x&nbsp;{{ height }}
{% if record.getType() == 'image' %}
{% set size_w = width * 2.54 %}
{% set size_h = height * 2.54 %}
<br/>
{{ (size_w/300)|round(1) }}&nbsp;x&nbsp;{{ (size_h/300)|round(1) }}&nbsp;cm&nbsp;(300&nbsp;dpi)
<br/>
{{ (size_w/72)|round(1) }}&nbsp;x&nbsp;{{ (size_h/72)|round(1) }}&nbsp;cm&nbsp;(72&nbsp;dpi)
{% endif %}
</dd>
{% endif %}
{% endif %}
{% endblock %}
</dl>