diff --git a/Entity/UploadedFile.php b/Entity/UploadedFile.php index 452c6e52feef1e1b0a291386bc1e468920fc5a7a..e8b935ea38600ea897e16d058e685fb824df2b9f 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);