From f10c196495ca357765ef0a076e379c83d795aff7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Guillaume=20Perr=C3=A9al?= <guillaume.perreal@irstea.fr>
Date: Fri, 27 Nov 2015 15:08:27 +0100
Subject: [PATCH] =?UTF-8?q?Ajout=20d'un=20m=C3=A9thode=20duplicate=20?=
 =?UTF-8?q?=C3=A0=20FileManagerInterface.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Et implémentation.
---
 Entity/Repository/UploadedFileRepository.php | 35 ++++++++++++++++++++
 Model/FileManagerInterface.php               | 11 ++++++
 2 files changed, 46 insertions(+)

diff --git a/Entity/Repository/UploadedFileRepository.php b/Entity/Repository/UploadedFileRepository.php
index eb7f60bc..592e4d36 100644
--- a/Entity/Repository/UploadedFileRepository.php
+++ b/Entity/Repository/UploadedFileRepository.php
@@ -84,6 +84,41 @@ class UploadedFileRepository extends EntityRepository implements FileManagerInte
         return $file;
     }
 
+    public function duplicate(UploadedFileInterface $original)
+    {
+        if (!$original->isValid()) {
+            throw new \InvalidArgumentException("Impossible de dupliquer le fichier ".$original->getId()." car il est invalide !");
+        }
+
+        $new = new UploadedFile();
+
+        $metadata = $original->getMetadata();
+        if (!isset($metadata["duplicateOf"])) {
+            $metadata["duplicateOf"] = [$original->getId()];
+        } else {
+            array_unshift($metadata["duplicateOf"], $original->getId());
+        }
+
+        $new
+            ->setFilesystem($this->filesystem)
+            ->setMetadata($metadata)
+            ->setDisplayName($original->getDisplayName())
+            ->setDescription($original->getDescription())
+            ->setMimeType($original->getMimeType())
+            ->setChecksum($original->getChecksum())
+            ->setSize($original->getSize());
+
+        $stream = $this->filesystem->createStream($new->getPath());
+        $stream->open("cb");
+        $original->copyTo($stream);
+        $stream->close();
+
+        $this->_em->persist($new);
+        $this->_em->flush();
+
+        return $new;
+    }
+
     public function delete(UploadedFileInterface $file)
     {
         $this->_em->remove($file);
diff --git a/Model/FileManagerInterface.php b/Model/FileManagerInterface.php
index 4616577b..eff9ebf8 100644
--- a/Model/FileManagerInterface.php
+++ b/Model/FileManagerInterface.php
@@ -41,6 +41,17 @@ interface FileManagerInterface
      */
     public function create($name, $size, $mimeType, $lastModified = null);
 
+    /** Duplique un fichier.
+     *
+     * N'accepte que des fichiers valides.
+     * Le nouveau fichier est automatiquement orphelin, avec un chemin par défaut.
+     *
+     * @param UploadedFileInterface $original
+     * @return UploadedFileInterface
+     * @throw InvalidArgumentException Le fichier original est invalide.
+     */
+    public function duplicate(UploadedFileInterface $original);
+
     /** Supprime un fichier uploadé.
      *
      * *Attention :* le fichier est supprimé du disque !
-- 
GitLab