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\Value\Mono as MonoValue;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Exception\IOException;
use XPDF\PdfToText;
/**
@@ -204,22 +205,25 @@ class Manager
* @param string $filename The desired filename
* @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 $output;
return realpath($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);
@@ -424,7 +429,7 @@ class Manager
try {
$core['media-alchemyst']
->open($file->getFile()->getPathname())
->turnInto($lazaretPathname, $spec)
->turnInto($lazaretPathnameThumb, $spec)
->close();
} catch (MediaAlchemystException $e) {
@@ -438,7 +443,9 @@ class Manager
$lazaretFile->setForced($forced);
$lazaretFile->setPathname($lazaretPathname);
$lazaretFile->setPathname(pathinfo($lazaretPathname, PATHINFO_BASENAME));
$lazaretFile->setThumbPathName(pathinfo($lazaretPathnameThumb, PATHINFO_BASENAME));
$lazaretFile->setSession($session);
$this->em->persist($lazaretFile);

View File

@@ -18,6 +18,7 @@ use Silex\ControllerProviderInterface;
use Silex\ControllerCollection;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Filesystem\Exception\IOException;
/**
* Lazaret controller collection
@@ -189,12 +190,13 @@ class Lazaret implements ControllerProviderInterface
{
$ret = array('success' => false, 'message' => '', 'result' => array());
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));
$ret['message'] = _('File is not present in quarantine anymore, please refresh');
return self::formatJson($app['Core']['Serializer'], $ret);
}
$file = array(
@@ -209,11 +211,6 @@ class Lazaret implements ControllerProviderInterface
$ret['result'] = $file;
$ret['success'] = true;
} catch (\Exception_NotFound $e) {
$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);
}
@@ -242,14 +239,17 @@ class Lazaret implements ControllerProviderInterface
return self::formatJson($app['Core']['Serializer'], $ret);
}
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));
$ret['message'] = _('File is not present in quarantine anymore, please refresh');
return self::formatJson($app['Core']['Serializer'], $ret);
}
try {
$borderFile = Border\File::buildFromPathfile(
$lazaretFile->getPathname(), $lazaretFile->getCollection(), $lazaretFile->getOriginalName()
);
@@ -311,15 +311,18 @@ class Lazaret implements ControllerProviderInterface
$app['Core']['EM']->remove($lazaretFile);
$app['Core']['EM']->flush();
$app['Core']['file-system']->remove($lazaretFile->getPathname());
$ret['success'] = true;
} catch (\Exception_NotFound $e) {
$ret['message'] = _('File is not present in quarantine anymore, please refresh');
} catch (\Exception $e) {
$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);
}
@@ -336,27 +339,32 @@ class Lazaret implements ControllerProviderInterface
{
$ret = array('success' => false, 'message' => '', 'result' => array());
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));
$ret['message'] = _('File is not present in quarantine anymore, please refresh');
return self::formatJson($app['Core']['Serializer'], $ret);
}
//Delete lazaret file
try {
$app['Core']['EM']->remove($lazaretFile);
$app['Core']['EM']->flush();
$app['Core']['file-system']->remove($lazaretFile->getPathname());
$ret['success'] = true;
} catch (\Exception_NotFound $e) {
$ret['message'] = _('File is not present in quarantine anymore, please refresh');
} catch (\Exception $e) {
$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);
}
@@ -380,12 +388,13 @@ class Lazaret implements ControllerProviderInterface
return self::formatJson($app['Core']['Serializer'], $ret);
}
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));
$ret['message'] = _('File is not present in quarantine anymore, please refresh');
return self::formatJson($app['Core']['Serializer'], $ret);
}
$found = false;
@@ -406,6 +415,7 @@ class Lazaret implements ControllerProviderInterface
return self::formatJson($app['Core']['Serializer'], $ret);
}
try {
$media = $app['Core']['mediavorus']->guess(new \SplFileInfo($lazaretFile->getPathname()));
$record = $lazaretFile->getCollection()->get_databox()->get_record($recordId);
@@ -415,15 +425,18 @@ class Lazaret implements ControllerProviderInterface
$app['Core']['EM']->remove($lazaretFile);
$app['Core']['EM']->flush();
$app['Core']['file-system']->remove($lazaretFile->getPathname());
$ret['success'] = true;
} catch (\Exception_NotFound $e) {
$ret['message'] = _('File is not present in quarantine anymore, please refresh');
} catch (\Exception $e) {
$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);
}
@@ -442,11 +455,14 @@ class Lazaret implements ControllerProviderInterface
/* @var $lazaretFile \Entities\LazaretFile */
if (null === $lazaretFile) {
return new Response(null, 404);
}
$lazaretPath = __DIR__ . '/../../../../../tmp/lazaret/';
$response = \set_export::stream_file(
$lazaretFile->getPathname(), $lazaretFile->getOriginalName(), 'image/jpeg', 'inline'
$lazaretPath . $lazaretFile->getThumbPathname(), $lazaretFile->getOriginalName(), 'image/jpeg', 'inline'
);
return $response;

View File

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

View File

@@ -17,6 +17,11 @@ class LazaretFile
*/
private $pathname;
/**
* @var string $thumbPathname
*/
private $thumbPathname;
/**
* @var integer $base_id
*/
@@ -372,4 +377,16 @@ class LazaretFile
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.
*/
use MediaAlchemyst\Exception\Exception as MediaAlchemystException;
use MediaAlchemyst\Specification\Image as ImageSpec;
/**
*
* @license http://opensource.org/licenses/gpl-3.0 GPLv3
@@ -84,6 +87,24 @@ class patch_370a7 implements patchInterface
$filePath = __DIR__ . '/../../../tmp/lazaret/' . $row['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));
$collection = \collection::get_from_base_id($row['base_id']);
@@ -111,7 +132,8 @@ class patch_370a7 implements patchInterface
}
$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->setSession($lazaretSession);

View File

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