diff --git a/Entity/Repository/UploadedFileRepository.php b/Entity/Repository/UploadedFileRepository.php index 9335ec404755d0460abb991b37f8b2434499f027..4f538704471822d39cc2547cd7bf911222bb8ce0 100644 --- a/Entity/Repository/UploadedFileRepository.php +++ b/Entity/Repository/UploadedFileRepository.php @@ -13,6 +13,7 @@ use Irstea\FileUploadBundle\Entity\UploadedFile; use Irstea\FileUploadBundle\Event\FileUploadCompleteEvent; use Irstea\FileUploadBundle\FileUploadEvents; use Irstea\FileUploadBundle\Model\FileManagerInterface; +use Irstea\FileUploadBundle\Model\UploadedFileInterface; use Psr\Log\LogLevel; use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\EventDispatcher\EventDispatcherInterface; @@ -80,7 +81,7 @@ class UploadedFileRepository extends EntityRepository implements FileManagerInte return $file; } - public function delete(UploadedFile $file) + public function delete(UploadedFileInterface $file) { $this->_em->remove($file); $this->_em->flush(); @@ -93,7 +94,7 @@ class UploadedFileRepository extends EntityRepository implements FileManagerInte return $this->findOneById($id); } - public function completed(UploadedFile $file) + public function completed(UploadedFileInterface $file) { $path = $file->getPath(); $fs = $this->filesystem; @@ -102,7 +103,7 @@ class UploadedFileRepository extends EntityRepository implements FileManagerInte ->setChecksum($fs->checksum($path)) ->setSize($fs->size($path)) ->setMimeType($fs->mimeType($path)) - ->setEtat(UploadedFile::ETAT_ORPHELIN); + ->setEtat(UploadedFileInterface::ETAT_ORPHELIN); $this->eventDispatcher->dispatch(FileUploadEvents::UPLOAD_COMPLETE, new FileUploadCompleteEvent($file)); diff --git a/Entity/UploadedFile.php b/Entity/UploadedFile.php index f4c8d443d13c2f94cd2cc468248af6de228f5f68..293ab375fab8f0928b2b3dd1c746ff6f4c8cfd01 100644 --- a/Entity/UploadedFile.php +++ b/Entity/UploadedFile.php @@ -12,23 +12,18 @@ use Doctrine\ORM\Mapping as ORM; use Gaufrette\Filesystem; use Gaufrette\StreamMode; use InvalidArgumentException; +use Irstea\FileUploadBundle\Model\UploadedFileInterface; use Rhumsaa\Uuid\Uuid; /** * @ORM\Entity(repositoryClass="Irstea\FileUploadBundle\Entity\Repository\UploadedFileRepository") * @ORM\EntityListeners({"Irstea\FileUploadBundle\Listener\UploadedFileListener"}) */ -class UploadedFile +class UploadedFile implements UploadedFileInterface { // Taille de bloc utilisé pour les copies static public $copyBlockSize = 8192; - const ETAT_EN_COURS = 'en-cours'; - const ETAT_ORPHELIN = 'orphelin'; - const ETAT_NORMAL = 'normal'; - const ETAT_CORROMPU = 'corrompu'; - const ETAT_MANQUANT = 'manquant'; - /** * @ORM\Id * @ORM\Column(type="guid") @@ -114,7 +109,7 @@ class UploadedFile * Set originalFilename * * @param string $displayName - * @return UploadedFile + * @return UploadedFileInterface */ public function setDisplayName($displayName) { @@ -147,7 +142,7 @@ class UploadedFile * Set path * * @param string $path - * @return UploadedFile + * @return UploadedFileInterface */ public function setPath($path) { @@ -173,7 +168,7 @@ class UploadedFile * Set mimeType * * @param string $mimeType - * @return UploadedFile + * @return UploadedFileInterface */ public function setMimeType($mimeType) { @@ -196,7 +191,7 @@ class UploadedFile * Set size * * @param integer $size - * @return UploadedFile + * @return UploadedFileInterface */ public function setSize($size) { @@ -219,7 +214,7 @@ class UploadedFile * Set checksum * * @param string $checksum - * @return UploadedFile + * @return UploadedFileInterface */ public function setChecksum($checksum) { @@ -242,7 +237,7 @@ class UploadedFile * Set etat * * @param string $etat - * @return UploadedFile + * @return UploadedFileInterface */ public function setEtat($etat) { @@ -278,7 +273,7 @@ class UploadedFile * Set metadata * * @param array $metadata - * @return UploadedFile + * @return UploadedFileInterface */ public function setMetadata(array $metadata = null) { diff --git a/Event/FileUploadCompleteEvent.php b/Event/FileUploadCompleteEvent.php index f1ed986b02256b8e768b51dbc719e525b77a5059..a35df5a306e84242bc79d38de88c8d5e6707a23a 100644 --- a/Event/FileUploadCompleteEvent.php +++ b/Event/FileUploadCompleteEvent.php @@ -7,7 +7,7 @@ namespace Irstea\FileUploadBundle\Event; -use Irstea\FileUploadBundle\Entity\UploadedFile; +use Irstea\FileUploadBundle\Model\UploadedFileInterface; use Symfony\Component\EventDispatcher\Event; /** @@ -19,22 +19,22 @@ class FileUploadCompleteEvent extends Event { /** * - * @var UploadedFile + * @var UploadedFileInterface */ private $uploadedFile; /** * - * @param UploadedFile $uploadedFile + * @param UploadedFileInterface $uploadedFile */ - public function __construct(UploadedFile $uploadedFile) + public function __construct(UploadedFileInterface $uploadedFile) { $this->uploadedFile = $uploadedFile; } /** * - * @return UploadedFile $uploadedFile + * @return UploadedFileInterface $uploadedFile */ public function getUploadedFile() { diff --git a/Form/DataTranformer/UploadedFileTransformer.php b/Form/DataTranformer/UploadedFileTransformer.php index c97958c077c99b7b81ac72196bb86cc649062dca..790bcd86ecdf44a2f7de62aed0019e22a46663a4 100644 --- a/Form/DataTranformer/UploadedFileTransformer.php +++ b/Form/DataTranformer/UploadedFileTransformer.php @@ -7,10 +7,11 @@ namespace Irstea\FileUploadBundle\Form\DataTranformer; -use Irstea\FileUploadBundle\Entity\UploadedFile; use Irstea\FileUploadBundle\Model\FileManagerInterface; +use Irstea\FileUploadBundle\Model\UploadedFileInterface; use Symfony\Component\Form\DataTransformerInterface; use Symfony\Component\Form\Exception\TransformationFailedException; +use Traversable; /** * Description of UploadedFileTransformer @@ -48,7 +49,7 @@ class UploadedFileTransformer implements DataTransformerInterface return []; } - if(!is_array($value) && !$value instanceof \Traversable) { + if(!is_array($value) && !$value instanceof Traversable) { throw new TransformationFailedException('Expected an array or a \Traversable.'); } @@ -74,7 +75,7 @@ class UploadedFileTransformer implements DataTransformerInterface return []; } - if(!is_array($value) && !$value instanceof \Traversable) { + if(!is_array($value) && !$value instanceof Traversable) { throw new TransformationFailedException('Expected an array or a \Traversable.'); } @@ -98,8 +99,8 @@ class UploadedFileTransformer implements DataTransformerInterface return null; } - if(!$value instanceof UploadedFile) { - throw new TransformationFailedException('Expected an UploadedFile.'); + if(!$value instanceof UploadedFileInterface) { + throw new TransformationFailedException('Expected an UploadedFileInterface.'); } return $value; @@ -108,7 +109,7 @@ class UploadedFileTransformer implements DataTransformerInterface /** * * @param mixed $value - * @return UploadedFile + * @return UploadedFileInterface */ protected function reverseTransformFile($value) { @@ -120,13 +121,13 @@ class UploadedFileTransformer implements DataTransformerInterface return null; } - if($value instanceof UploadedFile) { + if($value instanceof UploadedFileInterface) { if($value->isOrphelin()) { - $value->setEtat(UploadedFile::ETAT_NORMAL); + $value->setEtat(UploadedFileInterface::ETAT_NORMAL); } return $value; } - throw new TransformationFailedException('Expected an UploadedFile or a valid file UUID.'); + throw new TransformationFailedException('Expected an UploadedFileInterface or a valid file identifier.'); } } diff --git a/Http/UploadedFileResponse.php b/Http/UploadedFileResponse.php index 41402d212a7dad90b9ed6036678efdd49e4a1e91..06dd3aec8b3b838536e05e7265adbc8d9d3c00c2 100644 --- a/Http/UploadedFileResponse.php +++ b/Http/UploadedFileResponse.php @@ -8,7 +8,7 @@ namespace Irstea\FileUploadBundle\Http; use DateTime; -use Irstea\FileUploadBundle\Entity\UploadedFile; +use Irstea\FileUploadBundle\Model\UploadedFileInterface; use LogicException; use SplFileInfo; use Symfony\Component\HttpFoundation\File\Exception\FileException; @@ -26,24 +26,31 @@ class UploadedFileResponse extends Response { /** * - * @var UploadedFile + * @var UploadedFileInterface */ protected $file; + /** + * @var int + */ protected $offset = 0; + /** + * + * @var int + */ protected $maxlen; /** * Constructor. * - * @param UploadedFile $file The file to stream - * @param int $status The response status code - * @param array $headers An array of response headers - * @param bool $public Files are public by default - * @param null|string $contentDisposition The type of Content-Disposition to set automatically with the filename + * @param UploadedFileInterface $file The file to stream + * @param int $status The response status code + * @param array $headers An array of response headers + * @param bool $public Files are public by default + * @param null|string $contentDisposition The type of Content-Disposition to set automatically with the filename */ - public function __construct(UploadedFile $file = null, $status = 200, $headers = array(), $public = true, $contentDisposition = null) + public function __construct(UploadedFileInterface $file = null, $status = 200, $headers = array(), $public = true, $contentDisposition = null) { parent::__construct(null, $status, $headers); @@ -57,11 +64,11 @@ class UploadedFileResponse extends Response } /** - * @param UploadedFile $file The file to stream - * @param int $status The response status code - * @param array $headers An array of response headers - * @param bool $public Files are public by default - * @param null|string $contentDisposition The type of Content-Disposition to set automatically with the filename + * @param UploadedFileInterface $file The file to stream + * @param int $status The response status code + * @param array $headers An array of response headers + * @param bool $public Files are public by default + * @param null|string $contentDisposition The type of Content-Disposition to set automatically with the filename * * @return UploadedFileResponse The created response */ @@ -74,14 +81,14 @@ class UploadedFileResponse extends Response /** * Sets the file to stream. * - * @param UploadedFile $file The file to stream - * @param string $contentDisposition + * @param UploadedFileInterface $file The file to stream + * @param string $contentDisposition * * @return UploadedFileResponse * * @throws FileException */ - public function setFile(UploadedFile $file, $contentDisposition = null) + public function setFile(UploadedFileInterface $file, $contentDisposition = null) { $this->file = $file; @@ -99,7 +106,7 @@ class UploadedFileResponse extends Response /** * Gets the file. * - * @return UploadedFile The file to stream + * @return UploadedFileInterface The file to stream */ public function getFile() { @@ -118,7 +125,7 @@ class UploadedFileResponse extends Response public function setContentDisposition($disposition, $filename = '', $filenameFallback = '') { if ($filename === '') { - $filename = $this->file->getOriginalFilename(); + $filename = $this->file->getDisplayName(); } $dispositionHeader = $this->headers->makeDisposition($disposition, $filename, $filenameFallback); diff --git a/Model/UploadedFileInterface.php b/Model/UploadedFileInterface.php new file mode 100644 index 0000000000000000000000000000000000000000..bb55278a64edcd139e949f4b73579303acf961d7 --- /dev/null +++ b/Model/UploadedFileInterface.php @@ -0,0 +1,207 @@ +<?php + +/* + * Copyright (C) 2015 IRSTEA + * All rights reserved. + */ + +namespace Irstea\FileUploadBundle\Model; + +use DateTime; + +/** + * + * @author Guillaume Perréal <guillaume.perreal@irstea.fr> + */ +interface UploadedFileInterface +{ + const ETAT_EN_COURS = 'en-cours'; + const ETAT_ORPHELIN = 'orphelin'; + const ETAT_NORMAL = 'normal'; + const ETAT_CORROMPU = 'corrompu'; + const ETAT_MANQUANT = 'manquant'; + + /** + * Get id + * + * @return integer + */ + public function getId(); + + /** + * Set originalFilename + * + * @param string $displayName + * @return UploadedFileInterface + */ + public function setDisplayName($displayName); + + /** + * Get originalFilename + * + * @return string + */ + public function getDisplayName(); + + /** + * Get path + * + * @return string + */ + public function getPath(); + + /** + * Set path + * + * @param string $path + * @return UploadedFileInterface + */ + public function setPath($path); + + /** Change le chemin d'un fichier sans changer le nom. + * + * @param string $newDir Nouveau répertoire + * @return UploadedFileInterface + */ + public function moveTo($newDir); + + /** + * Set mimeType + * + * @param string $mimeType + * @return UploadedFileInterface + */ + public function setMimeType($mimeType); + + /** + * Get mimeType + * + * @return string + */ + public function getMimeType(); + + /** + * Set size + * + * @param integer $size + * @return UploadedFileInterface + */ + public function setSize($size); + + /** + * Get size + * + * @return integer + */ + public function getSize(); + + /** + * Set checksum + * + * @param string $checksum + * @return UploadedFileInterface + */ + public function setChecksum($checksum); + + /** + * Get checksum + * + * @return string + */ + public function getChecksum(); + + /** + * Set etat + * + * @param string $etat + * @return UploadedFileInterface + */ + public function setEtat($etat); + + /** + * Get etat + * + * @return string + */ + public function getEtat(); + + /** + * Get createdAt + * + * @return DateTime + */ + public function getCreatedAt(); + + /** + * Set metadata + * + * @param array $metadata + * @return UploadedFileInterface + */ + public function setMetadata(array $metadata); + + /** + * Get metadata + * + * @return array + */ + public function getMetadata(); + + /** + * @return string + */ + public function __toString(); + + /** + * + */ + public function validate(); + + /** + * + * @return boolean + */ + public function isValid(); + + /** + * + * @return boolean + */ + public function isOrphelin(); + + /** Retourne la date de dernière modification dans le filesystem. + * + * @return DateTime + */ + public function getLastModified(); + + /** Retourne le contenu du fichier. + * + * @return string Une chaîne si $asResource vaut faux, + */ + public function getContent(); + + /** Ecrit dans le fichier. + * + * @param string $content + */ + public function setContent($content); + + /** Ecrit dans le fichier depuis un descripteur de fichier. + * + * @param resource $source + * @param int $maxlen + * @param int $writeOffset + * @return int + */ + public function copyFrom($source, $maxlen = -1, $writeOffset = 0); + + /** Envoie le contenu du fichier dans un descripteur de fichier. + * + * @param resource $dest + * @param int $maxlen + * @param int $readOffset + * @return int + */ + public function copyTo($dest, $maxlen = PHP_INT_MAX, $readOffset = 0); +}