Commit 826daa26 authored by Guillaume Perréal's avatar Guillaume Perréal
Browse files

Compat Symfony 3.4.

parent 701e52e8
...@@ -100,11 +100,9 @@ class UploadController extends Controller ...@@ -100,11 +100,9 @@ class UploadController extends Controller
$file = $this->fileManager->create( $file = $this->fileManager->create(
$data['name'], $data['name'],
$data['size'], (int) $data['size'],
$data['type'], $data['type'],
$data['lastModified'] ?? null, (int) $data['lastModified'] ?: null
$token !== null ? $token->getUsername() : null,
$request->getClientIp()
); );
$parameters = ['id' => $file->getId()]; $parameters = ['id' => $file->getId()];
......
...@@ -17,7 +17,7 @@ use Irstea\FileUploadBundle\Utils\MimeTypeIcon; ...@@ -17,7 +17,7 @@ use Irstea\FileUploadBundle\Utils\MimeTypeIcon;
use Rhumsaa\Uuid\Uuid; use Rhumsaa\Uuid\Uuid;
/** /**
* @ORM\Entity(repositoryClass="Irstea\FileUploadBundle\Entity\Repository\UploadedFileRepository") * @ORM\Entity
* @ORM\EntityListeners({ * @ORM\EntityListeners({
* "Irstea\FileUploadBundle\Listener\UploadedFileListener", * "Irstea\FileUploadBundle\Listener\UploadedFileListener",
* "Irstea\FileUploadBundle\Listener\CreationDataListener" * "Irstea\FileUploadBundle\Listener\CreationDataListener"
...@@ -351,15 +351,11 @@ class UploadedFile implements UploadedFileInterface ...@@ -351,15 +351,11 @@ class UploadedFile implements UploadedFileInterface
/** /**
* @param Filesystem $filesystem * @param Filesystem $filesystem
* *
* @return self
*
* @internal * @internal
*/ */
public function setFilesystem(Filesystem $filesystem) public function setFilesystem(Filesystem $filesystem): void
{ {
$this->filesystem = $filesystem; $this->filesystem = $filesystem;
return $this;
} }
/** /**
......
...@@ -21,7 +21,7 @@ interface FileManagerInterface ...@@ -21,7 +21,7 @@ interface FileManagerInterface
* *
* @api * @api
*/ */
public function get($id); public function get($id): ?UploadedFileInterface;
/** Crée un nouveau fichier uploadé, vide. /** Crée un nouveau fichier uploadé, vide.
* Le fichier est créé à l'état UploadedFileInterface::ETAT_EN_COURS. * Le fichier est créé à l'état UploadedFileInterface::ETAT_EN_COURS.
...@@ -35,7 +35,7 @@ interface FileManagerInterface ...@@ -35,7 +35,7 @@ interface FileManagerInterface
* *
* @api * @api
*/ */
public function create($name, $size, $mimeType, $lastModified = null); public function create(string $name, int $size, string $mimeType, int $lastModified = null): UploadedFileInterface;
/** Duplique un fichier. /** Duplique un fichier.
* N'accepte que des fichiers valides. * N'accepte que des fichiers valides.
...@@ -49,7 +49,7 @@ interface FileManagerInterface ...@@ -49,7 +49,7 @@ interface FileManagerInterface
* *
* @api * @api
*/ */
public function duplicate(UploadedFileInterface $original); public function duplicate(UploadedFileInterface $original): UploadedFileInterface;
/** Supprime un fichier uploadé. /** Supprime un fichier uploadé.
* *Attention :* le fichier est supprimé du disque ! * *Attention :* le fichier est supprimé du disque !
...@@ -58,7 +58,7 @@ interface FileManagerInterface ...@@ -58,7 +58,7 @@ interface FileManagerInterface
* *
* @api * @api
*/ */
public function delete(UploadedFileInterface $file); public function delete(UploadedFileInterface $file): void;
/** Indique que l'upload d'un fichier est terminé. /** Indique que l'upload d'un fichier est terminé.
* Cela passe le fichier à l'état UploadedFileInterface::ORPHELIN et provoque la mise-à-jour de la taille, du * Cela passe le fichier à l'état UploadedFileInterface::ORPHELIN et provoque la mise-à-jour de la taille, du
...@@ -68,7 +68,7 @@ interface FileManagerInterface ...@@ -68,7 +68,7 @@ interface FileManagerInterface
* *
* @internal * @internal
*/ */
public function completed(UploadedFileInterface $file); public function completed(UploadedFileInterface $file): void;
/** Retourne une liste de fichiers invalides à supprimer. /** Retourne une liste de fichiers invalides à supprimer.
* Ce sont des fichiers partiels ou orphelin qui n'ont pas été modifiés depuis plus d'une heure. * Ce sont des fichiers partiels ou orphelin qui n'ont pas été modifiés depuis plus d'une heure.
...@@ -77,12 +77,12 @@ interface FileManagerInterface ...@@ -77,12 +77,12 @@ interface FileManagerInterface
* *
* @internal * @internal
*/ */
public function findGarbage(); public function findGarbage(): array;
/** Retourne la liste des fichiers à valider. /** Retourne la liste des fichiers à valider.
* @return UploadedFileInterface[] * @return UploadedFileInterface[]
* *
* @internal * @internal
*/ */
public function findFilesToValidate(); public function findFilesToValidate(): array;
} }
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
namespace Irstea\FileUploadBundle\Model; namespace Irstea\FileUploadBundle\Model;
use DateTime; use DateTime;
use Gaufrette\Filesystem;
/** Un fichier uploadé. /** Un fichier uploadé.
*/ */
...@@ -360,4 +361,9 @@ interface UploadedFileInterface ...@@ -360,4 +361,9 @@ interface UploadedFileInterface
* @api * @api
*/ */
public function hasSameContent(UploadedFileInterface $other); public function hasSameContent(UploadedFileInterface $other);
/**
* @param Filesystem $fs
*/
public function setFilesystem(Filesystem $fs): void;
} }
...@@ -18,14 +18,13 @@ services: ...@@ -18,14 +18,13 @@ services:
# Le gestionnaire de fichiers # Le gestionnaire de fichiers
irstea_file_upload.file_manager: irstea_file_upload.file_manager:
class: Irstea\FileUploadBundle\Entity\Repository\UploadedFileRepository class: Irstea\FileUploadBundle\Service\FileManager
factory: [ "@Doctrine\\Common\\Persistence\\ObjectManager", getRepository ]
arguments: arguments:
- "@irstea_file_upload.filesystem"
- "@event_dispatcher"
- "@doctrine"
- "%irstea_file_upload.entity%" - "%irstea_file_upload.entity%"
calls: - "@logger"
- [ setFilesystem, [ "@irstea_file_upload.filesystem" ] ]
- [ setLogger, [ "@logger" ] ]
- [ setEventDispatcher, [ "@event_dispatcher" ] ]
public: true public: true
tags: tags:
- { name: monolog.logger, channel: irstea_file_upload } - { name: monolog.logger, channel: irstea_file_upload }
......
...@@ -4,9 +4,10 @@ ...@@ -4,9 +4,10 @@
* All rights reserved. * All rights reserved.
*/ */
namespace Irstea\FileUploadBundle\Entity\Repository; namespace Irstea\FileUploadBundle\Service;
use DateTime; use DateTime;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository; use Doctrine\ORM\EntityRepository;
use Gaufrette\Filesystem; use Gaufrette\Filesystem;
use Gaufrette\StreamMode; use Gaufrette\StreamMode;
...@@ -17,54 +18,70 @@ use Irstea\FileUploadBundle\Exception\RejectedFileException; ...@@ -17,54 +18,70 @@ use Irstea\FileUploadBundle\Exception\RejectedFileException;
use Irstea\FileUploadBundle\FileUploadEvents; use Irstea\FileUploadBundle\FileUploadEvents;
use Irstea\FileUploadBundle\Model\FileManagerInterface; use Irstea\FileUploadBundle\Model\FileManagerInterface;
use Irstea\FileUploadBundle\Model\UploadedFileInterface; use Irstea\FileUploadBundle\Model\UploadedFileInterface;
use Psr\Log\LoggerAwareTrait; use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel; use Psr\Log\LogLevel;
use Psr\Log\NullLogger;
use Rhumsaa\Uuid\Uuid; use Rhumsaa\Uuid\Uuid;
use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Bridge\Doctrine\RegistryInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface;
/** /**
* Class UploadedFileRepository. * Class FileManager.
*/ */
class UploadedFileRepository extends EntityRepository implements FileManagerInterface final class FileManager implements FileManagerInterface
{ {
use LoggerAwareTrait;
/** /**
* @var Filesystem * @var Filesystem
*/ */
protected $filesystem; private $filesystem;
/** /**
* @var EventDispatcher * @var EventDispatcherInterface
*/ */
protected $eventDispatcher; private $eventDispatcher;
/** /**
* @param Filesystem $filesystem * @var LoggerInterface
*/ */
public function setFilesystem(Filesystem $filesystem) private $logger;
{
$this->filesystem = $filesystem;
}
/** /**
* @var EntityManagerInterface
*/
private $entityManager;
/**
* @var EntityRepository
*/
private $repository;
/**
* FileManager constructor.
*
* @param Filesystem $filesystem
* @param EventDispatcherInterface $eventDispatcher * @param EventDispatcherInterface $eventDispatcher
* @param RegistryInterface $doctrine
* @param string $entityClass
* @param LoggerInterface $logger
*/ */
public function setEventDispatcher(EventDispatcherInterface $eventDispatcher) public function __construct(
{ Filesystem $filesystem,
EventDispatcherInterface $eventDispatcher,
RegistryInterface $doctrine,
string $entityClass,
LoggerInterface $logger = null
) {
$this->filesystem = $filesystem;
$this->eventDispatcher = $eventDispatcher; $this->eventDispatcher = $eventDispatcher;
$this->logger = $logger ?: new NullLogger();
$this->entityManager = $doctrine->getEntityManagerForClass($entityClass);
$this->repository = $this->entityManager->getRepository($entityClass);
} }
/** /**
* @param string $filename * {@inheritdoc}
* @param int $size
* @param string $mimeType
* @param null $lastModified
*
* @return UploadedFile
*/ */
public function create($filename, $size, $mimeType, $lastModified = null) public function create(string $filename, int $size, string $mimeType, int $lastModified = null): UploadedFileInterface
{ {
$file = new UploadedFile(); $file = new UploadedFile();
...@@ -82,20 +99,18 @@ class UploadedFileRepository extends EntityRepository implements FileManagerInte ...@@ -82,20 +99,18 @@ class UploadedFileRepository extends EntityRepository implements FileManagerInte
] ]
); );
$this->_em->persist($file); $this->entityManager->persist($file);
$this->_em->flush(); $this->entityManager->flush();
$this->log(LogLevel::INFO, 'File created', ['file' => $file]); $this->logger->log(LogLevel::INFO, 'File created', ['file' => $file]);
return $file; return $file;
} }
/** /**
* @param UploadedFileInterface $original * {@inheritdoc}
*
* @return UploadedFile
*/ */
public function duplicate(UploadedFileInterface $original) public function duplicate(UploadedFileInterface $original): UploadedFileInterface
{ {
if (!$original->isValid()) { if (!$original->isValid()) {
throw new InvalidArgumentException('Impossible de dupliquer le fichier ' . $original->getId() . ' car il est invalide !'); throw new InvalidArgumentException('Impossible de dupliquer le fichier ' . $original->getId() . ' car il est invalide !');
...@@ -124,26 +139,26 @@ class UploadedFileRepository extends EntityRepository implements FileManagerInte ...@@ -124,26 +139,26 @@ class UploadedFileRepository extends EntityRepository implements FileManagerInte
$original->copyTo($stream->cast(STREAM_CAST_AS_STREAM)); $original->copyTo($stream->cast(STREAM_CAST_AS_STREAM));
$stream->close(); $stream->close();
$this->_em->persist($new); $this->entityManager->persist($new);
return $new; return $new;
} }
/** /**
* @param UploadedFileInterface $file * {@inheritdoc}
*/ */
public function delete(UploadedFileInterface $file) public function delete(UploadedFileInterface $file): void
{ {
$this->_em->remove($file); $this->entityManager->remove($file);
$this->_em->flush(); $this->entityManager->flush();
$this->log(LogLevel::INFO, 'File deleted', ['file' => $file]); $this->logger->log(LogLevel::INFO, 'File deleted', ['file' => $file]);
} }
/** /**
* @param string $uuid * {@inheritdoc}
*/ */
public function get($uuid) public function get($uuid): ?UploadedFileInterface
{ {
if (!$uuid) { if (!$uuid) {
return null; return null;
...@@ -152,13 +167,14 @@ class UploadedFileRepository extends EntityRepository implements FileManagerInte ...@@ -152,13 +167,14 @@ class UploadedFileRepository extends EntityRepository implements FileManagerInte
throw new InvalidArgumentException(sprintf('Identifiant invalide: %s', (string) $uuid)); throw new InvalidArgumentException(sprintf('Identifiant invalide: %s', (string) $uuid));
} }
return $this->findOneById($uuid); /* @noinspection PhpUndefinedMethodInspection */
return $this->repository->findOneById($uuid);
} }
/** /**
* @param UploadedFileInterface $file * {@inheritdoc}
*/ */
public function completed(UploadedFileInterface $file) public function completed(UploadedFileInterface $file): void
{ {
$path = $file->getPath(); $path = $file->getPath();
$filesystem = $this->filesystem; $filesystem = $this->filesystem;
...@@ -169,41 +185,29 @@ class UploadedFileRepository extends EntityRepository implements FileManagerInte ...@@ -169,41 +185,29 @@ class UploadedFileRepository extends EntityRepository implements FileManagerInte
->setMimeType($filesystem->mimeType($path)) ->setMimeType($filesystem->mimeType($path))
->setEtat(UploadedFileInterface::ETAT_ORPHELIN); ->setEtat(UploadedFileInterface::ETAT_ORPHELIN);
$this->_em->persist($file); $this->entityManager->persist($file);
try { try {
$this->eventDispatcher->dispatch(FileUploadEvents::UPLOAD_COMPLETE, new FileUploadCompleteEvent($file)); $this->eventDispatcher->dispatch(FileUploadEvents::UPLOAD_COMPLETE, new FileUploadCompleteEvent($file));
$this->_em->flush(); $this->entityManager->flush();
$this->log(LogLevel::INFO, 'File completed', ['file' => $file]); $this->logger->log(LogLevel::INFO, 'File completed', ['file' => $file]);
} catch (RejectedFileException $ex) { } catch (RejectedFileException $ex) {
$file->setEtat(UploadedFileInterface::ETAT_REJETE); $file->setEtat(UploadedFileInterface::ETAT_REJETE);
$this->_em->flush(); $this->entityManager->flush();
$this->log(LogLevel::WARNING, 'File rejected', ['file' => $file, 'exception' => $ex]); $this->logger->log(LogLevel::WARNING, 'File rejected', ['file' => $file, 'exception' => $ex]);
throw $ex; throw $ex;
} }
} }
/** /**
* @param string $level * {@inheritdoc}
* @param string $message
* @param array $context
*/
protected function log($level, $message, array $context = [])
{
if (null !== $this->logger) {
$this->logger->log($level, $message, $context);
}
}
/**
* @return array
*/ */
public function findGarbage() public function findGarbage(): array
{ {
$files = $this->findBy(['etat' => [UploadedFileInterface::ETAT_EN_COURS, UploadedFileInterface::ETAT_ORPHELIN]]); $files = $this->repository->findBy(['etat' => [UploadedFileInterface::ETAT_EN_COURS, UploadedFileInterface::ETAT_ORPHELIN]]);
$limit = new DateTime('now'); $limit = new DateTime('now');
$limit->modify('- 1 hour'); $limit->modify('- 1 hour');
...@@ -219,10 +223,10 @@ class UploadedFileRepository extends EntityRepository implements FileManagerInte ...@@ -219,10 +223,10 @@ class UploadedFileRepository extends EntityRepository implements FileManagerInte
} }
/** /**
* @return array * {@inheritdoc}
*/ */
public function findFilesToValidate() public function findFilesToValidate(): array
{ {
return $this->findBy(['etat' => [UploadedFileInterface::ETAT_ORPHELIN, UploadedFileInterface::ETAT_NORMAL]]); return $this->repository->findBy(['etat' => [UploadedFileInterface::ETAT_ORPHELIN, UploadedFileInterface::ETAT_NORMAL]]);
} }
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment