In place refactoring of media_subdef

This commit is contained in:
Benoît Burnichon
2016-04-05 17:27:39 +02:00
parent b38afcd5d6
commit aeac1a7141

View File

@@ -115,102 +115,102 @@ class media_subdef extends media_abstract implements cache_cacheableInterface
$this->record = $record; $this->record = $record;
$this->load($substitute); $this->load($substitute);
$this->generate_url(); parent::__construct($this->width, $this->height, $this->generateUrl());
parent::__construct($this->width, $this->height, $this->url);
} }
/** /**
* @param bool $substitute * @param bool $substitute
* @return $this * @return void
*/ */
protected function load($substitute) protected function load($substitute)
{ {
try { try {
$datas = $this->get_data_from_cache(); $data = $this->get_data_from_cache();
if (!is_array($datas)) { } catch (Exception $e) {
throw new \Exception('Could not retrieve data'); $data = false;
}
$this->mime = $datas['mime'];
$this->width = $datas['width'];
$this->height = $datas['height'];
$this->size = $datas['size'];
$this->etag = $datas['etag'];
$this->path = $datas['path'];
$this->url = $datas['url'];
$this->file = $datas['file'];
$this->is_physically_present = $datas['physically_present'];
$this->is_substituted = $datas['is_substituted'];
$this->subdef_id = $datas['subdef_id'];
$this->modification_date = $datas['modification_date'];
$this->creation_date = $datas['creation_date'];
return $this;
} catch (\Exception $e) {
} }
$connbas = $this->record->getDatabox()->get_connection(); if (is_array($data)) {
$this->loadFromArray($data);
$sql = "SELECT subdef_id, name, file, width, height, mime," return;
. " path, size, substit, created_on, updated_on, etag" }
. " FROM subdef"
. " WHERE name = :name AND record_id = :record_id";
$params = [ $sql = <<<'SQL'
SELECT subdef_id, name, file, width, height, mime, path, size, substit, created_on, updated_on, etag
FROM subdef
WHERE name = :name AND record_id = :record_id
SQL;
$row = $this->getDataboxConnection()->fetchAssoc($sql, [
':record_id' => $this->record->getRecordId(), ':record_id' => $this->record->getRecordId(),
':name' => $this->name ':name' => $this->name
]; ]);
$stmt = $connbas->prepare($sql);
$stmt->execute($params);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$stmt->closeCursor();
if ($row) { if ($row) {
$this->width = (int) $row['width']; $this->loadFromArray([
$this->size = (int) $row['size']; 'width' => (int)$row['width'],
$this->height = (int) $row['height']; 'size' => (int)$row['size'],
$this->mime = $row['mime']; 'height' => (int)$row['height'],
$this->file = $row['file']; 'mime' => $row['mime'],
$this->path = p4string::addEndSlash($row['path']); 'file' => $row['file'],
$this->is_physically_present = file_exists($this->getRealPath()); 'path' => p4string::addEndSlash($row['path']),
$this->etag = $row['etag']; 'physically_present' => true,
$this->is_substituted = ! ! $row['substit']; 'is_substituted' => (bool)$row['substit'],
$this->subdef_id = (int) $row['subdef_id']; 'subdef_id' => (int)$row['subdef_id'],
'updated_on' => $row['updated_on'],
if ($row['updated_on']) 'created_on' => $row['created_on'],
$this->modification_date = new DateTime($row['updated_on']); 'etag' => $row['etag'] ?: null,
if ($row['created_on']) ]);
$this->creation_date = new DateTime($row['created_on']);
} elseif ($substitute === false) { } elseif ($substitute === false) {
throw new Exception_Media_SubdefNotFound($this->name . ' not found'); throw new Exception_Media_SubdefNotFound($this->name . ' not found');
} }
if (! $row) { if (! $row) {
$this->find_substitute_file(); $this->markPhysicallyUnavailable();
} }
$datas = [ $this->set_data_to_cache($this->toArray());
'mime' => $this->mime, }
'width' => $this->width,
'size' => $this->size, private function loadFromArray(array $data)
'height' => $this->height, {
'etag' => $this->etag, $this->mime = $data['mime'];
'path' => $this->path, $this->width = $data['width'];
'url' => $this->url, $this->height = $data['height'];
'file' => $this->file, $this->size = $data['size'];
$this->etag = $data['etag'];
$this->path = $data['path'];
$this->file = $data['file'];
$this->is_physically_present = $data['physically_present'];
$this->is_substituted = $data['is_substituted'];
$this->subdef_id = $data['subdef_id'];
$this->modification_date = isset($data['updated_on']) ? new DateTime($data['updated_on']) : null;
$this->creation_date = isset($data['created_on']) ? new DateTime($data['created_on']) : null;
$this->url = isset($data['url']) ? Url::factory((string)$data['url']) : $this->generateUrl();
if ($this->is_physically_present && !file_exists($this->getRealPath())) {
$this->markPhysicallyUnavailable();
}
}
private function toArray()
{
return [
'width' => $this->width,
'size' => $this->size,
'height' => $this->height,
'mime' => $this->mime,
'file' => $this->file,
'path' => $this->path,
'physically_present' => $this->is_physically_present, 'physically_present' => $this->is_physically_present,
'is_substituted' => $this->is_substituted, 'is_substituted' => $this->is_substituted,
'subdef_id' => $this->subdef_id, 'subdef_id' => $this->subdef_id,
'modification_date' => $this->modification_date, 'updated_on' => $this->modification_date ? $this->modification_date->format(DATE_ATOM) : '',
'creation_date' => $this->creation_date, 'created_on' => $this->creation_date ? $this->creation_date->format(DATE_ATOM) : '',
'etag' => $this->etag,
'url' => (string)$this->url,
]; ];
$this->set_data_to_cache($datas);
return $this;
} }
/** /**
@@ -225,11 +225,13 @@ class media_subdef extends media_abstract implements cache_cacheableInterface
$this->delete_data_from_cache(); $this->delete_data_from_cache();
if ($this->get_permalink() instanceof media_Permalink_Adapter) { $permalink = $this->get_permalink();
$this->get_permalink()->delete_data_from_cache();
if ($permalink instanceof media_Permalink_Adapter) {
$permalink->delete_data_from_cache();
} }
$this->find_substitute_file(); $this->markPhysicallyUnavailable();
} }
return $this; return $this;
@@ -245,57 +247,55 @@ class media_subdef extends media_abstract implements cache_cacheableInterface
$subdef_id = $this->subdef_id; $subdef_id = $this->subdef_id;
$this->remove_file(); $this->remove_file();
$connbas = $this->record->getDatabox()->get_connection(); $connection = $this->getDataboxConnection();
$sql = "DELETE FROM subdef WHERE subdef_id = :subdef_id"; $connection->executeUpdate(
$stmt = $connbas->prepare($sql); 'DELETE FROM subdef WHERE subdef_id = :subdef_id',
$stmt->execute(['subdef_id'=>$subdef_id]); ['subdef_id' => $subdef_id]
$stmt->closeCursor(); );
$sql = "DELETE FROM permalinks WHERE subdef_id = :subdef_id"; $connection->executeUpdate(
$stmt = $connbas->prepare($sql); 'DELETE FROM permalinks WHERE subdef_id = :subdef_id',
$stmt->execute(['subdef_id'=>$subdef_id]); ['subdef_id'=>$subdef_id]
$stmt->closeCursor(); );
$this->delete_data_from_cache(); $this->delete_data_from_cache();
$this->record->delete_data_from_cache(record_adapter::CACHE_SUBDEFS); $this->record->delete_data_from_cache(record_adapter::CACHE_SUBDEFS);
} }
/** private function getSubstituteFilename()
* Find a substitution file for a subdef
*
* @return \media_subdef
*/
protected function find_substitute_file()
{ {
if ($this->record->isStory()) { if ($this->record->isStory()) {
$this->mime = 'image/png'; return 'regroup_thumb.png';
$this->width = 256;
$this->height = 256;
$this->path = $this->app['root.path'] . '/www/assets/common/images/icons/substitution/';
$this->file = 'regroup_thumb.png';
$this->url = Url::factory('/assets/common/images/icons/substitution/regroup_thumb.png');
} else {
$mime = $this->record->getMimeType();
$mime = trim($mime) != '' ? str_replace('/', '_', $mime) : 'application_octet-stream';
$this->mime = 'image/png';
$this->width = 256;
$this->height = 256;
$this->path = $this->app['root.path'] . '/www/assets/common/images/icons/substitution/';
$this->file = str_replace('+', '%20', $mime) . '.png';
$this->url = Url::factory('/assets/common/images/icons/substitution/' . $this->file);
} }
$mime = $this->record->getMimeType();
$mime = trim($mime) != '' ? str_replace('/', '_', $mime) : 'application_octet-stream';
return str_replace('+', '%20', $mime) . '.png';
}
/**
* Find a substitution file for a subdef
* @return void
*/
protected function markPhysicallyUnavailable()
{
$this->is_physically_present = false; $this->is_physically_present = false;
if ( ! file_exists($this->path . $this->file)) { $this->mime = 'image/png';
$this->width = 256;
$this->height = 256;
$this->file = $this->getSubstituteFilename();
$this->path = $this->app['root.path'] . '/www/assets/common/images/icons/substitution/';
$this->url = Url::factory('/assets/common/images/icons/substitution/' . $this->file);
if (!file_exists($this->getRealPath())) {
$this->path = $this->app['root.path'] . '/www/assets/common/images/icons/'; $this->path = $this->app['root.path'] . '/www/assets/common/images/icons/';
$this->file = 'substitution.png'; $this->file = 'substitution.png';
$this->url = Url::factory('/assets/common/images/icons/' . $this->file); $this->url = Url::factory('/assets/common/images/icons/' . $this->file);
} }
return $this;
} }
/** /**
@@ -319,8 +319,9 @@ class media_subdef extends media_abstract implements cache_cacheableInterface
*/ */
public function get_permalink() public function get_permalink()
{ {
if ( ! $this->permalink && $this->is_physically_present()) if (null === $this->permalink && $this->is_physically_present()) {
$this->permalink = media_Permalink_Adapter::getPermalink($this->app, $this->record->getDatabox(), $this); $this->permalink = media_Permalink_Adapter::getPermalink($this->app, $this->record->getDatabox(), $this);
}
return $this->permalink; return $this->permalink;
} }
@@ -337,8 +338,9 @@ class media_subdef extends media_abstract implements cache_cacheableInterface
{ {
if (!$this->etag && $this->is_physically_present()) { if (!$this->etag && $this->is_physically_present()) {
$file = new SplFileInfo($this->getRealPath()); $file = new SplFileInfo($this->getRealPath());
if ($file->isFile()) { if ($file->isFile()) {
$this->setEtag(md5($file->getMTime())); $this->setEtag(md5($file->getRealPath() . $file->getMTime()));
} }
} }
@@ -349,10 +351,10 @@ class media_subdef extends media_abstract implements cache_cacheableInterface
{ {
$this->etag = $etag; $this->etag = $etag;
$sql = "UPDATE subdef SET etag = :etag WHERE subdef_id = :subdef_id"; $this->getDataboxConnection()->executeUpdate(
$stmt = $this->record->getDatabox()->get_connection()->prepare($sql); 'UPDATE subdef SET etag = :etag WHERE subdef_id = :subdef_id',
$stmt->execute([':subdef_id' => $this->subdef_id, ':etag' => $etag]); [':subdef_id' => $this->subdef_id, ':etag' => $etag]
$stmt->closeCursor(); );
return $this; return $this;
} }
@@ -361,13 +363,13 @@ class media_subdef extends media_abstract implements cache_cacheableInterface
{ {
$this->is_substituted = !!$substit; $this->is_substituted = !!$substit;
$sql = "UPDATE subdef SET substit = :substit, updated_on=NOW() WHERE subdef_id = :subdef_id"; $this->getDataboxConnection()->executeUpdate(
$stmt = $this->record->getDatabox()->get_connection()->prepare($sql); 'UPDATE subdef SET substit = :substit, updated_on=NOW() WHERE subdef_id = :subdef_id',
$stmt->execute(array( [
':subdef_id' => $this->subdef_id, ':subdef_id' => $this->subdef_id,
':substit' => $this->is_substituted ':substit' => $this->is_substituted
)); ]
$stmt->closeCursor(); );
$this->delete_data_from_cache(); $this->delete_data_from_cache();
@@ -387,31 +389,22 @@ class media_subdef extends media_abstract implements cache_cacheableInterface
*/ */
public function get_type() public function get_type()
{ {
switch ($this->mime) { static $types = [
case 'video/mp4': 'application/x-shockwave-flash' => self::TYPE_FLEXPAPER,
$type = self::TYPE_VIDEO_MP4; 'audio/mp3' => self::TYPE_AUDIO_MP3,
break; 'audio/mpeg' => self::TYPE_AUDIO_MP3,
case 'video/x-flv': 'image/gif' => self::TYPE_IMAGE,
$type = self::TYPE_VIDEO_FLV; 'image/jpeg' => self::TYPE_IMAGE,
break; 'image/png' => self::TYPE_IMAGE,
case 'application/x-shockwave-flash': 'video/mp4' => self::TYPE_VIDEO_MP4,
$type = self::TYPE_FLEXPAPER; 'video/x-flv' => self::TYPE_VIDEO_FLV,
break; ];
case 'audio/mpeg':
case 'audio/mp3': if (isset($types[$this->mime])) {
$type = self::TYPE_AUDIO_MP3; return $types[$this->mime];
break;
case 'image/jpeg':
case 'image/png':
case 'image/gif':
$type = self::TYPE_IMAGE;
break;
default:
$type = self::TYPE_NO_PLAYER;
break;
} }
return $type; return self::TYPE_NO_PLAYER;
} }
/** /**
@@ -496,11 +489,11 @@ class media_subdef extends media_abstract implements cache_cacheableInterface
} }
/** /**
* @return string * @return Url
*/ */
public function renew_url() public function renew_url()
{ {
$this->generate_url(); $this->url = $this->generateUrl();
return $this->get_url(); return $this->get_url();
} }
@@ -520,7 +513,7 @@ class media_subdef extends media_abstract implements cache_cacheableInterface
public function getDevices() public function getDevices()
{ {
if ($this->get_name() == 'document') { if ($this->get_name() === 'document') {
return [\databox_subdef::DEVICE_ALL]; return [\databox_subdef::DEVICE_ALL];
} }
@@ -559,27 +552,26 @@ class media_subdef extends media_abstract implements cache_cacheableInterface
$media = $mediavorus->guess($this->getRealPath()); $media = $mediavorus->guess($this->getRealPath());
$sql = "UPDATE subdef SET height = :height , width = :width, updated_on = NOW()" $sql = <<<'SQL'
. " WHERE record_id = :record_id AND name = :name"; UPDATE subdef SET height = :height , width = :width, updated_on = NOW()
WHERE record_id = :record_id AND name = :name
SQL;
$params = [ $this->getDataboxConnection()->executeUpdate(
':width' => $media->getWidth(), $sql,
':height' => $media->getHeight(), [
':record_id' => $this->get_record_id(), ':width' => $media->getWidth(),
':name' => $this->get_name(), ':height' => $media->getHeight(),
]; ':record_id' => $this->get_record_id(),
':name' => $this->get_name(),
$stmt = $this->record->getDatabox()->get_connection()->prepare($sql); ]
$stmt->execute($params); );
$stmt->closeCursor();
$this->width = $media->getWidth(); $this->width = $media->getWidth();
$this->height = $media->getHeight(); $this->height = $media->getHeight();
$this->delete_data_from_cache(); $this->delete_data_from_cache();
unset($media);
return $this; return $this;
} }
@@ -646,7 +638,7 @@ class media_subdef extends media_abstract implements cache_cacheableInterface
public static function create(Application $app, \record_adapter $record, $name, MediaInterface $media) public static function create(Application $app, \record_adapter $record, $name, MediaInterface $media)
{ {
$databox = $record->getDatabox(); $databox = $record->getDatabox();
$connbas = $databox->get_connection(); $connection = $databox->get_connection();
$path = $media->getFile()->getPath(); $path = $media->getFile()->getPath();
$newname = $media->getFile()->getFilename(); $newname = $media->getFile()->getFilename();
@@ -670,23 +662,23 @@ class media_subdef extends media_abstract implements cache_cacheableInterface
$params[':height'] = $media->getHeight(); $params[':height'] = $media->getHeight();
} }
$sql = "INSERT INTO subdef" $sql = <<<'SQL'
. " (record_id, name, path, file, width, height, mime, size, dispatched, created_on, updated_on)" INSERT INTO subdef (record_id, name, path, file, width, height, mime, size, dispatched, created_on, updated_on)
. " VALUES (:record_id, :name, :path, :file, :width, :height, :mime, :size, :dispatched, NOW(), NOW())" VALUES (:record_id, :name, :path, :file, :width, :height, :mime, :size, :dispatched, NOW(), NOW())
. " ON DUPLICATE KEY UPDATE" ON DUPLICATE KEY UPDATE
. " path = VALUES(path), file = VALUES(file)," path = VALUES(path), file = VALUES(file), width = VALUES(width) , height = VALUES(height), mime = VALUES(mime),
. " width = VALUES(width) , height = VALUES(height), mime = VALUES(mime)," size = VALUES(size), dispatched = VALUES(dispatched), updated_on = NOW()
. " size = VALUES(size), dispatched = VALUES(dispatched), updated_on = NOW()"; SQL;
$stmt = $connbas->prepare($sql); $connection->executeUpdate($sql, $params);
$stmt->execute($params);
$stmt->closeCursor();
$subdef = new self($app, $record, $name); $subdef = new self($app, $record, $name);
$subdef->delete_data_from_cache(); $subdef->delete_data_from_cache();
if ($subdef->get_permalink() instanceof media_Permalink_Adapter) { $permalink = $subdef->get_permalink();
$subdef->get_permalink()->delete_data_from_cache();
if ($permalink instanceof media_Permalink_Adapter) {
$permalink->delete_data_from_cache();
} }
if ($name === 'thumbnail') { if ($name === 'thumbnail') {
@@ -695,43 +687,41 @@ class media_subdef extends media_abstract implements cache_cacheableInterface
$symlinker->symlink($subdef->getRealPath()); $symlinker->symlink($subdef->getRealPath());
} }
unset($media);
return $subdef; return $subdef;
} }
protected function generate_url() /**
* @return Url
*/
protected function generateUrl()
{ {
if (!$this->is_physically_present()) { if (!$this->is_physically_present()) {
return; $this->markPhysicallyUnavailable();
return $this->url;
} }
// serve thumbnails using static file service // serve thumbnails using static file service
if ($this->get_name() === 'thumbnail') { if ($this->get_name() === 'thumbnail') {
if (null !== $url = $this->app['phraseanet.static-file']->getUrl($this->getRealPath())) { if (null !== $url = $this->app['phraseanet.static-file']->getUrl($this->getRealPath())) {
$url->getQuery()->offsetSet('etag', $this->getEtag()); $url->getQuery()->offsetSet('etag', $this->getEtag());
$this->url = $url;
return; return $url;
} }
} }
if ($this->app['phraseanet.h264-factory']->isH264Enabled() && in_array($this->mime, ['video/mp4'])) { if ($this->app['phraseanet.h264-factory']->isH264Enabled() && in_array($this->mime, ['video/mp4'], false)) {
if (null !== $url = $this->app['phraseanet.h264']->getUrl($this->getRealPath())) { if (null !== $url = $this->app['phraseanet.h264']->getUrl($this->getRealPath())) {
$this->url = $url; return $url;
return;
} }
} }
$this->url = Url::factory($this->app->path('datafile', [ return Url::factory($this->app->path('datafile', [
'sbas_id' => $this->record->getDataboxId(), 'sbas_id' => $this->record->getDataboxId(),
'record_id' => $this->record->getRecordId(), 'record_id' => $this->record->getRecordId(),
'subdef' => $this->get_name(), 'subdef' => $this->get_name(),
'etag' => $this->getEtag(), 'etag' => $this->getEtag(),
])); ]));
return;
} }
public function get_cache_key($option = null) public function get_cache_key($option = null)
@@ -785,4 +775,12 @@ class media_subdef extends media_abstract implements cache_cacheableInterface
{ {
return $this->path . 'stamp_' . $this->file; return $this->path . 'stamp_' . $this->file;
} }
/**
* @return \Doctrine\DBAL\Connection
*/
private function getDataboxConnection()
{
return $this->record->getDatabox()->get_connection();
}
} }