From b38f063b7284e3db2219f2718bcdb2cfc024860f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Guillaume=20Perr=C3=A9al?= <guillaume.perreal@irstea.fr>
Date: Thu, 3 Dec 2015 09:27:43 +0100
Subject: [PATCH] =?UTF-8?q?M=C3=A9morise=20le=20chemin=20*courant*=20d'un?=
 =?UTF-8?q?=20UploadedFile.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Comme un fichier n'est réellement déplacé qu'après l'écriture en base
de données, il faut mémoriser son emplacement exact pour que les méthodes
d'accès fonctionnent jusque là quand le chemin est modifié via setPath.
---
 Entity/UploadedFile.php | 50 ++++++++++++++++++++++++++++++++---------
 1 file changed, 39 insertions(+), 11 deletions(-)

diff --git a/Entity/UploadedFile.php b/Entity/UploadedFile.php
index 23ce3ec2..8b9df5ea 100644
--- a/Entity/UploadedFile.php
+++ b/Entity/UploadedFile.php
@@ -23,6 +23,7 @@ use Rhumsaa\Uuid\Uuid;
  *  "Irstea\FileUploadBundle\Listener\UploadedFileListener",
  *  "Irstea\FileUploadBundle\Listener\CreationDataListener"
  * })
+ * @ORM\HasLifecycleCallbacks
  */
 class UploadedFile implements UploadedFileInterface
 {
@@ -50,6 +51,11 @@ class UploadedFile implements UploadedFileInterface
      */
     private $path;
 
+    /**
+     * @var string
+     */
+    private $actualPath;
+
     /**
      *
      * @ORM\Column(type="string", length=255, nullable=true)
@@ -123,7 +129,7 @@ class UploadedFile implements UploadedFileInterface
     public function __construct()
     {
         $this->id = Uuid::uuid4()->toString();
-        $this->path = self::ORPHAN_PREFIX.$this->id;
+        $this->actualPath = $this->path = self::ORPHAN_PREFIX.$this->id;
     }
 
     /**
@@ -160,6 +166,17 @@ class UploadedFile implements UploadedFileInterface
         return $this->path;
     }
 
+    /**
+     * @return string
+     */
+    public function getActualPath()
+    {
+        if (!isset($this->actualPath)) {
+            $this->actualPath = $this->path;
+        }
+        return $this->actualPath;
+    }
+
     /**
      * {@inheritdoc}
      */
@@ -338,7 +355,7 @@ class UploadedFile implements UploadedFileInterface
         }
 
         $filesystem = $this->filesystem;
-        $path = $this->getPath();
+        $path = $this->getActualPath();
 
         if (!$filesystem->has($path)) {
             $this->setEtat(self::ETAT_MANQUANT);
@@ -373,7 +390,7 @@ class UploadedFile implements UploadedFileInterface
     public function getLastModified()
     {
         try {
-            return new \DateTime(sprintf('@%d', $this->filesystem->mtime($this->getPath())));
+            return new \DateTime(sprintf('@%d', $this->filesystem->mtime($this->getActualPath())));
         } catch(FileNotFound $ex) {
             return null;
         }
@@ -384,7 +401,7 @@ class UploadedFile implements UploadedFileInterface
      */
     public function getContent()
     {
-        return $this->filesystem->read($this->getPath());
+        return $this->filesystem->read($this->getActualPath());
     }
 
     /**
@@ -392,7 +409,7 @@ class UploadedFile implements UploadedFileInterface
      */
     public function setContent($content)
     {
-        return $this->filesystem->write($this->getPath(), $content, true);
+        return $this->filesystem->write($this->getActualPath(), $content, true);
     }
 
     /**
@@ -404,7 +421,7 @@ class UploadedFile implements UploadedFileInterface
             return 0;
         }
 
-        $stream = $this->filesystem->createStream($this->getPath());
+        $stream = $this->filesystem->createStream($this->getActualPath());
         $stream->open(new StreamMode('cb'));
         $stream->seek($writeOffset);
 
@@ -441,7 +458,7 @@ class UploadedFile implements UploadedFileInterface
             return 0;
         }
 
-        $stream = $this->filesystem->createStream($this->getPath());
+        $stream = $this->filesystem->createStream($this->getActualPath());
         $stream->open(new StreamMode('rb'));
         $stream->seek($readOffset);
 
@@ -542,9 +559,7 @@ class UploadedFile implements UploadedFileInterface
                         return false;
                     }
                     break;
-                default:    /**
-     * @return string
-     */
+                default:
                     $level++;
             }
         }
@@ -637,7 +652,7 @@ class UploadedFile implements UploadedFileInterface
             return $this->localTempPath;
         }
 
-        $stream = $this->filesystem->createStream($this->getPath());
+        $stream = $this->filesystem->createStream($this->getActualPath());
         $stream->open(new StreamMode('rb'));
         $handle = $stream->cast(STREAM_CAST_AS_STREAM);
 
@@ -659,4 +674,17 @@ class UploadedFile implements UploadedFileInterface
 
         return $this->localTempPath;
     }
+
+    /**
+     * Met à jour le chemin réel du fichier.
+     *
+     * @ORM\PostLoad
+     * @ORM\PostPersist
+     * @ORM\PostUpdate
+     */
+    public function updateActualPath()
+    {
+        $this->actualPath = $this->path;
+    }
+
 }
-- 
GitLab