fix #777 preview in quarantine

This commit is contained in:
Nicolas Le Goff
2012-07-02 18:45:27 +02:00
parent a6dc85092e
commit 06eecb913f
6 changed files with 161 additions and 95 deletions

View File

@@ -23,6 +23,7 @@ use Monolog\Logger;
use PHPExiftool\Driver\Metadata\Metadata; use PHPExiftool\Driver\Metadata\Metadata;
use PHPExiftool\Driver\Value\Mono as MonoValue; use PHPExiftool\Driver\Value\Mono as MonoValue;
use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Exception\IOException;
use XPDF\PdfToText; use XPDF\PdfToText;
/** /**
@@ -204,22 +205,25 @@ class Manager
* @param string $filename The desired filename * @param string $filename The desired filename
* @return string The available filename to use * @return string The available filename to use
*/ */
protected function bookLazaretPathfile($filename) protected function bookLazaretPathfile($filename, $suffix = '')
{ {
$root = __DIR__ . '/../../../../tmp/lazaret/'; $output = __DIR__ . '/../../../../tmp/lazaret/lzrt_' . substr($filename, 0, 3) . '_' . $suffix . '.' .pathinfo($filename, PATHINFO_EXTENSION);
$infos = pathinfo($output);
$n = 0;
while (true) {
$output = sprintf('%s/%s-%d%s', $infos['dirname'], $infos['filename'], ++ $n, (isset($infos['extension']) ? '.' . $infos['extension'] : ''));
$infos = pathinfo($filename); try {
if ( ! $this->filesystem->exists($output)) {
$this->filesystem->touch($output);
break;
}
} catch (IOException $e) {
$output = $root . $infos['basename']; }
$n = 1;
while (file_exists($output) || ! touch($output)) {
$output = $root . $infos['filename'] . '-' . ++ $n . '.' . $infos['extension'];
} }
$this->filesystem->touch($output); return realpath($output);
return $output;
} }
/** /**
@@ -410,7 +414,8 @@ class Manager
) )
); );
$lazaretPathname = $this->bookLazaretPathfile($file->getFile()->getRealPath()); $lazaretPathname = $this->bookLazaretPathfile($file->getOriginalName());
$lazaretPathnameThumb = $this->bookLazaretPathfile($file->getOriginalName(), 'thumb');
$this->filesystem->copy($file->getFile()->getRealPath(), $lazaretPathname, true); $this->filesystem->copy($file->getFile()->getRealPath(), $lazaretPathname, true);
@@ -422,9 +427,9 @@ class Manager
$core = \bootstrap::getCore(); $core = \bootstrap::getCore();
try { try {
$core['media-alchemyst'] $core['media-alchemyst']
->open($file->getFile()->getPathname()) ->open($file->getFile()->getPathname())
->turnInto($lazaretPathname, $spec) ->turnInto($lazaretPathnameThumb, $spec)
->close(); ->close();
} catch (MediaAlchemystException $e) { } catch (MediaAlchemystException $e) {
@@ -438,7 +443,9 @@ class Manager
$lazaretFile->setForced($forced); $lazaretFile->setForced($forced);
$lazaretFile->setPathname($lazaretPathname); $lazaretFile->setPathname(pathinfo($lazaretPathname, PATHINFO_BASENAME));
$lazaretFile->setThumbPathName(pathinfo($lazaretPathnameThumb, PATHINFO_BASENAME));
$lazaretFile->setSession($session); $lazaretFile->setSession($session);
$this->em->persist($lazaretFile); $this->em->persist($lazaretFile);

View File

@@ -18,6 +18,7 @@ use Silex\ControllerProviderInterface;
use Silex\ControllerCollection; use Silex\ControllerCollection;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Filesystem\Exception\IOException;
/** /**
* Lazaret controller collection * Lazaret controller collection
@@ -189,32 +190,28 @@ class Lazaret implements ControllerProviderInterface
{ {
$ret = array('success' => false, 'message' => '', 'result' => array()); $ret = array('success' => false, 'message' => '', 'result' => array());
try { $lazaretFile = $app['Core']['EM']->find('Entities\LazaretFile', $file_id);
$lazaretFile = $app['Core']['EM']->find('Entities\LazaretFile', $file_id);
/* @var $lazaretFile \Entities\LazaretFile */ /* @var $lazaretFile \Entities\LazaretFile */
if (null === $lazaretFile) { if (null === $lazaretFile) {
throw new \Exception_NotFound(sprintf('Lazaret file id %d not found', $file_id));
}
$file = array(
'filename' => $lazaretFile->getOriginalName(),
'base_id' => $lazaretFile->getBaseId(),
'created' => $lazaretFile->getCreated()->format(\DateTime::ATOM),
'updated' => $lazaretFile->getUpdated()->format(\DateTime::ATOM),
'pathname' => $lazaretFile->getPathname(),
'sha256' => $lazaretFile->getSha256(),
'uuid' => $lazaretFile->getUuid(),
);
$ret['result'] = $file;
$ret['success'] = true;
} catch (\Exception_NotFound $e) {
$ret['message'] = _('File is not present in quarantine anymore, please refresh'); $ret['message'] = _('File is not present in quarantine anymore, please refresh');
} catch (\Exception $e) {
$ret['message'] = _('An error occured'); return self::formatJson($app['Core']['Serializer'], $ret);
} }
$file = array(
'filename' => $lazaretFile->getOriginalName(),
'base_id' => $lazaretFile->getBaseId(),
'created' => $lazaretFile->getCreated()->format(\DateTime::ATOM),
'updated' => $lazaretFile->getUpdated()->format(\DateTime::ATOM),
'pathname' => $lazaretFile->getPathname(),
'sha256' => $lazaretFile->getSha256(),
'uuid' => $lazaretFile->getUuid(),
);
$ret['result'] = $file;
$ret['success'] = true;
return self::formatJson($app['Core']['Serializer'], $ret); return self::formatJson($app['Core']['Serializer'], $ret);
} }
@@ -242,14 +239,17 @@ class Lazaret implements ControllerProviderInterface
return self::formatJson($app['Core']['Serializer'], $ret); return self::formatJson($app['Core']['Serializer'], $ret);
} }
$lazaretFile = $app['Core']['EM']->find('Entities\LazaretFile', $file_id);
/* @var $lazaretFile \Entities\LazaretFile */
if (null === $lazaretFile) {
$ret['message'] = _('File is not present in quarantine anymore, please refresh');
return self::formatJson($app['Core']['Serializer'], $ret);
}
try { try {
$lazaretFile = $app['Core']['EM']->find('Entities\LazaretFile', $file_id);
/* @var $lazaretFile \Entities\LazaretFile */
if (null === $lazaretFile) {
throw new \Exception_NotFound(sprintf('Lazaret file id %d not found', $file_id));
}
$borderFile = Border\File::buildFromPathfile( $borderFile = Border\File::buildFromPathfile(
$lazaretFile->getPathname(), $lazaretFile->getCollection(), $lazaretFile->getOriginalName() $lazaretFile->getPathname(), $lazaretFile->getCollection(), $lazaretFile->getOriginalName()
); );
@@ -311,15 +311,18 @@ class Lazaret implements ControllerProviderInterface
$app['Core']['EM']->remove($lazaretFile); $app['Core']['EM']->remove($lazaretFile);
$app['Core']['EM']->flush(); $app['Core']['EM']->flush();
$app['Core']['file-system']->remove($lazaretFile->getPathname());
$ret['success'] = true; $ret['success'] = true;
} catch (\Exception_NotFound $e) {
$ret['message'] = _('File is not present in quarantine anymore, please refresh');
} catch (\Exception $e) { } catch (\Exception $e) {
$ret['message'] = _('An error occured'); $ret['message'] = _('An error occured');
} }
try {
$app['Core']['file-system']->remove($lazaretFile->getPathname());
$app['Core']['file-system']->remove($lazaretFile->getThumbPathname());
} catch (IOException $e) {
}
return self::formatJson($app['Core']['Serializer'], $ret); return self::formatJson($app['Core']['Serializer'], $ret);
} }
@@ -336,27 +339,32 @@ class Lazaret implements ControllerProviderInterface
{ {
$ret = array('success' => false, 'message' => '', 'result' => array()); $ret = array('success' => false, 'message' => '', 'result' => array());
$lazaretFile = $app['Core']['EM']->find('Entities\LazaretFile', $file_id);
/* @var $lazaretFile \Entities\LazaretFile */
if (null === $lazaretFile) {
$ret['message'] = _('File is not present in quarantine anymore, please refresh');
return self::formatJson($app['Core']['Serializer'], $ret);
}
try { try {
$lazaretFile = $app['Core']['EM']->find('Entities\LazaretFile', $file_id);
/* @var $lazaretFile \Entities\LazaretFile */
if (null === $lazaretFile) {
throw new \Exception_NotFound(sprintf('Lazaret file id %d not found', $file_id));
}
//Delete lazaret file
$app['Core']['EM']->remove($lazaretFile); $app['Core']['EM']->remove($lazaretFile);
$app['Core']['EM']->flush(); $app['Core']['EM']->flush();
$app['Core']['file-system']->remove($lazaretFile->getPathname());
$ret['success'] = true; $ret['success'] = true;
} catch (\Exception_NotFound $e) {
$ret['message'] = _('File is not present in quarantine anymore, please refresh');
} catch (\Exception $e) { } catch (\Exception $e) {
$ret['message'] = _('An error occured'); $ret['message'] = _('An error occured');
} }
try {
$app['Core']['file-system']->remove($lazaretFile->getPathname());
$app['Core']['file-system']->remove($lazaretFile->getThumbPathname());
} catch (IOException $e) {
}
return self::formatJson($app['Core']['Serializer'], $ret); return self::formatJson($app['Core']['Serializer'], $ret);
} }
@@ -380,32 +388,34 @@ class Lazaret implements ControllerProviderInterface
return self::formatJson($app['Core']['Serializer'], $ret); return self::formatJson($app['Core']['Serializer'], $ret);
} }
$lazaretFile = $app['Core']['EM']->find('Entities\LazaretFile', $file_id);
/* @var $lazaretFile \Entities\LazaretFile */
if (null === $lazaretFile) {
$ret['message'] = _('File is not present in quarantine anymore, please refresh');
return self::formatJson($app['Core']['Serializer'], $ret);
}
$found = false;
//Check if the choosen record is eligible to the substitution
foreach ($lazaretFile->getRecordsToSubstitute() as $record) {
if ($record->get_record_id() !== (int) $recordId) {
continue;
}
$found = true;
break;
}
if ( ! $found) {
$ret['message'] = _('The destination record provided is not allowed');
return self::formatJson($app['Core']['Serializer'], $ret);
}
try { try {
$lazaretFile = $app['Core']['EM']->find('Entities\LazaretFile', $file_id);
/* @var $lazaretFile \Entities\LazaretFile */
if (null === $lazaretFile) {
throw new \Exception_NotFound(sprintf('Lazaret file id %d not found', $file_id));
}
$found = false;
//Check if the choosen record is eligible to the substitution
foreach ($lazaretFile->getRecordsToSubstitute() as $record) {
if ($record->get_record_id() !== (int) $recordId) {
continue;
}
$found = true;
break;
}
if ( ! $found) {
$ret['message'] = _('The destination record provided is not allowed');
return self::formatJson($app['Core']['Serializer'], $ret);
}
$media = $app['Core']['mediavorus']->guess(new \SplFileInfo($lazaretFile->getPathname())); $media = $app['Core']['mediavorus']->guess(new \SplFileInfo($lazaretFile->getPathname()));
$record = $lazaretFile->getCollection()->get_databox()->get_record($recordId); $record = $lazaretFile->getCollection()->get_databox()->get_record($recordId);
@@ -415,15 +425,18 @@ class Lazaret implements ControllerProviderInterface
$app['Core']['EM']->remove($lazaretFile); $app['Core']['EM']->remove($lazaretFile);
$app['Core']['EM']->flush(); $app['Core']['EM']->flush();
$app['Core']['file-system']->remove($lazaretFile->getPathname());
$ret['success'] = true; $ret['success'] = true;
} catch (\Exception_NotFound $e) {
$ret['message'] = _('File is not present in quarantine anymore, please refresh');
} catch (\Exception $e) { } catch (\Exception $e) {
$ret['message'] = _('An error occured'); $ret['message'] = _('An error occured');
} }
try {
$app['Core']['file-system']->remove($lazaretFile->getPathname());
$app['Core']['file-system']->remove($lazaretFile->getThumbPathname());
} catch (IOException $e) {
}
return self::formatJson($app['Core']['Serializer'], $ret); return self::formatJson($app['Core']['Serializer'], $ret);
} }
@@ -442,11 +455,14 @@ class Lazaret implements ControllerProviderInterface
/* @var $lazaretFile \Entities\LazaretFile */ /* @var $lazaretFile \Entities\LazaretFile */
if (null === $lazaretFile) { if (null === $lazaretFile) {
return new Response(null, 404); return new Response(null, 404);
} }
$lazaretPath = __DIR__ . '/../../../../../tmp/lazaret/';
$response = \set_export::stream_file( $response = \set_export::stream_file(
$lazaretFile->getPathname(), $lazaretFile->getOriginalName(), 'image/jpeg', 'inline' $lazaretPath . $lazaretFile->getThumbPathname(), $lazaretFile->getOriginalName(), 'image/jpeg', 'inline'
); );
return $response; return $response;

