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

Ajout d'une interface UploadedFileInterface, à manipuler à la place d'UploadedFile.

Showing with 260 additions and 49 deletions
+260 -49
......@@ -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));
......
......@@ -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)
{
......
......@@ -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()
{
......
......@@ -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.');
}
}
......@@ -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);
......
<?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);
}
Supports Markdown
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