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

Mémorise le chemin *courant* d'un UploadedFile.

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.
Showing with 39 additions and 11 deletions
+39 -11
......@@ -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;
}
}
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