View File

@@ -176,8 +176,9 @@ class Upload implements ControllerProviderInterface
} }
try { try {
// Add file extension, so mediavorus can guess file type for octet-stream file
$uploadedFilename = $file->getRealPath(); $uploadedFilename = $file->getRealPath();
$renamedFilename = $file->getRealPath() . $file->getClientOriginalName(); $renamedFilename = $file->getRealPath() . '.' . pathinfo($file->getClientOriginalName(), PATHINFO_EXTENSION);
$originalname = $file->getClientOriginalName(); $originalname = $file->getClientOriginalName();
$clientMimeType = $file->getClientMimeType(); $clientMimeType = $file->getClientMimeType();

View File

@@ -17,6 +17,11 @@ class LazaretFile
*/ */
private $pathname; private $pathname;
/**
* @var string $thumbPathname
*/
private $thumbPathname;
/** /**
* @var integer $base_id * @var integer $base_id
*/ */
@@ -355,21 +360,33 @@ class LazaretFile
$ret = array(); $ret = array();
$shaRecords = \record_adapter::get_record_by_sha( $shaRecords = \record_adapter::get_record_by_sha(
$this->getCollection()->get_sbas_id(), $this->getSha256() $this->getCollection()->get_sbas_id(), $this->getSha256()
); );
$uuidRecords = \record_adapter::get_record_by_uuid( $uuidRecords = \record_adapter::get_record_by_uuid(
$this->getCollection()->get_databox(), $this->getUuid() $this->getCollection()->get_databox(), $this->getUuid()
); );
$merged = array_merge($uuidRecords, $shaRecords); $merged = array_merge($uuidRecords, $shaRecords);
foreach ($merged as $record) { foreach ($merged as $record) {
if (!in_array($record, $ret)) { if ( ! in_array($record, $ret)) {
$ret[] = $record; $ret[] = $record;
} }
} }
return $ret; return $ret;
} }
public function getThumbPathname()
{
return $this->thumbPathname;
}
public function setThumbPathname($thumbPathname)
{
$this->thumbPathname = $thumbPathname;
}
} }

