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

UploadController: gère les upload par morceaux (Content-Range).

Showing with 26 additions and 2 deletions
+26 -2
......@@ -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(
);
......
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