Fix substitution route

This commit is contained in:
Romain Neutron
2012-04-25 13:53:30 +02:00
parent d5dbe514d7
commit 952d935746

View File

@@ -32,68 +32,68 @@ use DataURI;
class Tools implements ControllerProviderInterface class Tools implements ControllerProviderInterface
{ {
public function connect(Application $app) public function connect(Application $app)
{ {
$controllers = new ControllerCollection(); $controllers = new ControllerCollection();
$controllers->get('/', function(Application $app, Request $request) $controllers->get('/', function(Application $app, Request $request)
{ {
$helper = new Helper\Record\Tools($app['Core'], $request); $helper = new Helper\Record\Tools($app['Core'], $request);
$selection = $helper->get_elements(); $selection = $helper->get_elements();
$binary = $app['Core']->getRegistry()->get('GV_exiftool'); $binary = $app['Core']->getRegistry()->get('GV_exiftool');
$metadatas = false; $metadatas = false;
$record = null; $record = null;
$metadatasFirst = $metadatasSecond = array(); $metadatasFirst = $metadatasSecond = array();
if (count($selection) == 1 && ! empty($binary)) if (count($selection) == 1 && ! empty($binary))
{ {
try try
{ {
$record = reset($selection); $record = reset($selection);
$file = $record->get_subdef('document')->get_pathfile(); $file = $record->get_subdef('document')->get_pathfile();
$cmd = $binary . ' -h ' . escapeshellarg($file); $cmd = $binary . ' -h ' . escapeshellarg($file);
$out = ""; $out = "";
exec($cmd, $out); exec($cmd, $out);
foreach ($out as $liout) foreach ($out as $liout)
{ {
if (strpos($liout, '<tr><td>Directory') === false) if (strpos($liout, '<tr><td>Directory') === false)
$metadatasFirst[] = $liout; $metadatasFirst[] = $liout;
}
$out = "";
$cmd = $binary . ' -X -n -fast ' . escapeshellarg($file) . '';
exec($cmd, $out);
foreach ($out as $liout)
{
$metadatasSecond[] = htmlentities($liout);
}
$metadatas = true;
} }
$out = ""; catch (\Exception $e)
$cmd = $binary . ' -X -n -fast ' . escapeshellarg($file) . '';
exec($cmd, $out);
foreach ($out as $liout)
{ {
$metadatasSecond[] = htmlentities($liout);
} }
$metadatas = true;
}
catch (\Exception $e)
{
}
} }
$template = 'prod/actions/Tools/index.html.twig'; $template = 'prod/actions/Tools/index.html.twig';
$var = array( $var = array(
'helper' => $helper, 'helper' => $helper,
'selection' => $selection, 'selection' => $selection,
'record' => $record, 'record' => $record,
'metadatas' => $metadatas, 'metadatas' => $metadatas,
'metadatasFirst' => $metadatasFirst, 'metadatasFirst' => $metadatasFirst,
'metadatasSecond' => $metadatasSecond 'metadatasSecond' => $metadatasSecond
); );
return new Response($app['Core']->getTwig()->render($template, $var)); return new Response($app['Core']->getTwig()->render($template, $var));
}); });
$controllers->post('/rotate/', function(Application $app, Request $request) $controllers->post('/rotate/', function(Application $app, Request $request)
{ {
$return = array('success' => false, 'errorMessage' => ''); $return = array('success' => false, 'errorMessage' => '');
$helper = new Helper\Record\Tools($app['Core'], $request); $helper = new Helper\Record\Tools($app['Core'], $request);
@@ -103,208 +103,214 @@ class Tools implements ControllerProviderInterface
foreach ($selection as $record) foreach ($selection as $record)
{ {
try try
{ {
$record->rotate_subdefs($rotation); $record->rotate_subdefs($rotation);
$return['success'] = true; $return['success'] = true;
} }
catch (\Exception $e) catch (\Exception $e)
{ {
$return['errorMessage'] = $e->getMessage(); $return['errorMessage'] = $e->getMessage();
} }
} }
$json = $app['Core']->getSerializer()->serialize($return, 'json'); $json = $app['Core']->getSerializer()->serialize($return, 'json');
return new Response($json, 200, array('content-type' => 'application/json')); return new Response($json, 200, array('content-type' => 'application/json'));
}); });
$controllers->post('/image/', function(Application $app, Request $request) $controllers->post('/image/', function(Application $app, Request $request)
{ {
$return = array('success' => true); $return = array('success' => true);
$helper = new Helper\Record\Tools($app['Core'], $request); $helper = new Helper\Record\Tools($app['Core'], $request);
$selection = $helper->get_elements(); $selection = $helper->get_elements();
if ($request->get('ForceThumbSubstit') == '1') if ($request->get('ForceThumbSubstit') == '1')
{ {
foreach ($selection as $record) foreach ($selection as $record)
{
try
{ {
$record->rebuild_subdefs(); try
{
$record->rebuild_subdefs();
}
catch (\Exception $e)
{
$return['success'] = false;
$return['message'] = _('an error occured');
}
} }
catch (\Exception $e)
{
$return['success'] = false;
$return['message'] = _('an error occured');
}
}
} }
$json = $app['Core']->getSerializer()->serialize($return, 'json'); $json = $app['Core']->getSerializer()->serialize($return, 'json');
return new Response($json, 200, array('content-type' => 'application/json')); return new Response($json, 200, array('content-type' => 'application/json'));
}); });
$controllers->post('/hddoc/', function(Application $app, Request $request) $controllers->post('/hddoc/', function(Application $app, Request $request)
{ {
$success = false; $success = false;
$errorMessage = ""; $errorMessage = "";
$fileName = null;
if ($file = $request->files->get('newHD')) if ($file = $request->files->get('newHD'))
{ {
$fileName = $file->getClientOriginalName(); $fileName = $file->getClientOriginalName();
$size = $file->getClientSize(); $size = $file->getClientSize();
if ($size && $fileName && $file->isValid()) if ($size && $fileName && $file->isValid())
{
try
{ {
$record = new \record_adapter(
$request->get('sbas_id')
, $request->get('record_id')
);
$record->substitute_subdef( try
{
$record = new \record_adapter(
$request->get('sbas_id')
, $request->get('record_id')
);
$record->substitute_subdef(
'document' 'document'
, new \system_file($file->getPathname()) , new \system_file($file->getPathname())
); );
if ((int) $request->get('ccfilename') === 1) if ((int) $request->get('ccfilename') === 1)
{ {
$record->set_original_name($fileName); $record->set_original_name($fileName);
} }
$success = true; $success = true;
}
catch (\Exception $e)
{
$errorMessage = $e->getMessage();
}
} }
catch (\Exception $e) else
{ {
$errorMessage = $e->getMessage(); $errorMessage = _('file is not valid');
} }
}
else
{
$errorMessage = _('file is not valid');
}
$template = 'prod/actions/Tools/iframeUpload.html.twig';
$var = array(
'success' => $success
, 'fileName' => $fileName
, 'errorMessage' => $errorMessage
);
return new Response($app['Core']->getTwig()->render($template, $var));
} }
});
$controllers->post('/chgthumb/', function(Application $app, Request $request) $template = 'prod/actions/Tools/iframeUpload.html.twig';
{ $var = array(
$success = false; 'success' => $success
, 'fileName' => $fileName
, 'errorMessage' => $errorMessage
);
return new Response($app['Core']->getTwig()->render($template, $var));
/**
*
*/
});
$controllers->post('/chgthumb/', function(Application $app, Request $request)
{
$success = false;
$errorMessage = ""; $errorMessage = "";
if ($file = $request->files->get('newThumb')) if ($file = $request->files->get('newThumb'))
{ {
$size = $file->getClientSize(); $size = $file->getClientSize();
$fileName = $file->getClientOriginalName(); $fileName = $file->getClientOriginalName();
if ($size && $fileName && $file->isValid()) if ($size && $fileName && $file->isValid())
{
try
{ {
$rootPath = $app['Core']->getRegistry()->get('GV_RootPath'); try
$tmpFile = $rootPath . 'tmp/' . $fileName; {
rename($file->getPathname(), $tmpFile); $rootPath = $app['Core']->getRegistry()->get('GV_RootPath');
$tmpFile = $rootPath . 'tmp/' . $fileName;
rename($file->getPathname(), $tmpFile);
$record = new \record_adapter( $record = new \record_adapter(
$request->get('sbas_id') $request->get('sbas_id')
, $request->get('record_id') , $request->get('record_id')
); );
$record->substitute_subdef( $record->substitute_subdef(
'thumbnail' 'thumbnail'
, new \system_file($tmpFile) , new \system_file($tmpFile)
); );
$success = true; $success = true;
}
catch (\Exception $e)
{
$errorMessage = $e->getMessage();
}
} }
catch (\Exception $e) else
{ {
$errorMessage = $e->getMessage(); $errorMessage = _('file is not valid');
} }
}
else
{
$errorMessage = _('file is not valid');
}
$template = 'prod/actions/Tools/iframeUpload.html.twig'; $template = 'prod/actions/Tools/iframeUpload.html.twig';
$var = array( $var = array(
'success' => $success 'success' => $success
, 'fileName' => $fileName , 'fileName' => $fileName
, 'errorMessage' => $errorMessage , 'errorMessage' => $errorMessage
); );
return new Response($app['Core']->getTwig()->render($template, $var)); return new Response($app['Core']->getTwig()->render($template, $var));
} }
}); });
$controllers->post('/thumb-extractor/confirm-box/', function(Application $app, Request $request) $controllers->post('/thumb-extractor/confirm-box/', function(Application $app, Request $request)
{ {
$return = array('error' => false, 'datas' =>''); $return = array('error' => false, 'datas' => '');
$template = 'prod/actions/Tools/confirm.html.twig'; $template = 'prod/actions/Tools/confirm.html.twig';
try try
{ {
$record = new \record_adapter($request->get('sbas_id'), $request->get('record_id')); $record = new \record_adapter($request->get('sbas_id'), $request->get('record_id'));
$var = array( $var = array(
'video_title' => $record->get_title() 'video_title' => $record->get_title()
, 'image' => $request->get('image', '') , 'image' => $request->get('image', '')
); );
$return['datas'] = $app['Core']->getTwig()->render($template, $var); $return['datas'] = $app['Core']->getTwig()->render($template, $var);
} }
catch (\Exception $e) catch (\Exception $e)
{ {
$return['datas'] = _('an error occured'); $return['datas'] = _('an error occured');
$return['error'] = true; $return['error'] = true;
} }
$json = $app['Core']->getSerializer()->serialize($return, 'json'); $json = $app['Core']->getSerializer()->serialize($return, 'json');
return new Response($json, 201, array('content-type' => 'application/json')); return new Response($json, 201, array('content-type' => 'application/json'));
}); });
$controllers->post('/thumb-extractor/apply/', function(Application $app, Request $request) $controllers->post('/thumb-extractor/apply/', function(Application $app, Request $request)
{ {
$return = array('success' => false, 'message' => ''); $return = array('success' => false, 'message' => '');
try try
{ {
$record = new \record_adapter($request->get('sbas_id'), $request->get('record_id')); $record = new \record_adapter($request->get('sbas_id'), $request->get('record_id'));
$dataUri = DataURI\Parser::parse($request->get('image', '')); $dataUri = DataURI\Parser::parse($request->get('image', ''));
$path = $app['Core']->getRegistry()->get('GV_RootPath') . 'tmp'; $path = $app['Core']->getRegistry()->get('GV_RootPath') . 'tmp';
$name = sprintf('extractor_thumb_%s', $record->get_serialize_key()); $name = sprintf('extractor_thumb_%s', $record->get_serialize_key());
$fileName = sprintf('%s/%s.png', $path, $name); $fileName = sprintf('%s/%s.png', $path, $name);
file_put_contents($fileName, $dataUri->getData()); file_put_contents($fileName, $dataUri->getData());
$record->substitute_subdef('thumbnail', new \system_file($fileName)); $record->substitute_subdef('thumbnail', new \system_file($fileName));
$return['success'] = true; $return['success'] = true;
} }
catch (\Exception $e) catch (\Exception $e)
{ {
$return['message'] = $e->getMessage(); $return['message'] = $e->getMessage();
} }
$json = $app['Core']->getSerializer()->serialize($return, 'json'); $json = $app['Core']->getSerializer()->serialize($return, 'json');
return new Response($json, 201, array('content-type' => 'application/json')); return new Response($json, 201, array('content-type' => 'application/json'));
}); });
return $controllers; return $controllers;
} }
} }