Skip to content

Commit da2c4a8

Browse files
committed
Merge pull request #1488 from claroline/stream-response-fix-master
Avoid sending response twice and switch to binary file response
2 parents 0ebb6a5 + defd723 commit da2c4a8

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

Controller/FileController.php

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use Claroline\CoreBundle\Form\FileType;
1515
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
1616
use Symfony\Component\HttpFoundation\JsonResponse;
17-
use Symfony\Component\HttpFoundation\StreamedResponse;
17+
use Symfony\Component\HttpFoundation\BinaryFileResponse;
1818
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
1919
use Symfony\Component\HttpFoundation\Response;
2020
use Claroline\CoreBundle\Entity\Resource\File;
@@ -42,23 +42,19 @@ public function streamMediaAction(ResourceNode $node)
4242
{
4343
$collection = new ResourceCollection(array($node));
4444
$this->checkAccess('OPEN', $collection);
45-
$file = $this->get('claroline.manager.resource_manager')->getResourceFromNode($node);
46-
$path = $this->container->getParameter('claroline.param.files_directory') . DIRECTORY_SEPARATOR
47-
. $file->getHashName();
4845

49-
$response = new StreamedResponse();
50-
$response->setCallBack(
51-
function () use ($path) {
52-
readfile($path);
53-
}
54-
);
55-
46+
// free the session as soon as possible
47+
// see https://github.com/claroline/CoreBundle/commit/7cee6de85bbc9448f86eb98af2abb1cb072c7b6b
5648
$this->get('session')->save();
5749

50+
$file = $this->get('claroline.manager.resource_manager')->getResourceFromNode($node);
51+
$path = $this->container->getParameter('claroline.param.files_directory').DIRECTORY_SEPARATOR
52+
.$file->getHashName();
53+
54+
$response = new BinaryFileResponse($path);
5855
$response->headers->set('Content-Type', $node->getMimeType());
59-
$response->send();
6056

61-
return new Response();
57+
return $response;
6258
}
6359

6460
/**

0 commit comments

Comments
 (0)