Commit d40c50ed authored by Guillaume Perréal's avatar Guillaume Perréal

UploadController: force les réponses en JSON et laisse Symfony gérer les exceptions.

parent 4c03c7d6
......@@ -33,6 +33,8 @@ class UploadController extends Controller
{
public const CSRF_INTENTION = 'uploaded_file';
protected const REQUEST_FORMAT = 'json';
/**
* @var FileManagerInterface
*/
......@@ -91,41 +93,38 @@ class UploadController extends Controller
*/
public function createAction(Request $request)
{
try {
$data = $request->request->get('file');
$token = $this->tokenStorage->getToken();
$file = $this->fileManager->create(
$data['name'],
$data['size'],
$data['type'],
$data['lastModified'] ?? null,
null !== $token ? $token->getUsername() : null,
$request->getClientIp()
);
$parameters = ['id' => $file->getId()];
$deleteUrl = $this->urlGenerator->generate('file_upload_delete', $parameters);
return $this->createResponse(
Response::HTTP_CREATED,
'New file created',
array_merge(
$file->toArray(),
[
'put_url' => $this->urlGenerator->generate('file_upload_put_content', $parameters),
'delete_type' => 'DELETE',
'delete_url' => $deleteUrl,
]
),
// On a pas de get pour l'instant, le DELETE et ce qui y ressemble le plus
['Location' => $deleteUrl]
);
} catch (\Exception $ex) {
return $this->createExceptionResponse($ex);
}
$request->setRequestFormat(self::REQUEST_FORMAT);
$data = $request->request->get('file');
$token = $this->tokenStorage->getToken();
$file = $this->fileManager->create(
$data['name'],
$data['size'],
$data['type'],
$data['lastModified'] ?? null,
$token !== null ? $token->getUsername() : null,
$request->getClientIp()
);
$parameters = ['id' => $file->getId()];
$deleteUrl = $this->urlGenerator->generate('file_upload_delete', $parameters);
return $this->createResponse(
Response::HTTP_CREATED,
'New file created',
array_merge(
$file->toArray(),
[
'put_url' => $this->urlGenerator->generate('file_upload_put_content', $parameters),
'delete_type' => 'DELETE',
'delete_url' => $deleteUrl,
]
),
// On a pas de get pour l'instant, le DELETE et ce qui y ressemble le plus
['Location' => $deleteUrl]
);
}
/**
......@@ -139,24 +138,21 @@ class UploadController extends Controller
*/
public function putContentAction(Request $request, UploadedFile $file)
{
try {
$this->validateCsrfToken($request);
[$offset, $maxlen, $complete] = $this->handleRangeHeader($request);
$request->setRequestFormat(self::REQUEST_FORMAT);
$this->validateCsrfToken($request);
// Demande un filehandle plutôt que charger le contenu en mémoire
$input = $request->getContent(true);
$file->copyFrom($input, $maxlen, $offset);
fclose($input);
[$offset, $maxlen, $complete] = $this->handleRangeHeader($request);
if ($complete) {
return $this->completeUpload($file);
}
// Demande un filehandle plutôt que charger le contenu en mémoire
$input = $request->getContent(true);
$file->copyFrom($input, $maxlen, $offset);
fclose($input);
return $this->createResponse(Response::HTTP_OK, 'Chunk received');
} catch (\Exception $ex) {
return $this->createExceptionResponse($ex);
if ($complete) {
return $this->completeUpload($file);
}
return $this->createResponse(Response::HTTP_OK, 'Chunk received');
}
/**
......@@ -166,9 +162,10 @@ class UploadController extends Controller
*/
protected function handleRangeHeader(Request $request)
{
if (null === $range = $request->headers->get('Content-Range')) {
if (!$request->headers->has('Content-Range')) {
return [0, PHP_INT_MAX, true];
}
$range = $request->headers->get('Content-Range');
$matches = [];
if (!preg_match('@^bytes (\d+)-(\d+)/(\d+)$@', $range, $matches)) {
......@@ -250,15 +247,13 @@ class UploadController extends Controller
*/
public function deleteAction(Request $request, UploadedFile $file)
{
try {
$this->validateCsrfToken($request);
$request->setRequestFormat(self::REQUEST_FORMAT);
$this->validateCsrfToken($request);
$this->fileManager->delete($file);
$this->fileManager->delete($file);
return $this->createResponse();
} catch (\Exception $ex) {
return $this->createExceptionResponse($ex);
}
return $this->createResponse();
}
/**
......@@ -291,26 +286,4 @@ class UploadController extends Controller
return $response;
}
/**
* @param \Exception $ex
*
* @return JsonResponse
*/
protected function createExceptionResponse(\Exception $ex)
{
return $this->createResponse(
$ex instanceof HttpException ? $ex->getStatusCode() : Response::HTTP_INTERNAL_SERVER_ERROR,
preg_replace("/[\n\r]+/", ' ', $ex->getMessage()),
[
'exception' => [
'class' => get_class($ex),
'file' => $ex->getFile(),
'line' => $ex->getLine(),
'code' => $ex->getCode(),
'trace' => $ex->getTrace(),
],
]
);
}
}
Markdown is supported
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