View File

@@ -9,6 +9,9 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
use MediaAlchemyst\Exception\Exception as MediaAlchemystException;
use MediaAlchemyst\Specification\Image as ImageSpec;
/** /**
* *
* @license http://opensource.org/licenses/gpl-3.0 GPLv3 * @license http://opensource.org/licenses/gpl-3.0 GPLv3
@@ -84,6 +87,24 @@ class patch_370a7 implements patchInterface
$filePath = __DIR__ . '/../../../tmp/lazaret/' . $row['filepath']; $filePath = __DIR__ . '/../../../tmp/lazaret/' . $row['filepath'];
if (file_exists($filePath)) { if (file_exists($filePath)) {
$spec = new ImageSpec();
$spec->setResizeMode(ImageSpec::RESIZE_MODE_INBOUND_FIXEDRATIO);
$spec->setDimensions(375, 275);
$thumbPass = sprintf("thumb_%s", $filePath);
try {
$Core['media-alchemyst']
->open($filePath)
->turnInto($thumbPass, $spec)
->close();
} catch (MediaAlchemystException $e) {
}
$media = $Core['mediavorus']->guess(new \SplFileInfo($filePath)); $media = $Core['mediavorus']->guess(new \SplFileInfo($filePath));
$collection = \collection::get_from_base_id($row['base_id']); $collection = \collection::get_from_base_id($row['base_id']);
@@ -111,7 +132,8 @@ class patch_370a7 implements patchInterface
} }
$lazaretFile->setOriginalName($row['filename']); $lazaretFile->setOriginalName($row['filename']);
$lazaretFile->setPathname(__DIR__ . '/../../../tmp/lazaret/' . $row['filepath']); $lazaretFile->setPathname($row['filepath']);
$lazaretFile->setThumbPathname($thumbPass);
$lazaretFile->setCreated(new \DateTime($row['created_on'])); $lazaretFile->setCreated(new \DateTime($row['created_on']));
$lazaretFile->setSession($lazaretSession); $lazaretFile->setSession($lazaretSession);

View File

@@ -11,6 +11,9 @@ Entities\LazaretFile:
pathname: pathname:
type: string type: string
length: 512 length: 512
thumbPathname:
type: string
length: 512
originalName: originalName:
type: string type: string
length: 256 length: 256