From bbee02c4529799ffab074ba105387f1944b33736 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:19:11 +0100
Subject: [PATCH] =?UTF-8?q?UploadController:=20g=C3=A8re=20les=20upload=20?=
 =?UTF-8?q?par=20morceaux=20(Content-Range).?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 Controller/UploadController.php | 28 ++++++++++++++++++++++++++--
 1 file changed, 26 insertions(+), 2 deletions(-)

diff --git a/Controller/UploadController.php b/Controller/UploadController.php
index 0ed88ce5..3e2e89b7 100644
--- a/Controller/UploadController.php
+++ b/Controller/UploadController.php
@@ -16,6 +16,7 @@ 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\HttpKernel\Exception\BadRequestHttpException;
 use Symfony\Component\HttpKernel\Exception\HttpException;
 use Symfony\Component\Routing\RouterInterface;
 
@@ -41,7 +42,6 @@ class UploadController extends Controller
      */
     protected $csrfProvider;
 
-
     /**
      *
      */
@@ -92,10 +92,34 @@ class UploadController extends Controller
     {
         $this->validateToken($request);
 
-        $stream = $file->open('wb+');
+        $range = $request->headers->get('Content-Range');
+        $final = true;
+
+        if($range) {
+            $matches = [];
+            if(!preg_match('@^bytes (\d+)-(\d+)/(\d+)$@', $range, $matches)) {
+                throw new BadRequestHttpException("Invalid Content-Range");
+            }
+            $start = intval($matches[1]);
+            $end   = intval($matches[2]);
+            $total = intval($matches[3]);
+
+            $stream = $file->open('ab');
+            $stream->seek($start);
+
+            $final = $end === ($total-1);
+
+        } else {
+            $stream = $file->open('wb');
+        }
+
         $stream->write($request->getContent());
         $stream->close();
 
+        if(!$final) {
+            return $this->createResponse(Response::HTTP_OK, 'Chunk received', ['range' => compact('start', 'end', 'total')]);
+        }
+
         $this->fileManager->completed($file);
         return $this->createResponse(
         );
-- 
GitLab