Add subdef generator service

This commit is contained in:
Romain Neutron
2013-12-20 16:55:01 +01:00
parent bb9eff5935
commit 0858586ceb
11 changed files with 255 additions and 177 deletions

View File

@@ -1597,51 +1597,7 @@ class record_adapter implements record_Interface, cache_cacheableInterface
return 'record_' . $this->get_serialize_key() . ($option ? '_' . $option : '');
}
public function generate_subdefs(databox $databox, Application $app, Array $wanted_subdefs = null)
{
$subdefs = $databox->get_subdef_structure()->getSubdefGroup($this->get_type());
$logger = isset($app['task-manager.logger']) ? $app['task-manager.logger'] : $app['monolog'];
if (!$subdefs) {
$logger->addInfo(sprintf('Nothing to do for %s', $this->get_type()));
return;
}
foreach ($subdefs as $subdef) {
$subdefname = $subdef->get_name();
if ($wanted_subdefs && !in_array($subdefname, $wanted_subdefs)) {
continue;
}
$pathdest = null;
if ($this->has_subdef($subdefname) && $this->get_subdef($subdefname)->is_physically_present()) {
$pathdest = $this->get_subdef($subdefname)->get_pathfile();
$this->get_subdef($subdefname)->remove_file();
$logger->addInfo(sprintf('Removed old file for %s', $subdefname));
$this->clearSubdefCache($subdefname);
}
$pathdest = $this->generateSubdefPathname($subdef, $app['filesystem'], $pathdest);
$logger->addInfo(sprintf('Generating subdef %s to %s', $subdefname, $pathdest));
$this->generate_subdef($app['media-alchemyst'], $subdef, $pathdest, $logger);
if (file_exists($pathdest)) {
$media = $app['mediavorus']->guess($pathdest);
media_subdef::create($app, $this, $subdef->get_name(), $media);
}
$this->clearSubdefCache($subdefname);
}
return $this;
}
protected function clearSubdefCache($subdefname)
public function clearSubdefCache($subdefname)
{
if ($this->has_subdef($subdefname)) {
$this->get_subdef($subdefname)->delete_data_from_cache();
@@ -1656,130 +1612,6 @@ class record_adapter implements record_Interface, cache_cacheableInterface
$this->delete_data_from_cache(self::CACHE_SUBDEFS);
}
/**
* Generate a subdef
*
* @param databox_subdef $subdef_class The related databox subdef
* @param type $pathdest The destination of the file
* @param Logger $logger A logger for binary operation
* @return \record_adapter
*/
protected function generate_subdef(Alchemyst $alchemyst, databox_subdef $subdef_class, $pathdest, Logger $logger)
{
try {
if (null === $this->get_hd_file()) {
$logger->addInfo('No HD file found, aborting');
return;
}
$alchemyst->turnInto($this->get_hd_file()->getPathname(), $pathdest, $subdef_class->getSpecs());
} catch (\MediaAlchemyst\Exception\ExceptionInterface $e) {
$logger->addError(sprintf('Subdef generation failed for record %d with message %s', $this->get_record_id(), $e->getMessage()));
}
return $this;
}
/**
* Generate a subdef pathname depending the databox_subdef and the previous file(if regenerated)
*
* @param databox_subdef $subdef
* @param type $oldVersion
* @return type
*/
protected function generateSubdefPathname(databox_subdef $subdef, Filesystem $filesystem, $oldVersion = null)
{
if ($oldVersion) {
$pathdest = p4string::addEndSlash(pathinfo($oldVersion, PATHINFO_DIRNAME));
} else {
$pathdest = databox::dispatch($filesystem, $subdef->get_path());
}
return $pathdest . $this->get_record_id() . '_' . $subdef->get_name() . '.' . $this->getExtensionFromSpec($subdef->getSpecs());
}
/**
* Get the extension from MediaAlchemyst specs
*
* @param Specification $spec
* @return string
*/
protected function getExtensionFromSpec(SpecificationInterface $spec)
{
$extension = null;
switch (true) {
case $spec->getType() === SpecificationInterface::TYPE_IMAGE:
$extension = 'jpg';
break;
case $spec->getType() === SpecificationInterface::TYPE_ANIMATION:
$extension = 'gif';
break;
case $spec->getType() === SpecificationInterface::TYPE_AUDIO:
$extension = $this->getExtensionFromAudioCodec($spec->getAudioCodec());
break;
case $spec->getType() === SpecificationInterface::TYPE_VIDEO:
$extension = $this->getExtensionFromVideoCodec($spec->getVideoCodec());
break;
case $spec->getType() === SpecificationInterface::TYPE_SWF:
$extension = 'swf';
break;
}
return $extension;
}
/**
* Get the extension from audiocodec
*
* @param string $audioCodec
* @return string
*/
protected function getExtensionFromAudioCodec($audioCodec)
{
$extension = null;
switch ($audioCodec) {
case 'flac':
$extension = 'flac';
break;
case 'libvorbis':
$extension = 'ogg';
break;
case 'libmp3lame':
$extension = 'mp3';
break;
}
return $extension;
}
/**
* Get the extension from videocodec
*
* @param string $videoCodec
* @return string
*/
protected function getExtensionFromVideoCodec($videoCodec)
{
$extension = null;
switch ($videoCodec) {
case 'libtheora':
$extension = 'ogv';
break;
case 'libvpx':
$extension = 'webm';
break;
case 'libx264':
$extension = 'mp4';
break;
}
return $extension;
}
/**
*
* @param string $option optionnal cache name