From b0dba1286305fb1a865572ab709540046fd83618 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Guillaume=20Perr=C3=A9al?= <guillaume.perreal@irstea.fr>
Date: Fri, 30 Jan 2015 11:52:01 +0100
Subject: [PATCH] =?UTF-8?q?UploadedFile:=20d=C3=A9place=20le=20fichier=20h?=
 =?UTF-8?q?ors=20de=20l'orphelinat=20lorsqu'il=20passe=20de=20l'=C3=A9tat?=
 =?UTF-8?q?=20"orphelin"=20=C3=A0=20"normal".?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 Entity/UploadedFile.php | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/Entity/UploadedFile.php b/Entity/UploadedFile.php
index 452c6e52..e8b935ea 100644
--- a/Entity/UploadedFile.php
+++ b/Entity/UploadedFile.php
@@ -13,7 +13,6 @@ use Gaufrette\Exception\FileNotFound;
 use Gaufrette\Filesystem;
 use Gaufrette\StreamMode;
 use InvalidArgumentException;
-use Irstea\FileUploadBundle\Entity\Repository\UploadedFileRepository;
 use Irstea\FileUploadBundle\Model\UploadedFileInterface;
 use Irstea\FileUploadBundle\Utils\MimeTypeIcon;
 use Rhumsaa\Uuid\Uuid;
@@ -27,6 +26,8 @@ class UploadedFile implements UploadedFileInterface
     // Taille de bloc utilisé pour les copies
     static public $copyBlockSize = 8192;
 
+    const ORPHAN_PREFIX = "orphan/";
+
     /**
      * @ORM\Id
      * @ORM\Column(type="guid")
@@ -120,7 +121,7 @@ class UploadedFile implements UploadedFileInterface
     public function __construct($createdBy = null, $createdFrom = null)
     {
         $this->id = Uuid::uuid4()->toString();
-        $this->path = "orphan/".$this->id;
+        $this->path = self::ORPHAN_PREFIX.$this->id;
         $this->createdAt = new DateTime('now');
         $this->createdBy = $createdBy;
         $this->createdFrom = $createdFrom;
@@ -285,6 +286,12 @@ class UploadedFile implements UploadedFileInterface
         )) {
             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;
 
         return $this;
@@ -488,7 +495,6 @@ class UploadedFile implements UploadedFileInterface
         $stream->open(new StreamMode('rb'));
         $stream->seek($readOffset);
 
-
         if(false !== $fileHandle = $stream->cast(STREAM_CAST_AS_STREAM)) {
             // Utilise stream_copy_to_stream si le Stream nous renvoie un filehandle
             $copied = $this->stream_copy_to_stream($fileHandle, $dest, $actualLength);
-- 
GitLab