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

UploadedFile: déplace le fichier hors de l'orphelinat lorsqu'il passe de...

UploadedFile: déplace le fichier hors de l'orphelinat lorsqu'il passe de l'état "orphelin" à "normal".
Showing with 9 additions and 3 deletions
+9 -3
...@@ -13,7 +13,6 @@ use Gaufrette\Exception\FileNotFound; ...@@ -13,7 +13,6 @@ use Gaufrette\Exception\FileNotFound;
use Gaufrette\Filesystem; use Gaufrette\Filesystem;
use Gaufrette\StreamMode; use Gaufrette\StreamMode;
use InvalidArgumentException; use InvalidArgumentException;
use Irstea\FileUploadBundle\Entity\Repository\UploadedFileRepository;
use Irstea\FileUploadBundle\Model\UploadedFileInterface; use Irstea\FileUploadBundle\Model\UploadedFileInterface;
use Irstea\FileUploadBundle\Utils\MimeTypeIcon; use Irstea\FileUploadBundle\Utils\MimeTypeIcon;
use Rhumsaa\Uuid\Uuid; use Rhumsaa\Uuid\Uuid;
...@@ -27,6 +26,8 @@ class UploadedFile implements UploadedFileInterface ...@@ -27,6 +26,8 @@ class UploadedFile implements UploadedFileInterface
// Taille de bloc utilisé pour les copies // Taille de bloc utilisé pour les copies
static public $copyBlockSize = 8192; static public $copyBlockSize = 8192;
const ORPHAN_PREFIX = "orphan/";
/** /**
* @ORM\Id * @ORM\Id
* @ORM\Column(type="guid") * @ORM\Column(type="guid")
...@@ -120,7 +121,7 @@ class UploadedFile implements UploadedFileInterface ...@@ -120,7 +121,7 @@ class UploadedFile implements UploadedFileInterface
public function __construct($createdBy = null, $createdFrom = null) public function __construct($createdBy = null, $createdFrom = null)
{ {
$this->id = Uuid::uuid4()->toString(); $this->id = Uuid::uuid4()->toString();
$this->path = "orphan/".$this->id; $this->path = self::ORPHAN_PREFIX.$this->id;
$this->createdAt = new DateTime('now'); $this->createdAt = new DateTime('now');
$this->createdBy = $createdBy; $this->createdBy = $createdBy;
$this->createdFrom = $createdFrom; $this->createdFrom = $createdFrom;
...@@ -285,6 +286,12 @@ class UploadedFile implements UploadedFileInterface ...@@ -285,6 +286,12 @@ class UploadedFile implements UploadedFileInterface
)) { )) {
throw new InvalidArgumentException(sprintf("Etat invalide: '%s'", (string)$etat)); throw new InvalidArgumentException(sprintf("Etat invalide: '%s'", (string)$etat));
} }
// Déplace le fichier hors de l'orphelinat quand on passe d'orphelin à nouveau
if($this->etat === self::ETAT_ORPHELIN && $etat === self::ETAT_NORMAL && 0 === strpos($this->path, self::ORPHAN_PREFIX)) {
$this->path = substr($this->path, strlen($this->path));
}
$this->etat = $etat; $this->etat = $etat;
return $this; return $this;
...@@ -488,7 +495,6 @@ class UploadedFile implements UploadedFileInterface ...@@ -488,7 +495,6 @@ class UploadedFile implements UploadedFileInterface
$stream->open(new StreamMode('rb')); $stream->open(new StreamMode('rb'));
$stream->seek($readOffset); $stream->seek($readOffset);
if(false !== $fileHandle = $stream->cast(STREAM_CAST_AS_STREAM)) { if(false !== $fileHandle = $stream->cast(STREAM_CAST_AS_STREAM)) {
// Utilise stream_copy_to_stream si le Stream nous renvoie un filehandle // Utilise stream_copy_to_stream si le Stream nous renvoie un filehandle
$copied = $this->stream_copy_to_stream($fileHandle, $dest, $actualLength); $copied = $this->stream_copy_to_stream($fileHandle, $dest, $actualLength);
......
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