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

@@ -102,6 +102,7 @@ use Alchemy\Phrasea\Core\Provider\PluginServiceProvider;
use Alchemy\Phrasea\Core\Provider\PhraseaVersionServiceProvider; use Alchemy\Phrasea\Core\Provider\PhraseaVersionServiceProvider;
use Alchemy\Phrasea\Core\Provider\RegistrationServiceProvider; use Alchemy\Phrasea\Core\Provider\RegistrationServiceProvider;
use Alchemy\Phrasea\Core\Provider\SearchEngineServiceProvider; use Alchemy\Phrasea\Core\Provider\SearchEngineServiceProvider;
use Alchemy\Phrasea\Core\Provider\SubdefServiceProvider;
use Alchemy\Phrasea\Core\Provider\TasksServiceProvider; use Alchemy\Phrasea\Core\Provider\TasksServiceProvider;
use Alchemy\Phrasea\Core\Provider\TemporaryFilesystemServiceProvider; use Alchemy\Phrasea\Core\Provider\TemporaryFilesystemServiceProvider;
use Alchemy\Phrasea\Core\Provider\TokensServiceProvider; use Alchemy\Phrasea\Core\Provider\TokensServiceProvider;
@@ -280,6 +281,7 @@ class Application extends SilexApplication
$this->register(new PhraseaVersionServiceProvider()); $this->register(new PhraseaVersionServiceProvider());
$this->register(new PHPExiftoolServiceProvider()); $this->register(new PHPExiftoolServiceProvider());
$this->register(new ReCaptchaServiceProvider()); $this->register(new ReCaptchaServiceProvider());
$this->register(new SubdefServiceProvider());
$this['recaptcha.public-key'] = $this->share(function (Application $app) { $this['recaptcha.public-key'] = $this->share(function (Application $app) {
if ($app['conf']->get(['registry', 'webservices', 'captcha-enabled'])) { if ($app['conf']->get(['registry', 'webservices', 'captcha-enabled'])) {

View File

@@ -79,7 +79,7 @@ class BuildMissingSubdefs extends Command
} }
if ($todo) { if ($todo) {
$record->generate_subdefs($databox, $this->container, [$subdef->get_name()]); $this->container['subdef.generator']->generateSubdefs($record, [$subdef->get_name()]);
$this->container['monolog']->addInfo("generate " . $subdef->get_name() . " for record " . $record->get_record_id()); $this->container['monolog']->addInfo("generate " . $subdef->get_name() . " for record " . $record->get_record_id());
$n ++; $n ++;
} }

View File

@@ -288,7 +288,7 @@ class RegenerateSqliteDb extends Command
foreach (range(1, 7) as $i) { foreach (range(1, 7) as $i) {
$file = new File($this->container, $this->container['mediavorus']->guess(__DIR__ . '/../../../../../tests/files/test001.jpg'), $DI['coll']); $file = new File($this->container, $this->container['mediavorus']->guess(__DIR__ . '/../../../../../tests/files/test001.jpg'), $DI['coll']);
$record = \record_adapter::createFromFile($file, $this->container); $record = \record_adapter::createFromFile($file, $this->container);
$record->generate_subdefs($record->get_databox(), $this->container); $this->container['subdef.generator']->generateSubdefs($record);
$DI['record_' . $i] = $record; $DI['record_' . $i] = $record;
} }

View File

@@ -0,0 +1,35 @@
<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2013 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alchemy\Phrasea\Core\Provider;
use Alchemy\Phrasea\Application;
use Silex\Application as SilexApplication;
use Silex\ServiceProviderInterface;
use Alchemy\Phrasea\Media\SubdefGenerator;
use Alchemy\Phrasea\Media\SubdefGeneratorMock;
class SubdefServiceProvider implements ServiceProviderInterface
{
public function register(SilexApplication $app)
{
$app['subdef.generator'] = $app->share(function (SilexApplication $app) {
return new SubdefGenerator($app, $app['media-alchemyst'], $app['filesystem'], $app['mediavorus'], isset($app['task-manager.logger']) ? $app['task-manager.logger'] : $app['monolog']);
});
}
/**
* {@inheritDoc}
*/
public function boot(SilexApplication $app)
{
}
}

View File

@@ -0,0 +1,191 @@
<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2013 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alchemy\Phrasea\Media;
use Alchemy\Phrasea\Application;
use MediaAlchemyst\Alchemyst;
use MediaAlchemyst\Specification\SpecificationInterface;
use MediaVorus\MediaVorus;
use MediaAlchemyst\Exception\ExceptionInterface as MediaAlchemystException;
use Psr\Log\LoggerInterface;
use Symfony\Component\Filesystem\Filesystem;
class SubdefGenerator
{
private $app;
private $alchemyst;
private $filesystem;
private $logger;
private $mediavorus;
public function __construct(Application $app, Alchemyst $alchemyst, Filesystem $filesystem, MediaVorus $mediavorus, LoggerInterface $logger)
{
$this->app = $app;
$this->alchemyst = $alchemyst;
$this->filesystem = $filesystem;
$this->logger = $logger;
$this->mediavorus = $mediavorus;
}
public function generateSubdefs(\record_adapter $record, array $wanted_subdefs = null)
{
if (null === $subdefs = $record->get_databox()->get_subdef_structure()->getSubdefGroup($record->get_type())) {
$this->logger->info(sprintf('Nothing to do for %s', $record->get_type()));
return;
}
foreach ($subdefs as $subdef) {
$subdefname = $subdef->get_name();
if ($wanted_subdefs && !in_array($subdefname, $wanted_subdefs)) {
continue;
}
$pathdest = null;
if ($record->has_subdef($subdefname) && $record->get_subdef($subdefname)->is_physically_present()) {
$pathdest = $record->get_subdef($subdefname)->get_pathfile();
$record->get_subdef($subdefname)->remove_file();
$this->logger->info(sprintf('Removed old file for %s', $subdefname));
$record->clearSubdefCache($subdefname);
}
$pathdest = $this->generateSubdefPathname($record, $subdef, $pathdest);
$this->logger->addInfo(sprintf('Generating subdef %s to %s', $subdefname, $pathdest));
$this->generateSubdef($record, $subdef, $pathdest);
if ($this->filesystem->exists($pathdest)) {
$media = $this->mediavorus->guess($pathdest);
\media_subdef::create($this->app, $record, $subdef->get_name(), $media);
}
$record->clearSubdefCache($subdefname);
}
return $this;
}
private function generateSubdef(\record_adapter $record, \databox_subdef $subdef_class, $pathdest)
{
try {
if (null === $record->get_hd_file()) {
$this->logger->addInfo('No HD file found, aborting');
return;
}
$this->alchemyst->turnInto($record->get_hd_file()->getPathname(), $pathdest, $subdef_class->getSpecs());
} catch (MediaAlchemystException $e) {
$this->logger->error(sprintf('Subdef generation failed for record %d with message %s', $record->get_record_id(), $e->getMessage()));
}
}
private function generateSubdefPathname(\record_adapter $record, \databox_subdef $subdef, $oldVersion = null)
{
if ($oldVersion) {
$pathdest = \p4string::addEndSlash(pathinfo($oldVersion, PATHINFO_DIRNAME));
} else {
$pathdest = \databox::dispatch($this->filesystem, $subdef->get_path());
}
return $pathdest . $record->get_record_id() . '_' . $subdef->get_name() . '.' . $this->getExtensionFromSpec($subdef->getSpecs());
}
/**
* Get the extension from MediaAlchemyst specs
*
* @param SpecificationInterface $spec
*
* @return string
*/
private 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
*/
private 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
*/
private function getExtensionFromVideoCodec($videoCodec)
{
$extension = null;
switch ($videoCodec) {
case 'libtheora':
$extension = 'ogv';
break;
case 'libvpx':
$extension = 'webm';
break;
case 'libx264':
$extension = 'mp4';
break;
}
return $extension;
}
}

View File

@@ -82,7 +82,7 @@ class SubdefsJob extends AbstractJob
try { try {
$record = $databox->get_record($row['record_id']); $record = $databox->get_record($row['record_id']);
$record->generate_subdefs($databox, $app); $app['subdef.generator']->generateSubdefs($record);
} catch (\Exception $e) { } catch (\Exception $e) {
$this->log('warning', sprintf("Generate subdefs failed for : sbasid=%s / databox=%s / recordid=%s : %s", $databox->get_sbas_id(), $databox->get_dbname() , $row['record_id'], $e->getMessage())); $this->log('warning', sprintf("Generate subdefs failed for : sbasid=%s / databox=%s / recordid=%s : %s", $databox->get_sbas_id(), $databox->get_dbname() , $row['record_id'], $e->getMessage()));
} }

View File

@@ -97,8 +97,6 @@ interface record_Interface
public function delete(); public function delete();
public function generate_subdefs(databox $databox, Application $app, Array $wanted_subdefs = null);
public function log_view($log_id, $referrer, $gv_sit); public function log_view($log_id, $referrer, $gv_sit);
public function get_container_baskets(EntityManager $em, User_Adapter $user); public function get_container_baskets(EntityManager $em, User_Adapter $user);

View File

@@ -1597,51 +1597,7 @@ class record_adapter implements record_Interface, cache_cacheableInterface
return 'record_' . $this->get_serialize_key() . ($option ? '_' . $option : ''); return 'record_' . $this->get_serialize_key() . ($option ? '_' . $option : '');
} }
public function generate_subdefs(databox $databox, Application $app, Array $wanted_subdefs = null) public function clearSubdefCache($subdefname)
{
$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)
{ {
if ($this->has_subdef($subdefname)) { if ($this->has_subdef($subdefname)) {
$this->get_subdef($subdefname)->delete_data_from_cache(); $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); $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 * @param string $option optionnal cache name

View File

@@ -0,0 +1,20 @@
<?php
namespace Alchemy\Tests\Phrasea\Core\Provider;
use Alchemy\Phrasea\Core\Provider\TasksServiceProvider;
use Alchemy\Tests\Tools\TranslatorMockTrait;
class SubdefServiceProviderTest extends ServiceProviderTestCase
{
public function provideServiceDescription()
{
return [
[
'Alchemy\Phrasea\Core\Provider\SubdefServiceProvider',
'subdef.generator',
'Alchemy\Phrasea\Media\SubdefGenerator'
],
];
}
}

View File

@@ -181,7 +181,7 @@ abstract class PhraseanetTestCase extends WebTestCase
self::$recordsInitialized[] = $id; self::$recordsInitialized[] = $id;
$file = new File(self::$DI['app'], self::$DI['app']['mediavorus']->guess(__DIR__ . '/../files/cestlafete.jpg'), self::$DI['collection_no_access']); $file = new File(self::$DI['app'], self::$DI['app']['mediavorus']->guess(__DIR__ . '/../files/cestlafete.jpg'), self::$DI['collection_no_access']);
$record = record_adapter::createFromFile($file, self::$DI['app']); $record = record_adapter::createFromFile($file, self::$DI['app']);
$record->generate_subdefs($record->get_databox(), self::$DI['app']); self::$DI['app']['subdef.generator']->generateSubdefs($record);
self::$fixtureIds['records'][$id] = $record->get_record_id(); self::$fixtureIds['records'][$id] = $record->get_record_id();
return self::$fixtureIds['records'][$id]; return self::$fixtureIds['records'][$id];
@@ -197,7 +197,7 @@ abstract class PhraseanetTestCase extends WebTestCase
self::$recordsInitialized[] = $id; self::$recordsInitialized[] = $id;
$file = new File(self::$DI['app'], self::$DI['app']['mediavorus']->guess(__DIR__ . '/../files/cestlafete.jpg'), self::$DI['collection_no_access_by_status']); $file = new File(self::$DI['app'], self::$DI['app']['mediavorus']->guess(__DIR__ . '/../files/cestlafete.jpg'), self::$DI['collection_no_access_by_status']);
$record = record_adapter::createFromFile($file, self::$DI['app']); $record = record_adapter::createFromFile($file, self::$DI['app']);
$record->generate_subdefs($record->get_databox(), self::$DI['app']); self::$DI['app']['subdef.generator']->generateSubdefs($record);
self::$fixtureIds['records'][$id] = $record->get_record_id(); self::$fixtureIds['records'][$id] = $record->get_record_id();
return self::$fixtureIds['records'][$id]; return self::$fixtureIds['records'][$id];

View File

@@ -24,7 +24,7 @@ class media_subdefTest extends \PhraseanetTestCase
$file = new File(self::$DI['app'], self::$DI['app']['mediavorus']->guess(__DIR__ . "/../../files/iphone_pic.jpg"), self::$DI['collection']); $file = new File(self::$DI['app'], self::$DI['app']['mediavorus']->guess(__DIR__ . "/../../files/iphone_pic.jpg"), self::$DI['collection']);
self::$recordonbleu = record_adapter::createFromFile($file, self::$DI['app']); self::$recordonbleu = record_adapter::createFromFile($file, self::$DI['app']);
self::$recordonbleu->generate_subdefs(self::$recordonbleu->get_databox(), self::$DI['app']); self::$DI['app']['subdef.generator']->generateSubdefs(self::$recordonbleu);
foreach (self::$recordonbleu->get_subdefs() as $subdef) { foreach (self::$recordonbleu->get_subdefs() as $subdef) {