diff --git a/Entity/UploadedFile.php b/Entity/UploadedFile.php index 23ce3ec269ebbcdd5eb99aa536fae1cec0d8752a..8b9df5ea4599f2954f82a26068da5da14835e905 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; + } + }