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

UploadController: casse putContentAction en deux sous-méthodes.

Showing with 51 additions and 27 deletions
+51 -27
...@@ -102,47 +102,71 @@ class UploadController extends Controller ...@@ -102,47 +102,71 @@ class UploadController extends Controller
*/ */
public function putContentAction(Request $request, UploadedFile $file) public function putContentAction(Request $request, UploadedFile $file)
{ {
$this->validateCsrfToken($request); $this->validateCsrfToken($request);
if(null !== $range = $request->headers->get('Content-Range', null)) { list($offset, $maxlen, $complete) = $this->handleRangeHeader($request);
$matches = [];
if(!preg_match('@^bytes (\d+)-(\d+)/(\d+)$@', $range, $matches)) {
throw new BadRequestHttpException("Invalid Content-Range");
}
$start = intval($matches[1]); // Demande un filehandle plutôt que charger le contenu en mémoire
$end = intval($matches[2]); $input = $request->getContent(true);
$total = intval($matches[3]); $file->copyFrom($input, $maxlen, $offset);
fclose($input);
if($start < 0 || $start >= $end || $end >= $total) { if($complete) {
throw new HttpException(Response::HTTP_REQUESTED_RANGE_NOT_SATISFIABLE); return $this->completeUpload($file);
} }
$offset = $start; return $this->createResponse(Response::HTTP_OK, 'Chunk received');
$maxlen = 1 + ($end - $start);
$complete = $end === ($total-1);
} else {
$offset = 0;
$maxlen = PHP_INT_MAX;
$complete = true;
} }
// Demande un filehandle plutôt que charger le contenu en mémoire /**
$input = $request->getContent(true); *
$file->copyFrom($input, $maxlen, $offset); * @param Request $request
fclose($input); * @return array
*/
protected function handleRangeHeader(Request $request)
{
if(!$complete) { if(null === $range = $request->headers->get('Content-Range', null)) {
return $this->createResponse(Response::HTTP_OK, 'Chunk received'); return [0, PHP_INT_MAX, true];
}
$matches = [];
if(!preg_match('@^bytes (\d+)-(\d+)/(\d+)$@', $range, $matches)) {
throw new BadRequestHttpException("Invalid Content-Range");
} }
$this->fileManager->completed($file); $start = intval($matches[1]);
$end = intval($matches[2]);
$total = intval($matches[3]);
if($start < 0 || $start >= $end || $end >= $total) {
throw new HttpException(Response::HTTP_REQUESTED_RANGE_NOT_SATISFIABLE);
}
return [$start, 1 + ($end - $start), $end === ($total-1)];
}
/**
*
* @param UploadedFileInterface $file
* @return Response
*/
protected function completeUpload(UploadedFileInterface $file)
{
$status = Response::HTTP_OK;
$message = 'File uploaded';
try {
$this->fileManager->completed($file);
} catch(RejectedFileException $ex) {
$status = Response::HTTP_FORBIDDEN;
$message = 'File rejected: '.$ex->getMessage();
}
$parameters = ['id' => $file->getId()]; $parameters = ['id' => $file->getId()];
return $this->createResponse( return $this->createResponse(
Response::HTTP_OK, $status,
'File uploaded', $message,
[ [
'files' => [ 'files' => [
array_merge( array_merge(
......
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