From 95ffe933cdc465a9d4f7a5e54a15efecf28ed5af Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Guillaume=20Perr=C3=A9al?= <guillaume.perreal@irstea.fr>
Date: Thu, 22 Jan 2015 10:25:06 +0100
Subject: [PATCH] UploadController: ajout de getAction.

---
 Controller/UploadController.php | 39 +++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)

diff --git a/Controller/UploadController.php b/Controller/UploadController.php
index 7aeaebcb..dd81da81 100644
--- a/Controller/UploadController.php
+++ b/Controller/UploadController.php
@@ -16,8 +16,11 @@ use Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface;
 use Symfony\Component\HttpFoundation\JsonResponse;
 use Symfony\Component\HttpFoundation\Request;
 use Symfony\Component\HttpFoundation\Response;
+use Symfony\Component\HttpFoundation\ResponseHeaderBag;
+use Symfony\Component\HttpFoundation\StreamedResponse;
 use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
 use Symfony\Component\HttpKernel\Exception\HttpException;
+use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
 use Symfony\Component\Routing\RouterInterface;
 
 /**
@@ -146,6 +149,41 @@ class UploadController extends Controller
         );
     }
 
+    /**
+     * @Route("/{id}", name="file_upload_get")
+     * @Method("GET")
+     * @param Request $request
+     * @param UploadedFile $file
+     */
+    public function getAction(Request $request, UploadedFile $file)
+    {
+        $this->validateToken($request);
+
+        if(!$file->isValid()) {
+            throw new NotFoundHttpException();
+        }
+
+        $response = StreamedResponse::create(function() use($file) { echo $file->getContent(); })
+            ->setPrivate()
+            ->setLastModified($file->getLastModified())
+            ->setEtag($file->getChecksum());
+
+        $response->headers->add(
+            [
+                'Content-Type'        => $file->getMimeType(),
+                'Content-Length'      => $file->getSize(),
+                'Content-Disposition' => $response->headers->makeDisposition(
+                    ResponseHeaderBag::DISPOSITION_ATTACHMENT,
+                    $file->getOriginalFilename()
+                ),
+            ]
+        );
+
+        $response->isNotModified($request);
+
+        return $response;
+    }
+
     /**
      * @Route("/{id}", name="file_upload_delete")
      * @Method("DELETE")
@@ -157,6 +195,7 @@ class UploadController extends Controller
         $this->validateToken($request);
 
         $this->fileManager->delete($file);
+
         return $this->createResponse();
     }
 
-- 
GitLab