Chamilo is a learning management system focused on ease of use and accessibility
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
chamilo-lms/src/CoreBundle/Controller/AssetController.php

54 lines
1.7 KiB

<?php
/* For licensing terms, see /license.txt */
namespace Chamilo\CoreBundle\Controller;
use Chamilo\CoreBundle\Repository\AssetRepository;
use Chamilo\CoreBundle\Traits\ControllerTrait;
use Chamilo\CoreBundle\Traits\CourseControllerTrait;
use Chamilo\CoreBundle\Traits\ResourceControllerTrait;
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/assets")
*/
class AssetController
{
use CourseControllerTrait;
use ResourceControllerTrait;
use ControllerTrait;
/**
* @Route("/{category}/{path}", methods={"GET"}, requirements={"path"=".+"}, name="chamilo_core_asset_showfile")
*/
public function showFile($category, $path, AssetRepository $assetRepository)
{
$filePath = $category.'/'.$path;
$has = $assetRepository->getFileSystem()->has($filePath);
if ($has) {
$fileName = basename($filePath);
$stream = $assetRepository->getFileSystem()->readStream($filePath);
$response = new StreamedResponse(
function () use ($stream): void {
stream_copy_to_stream($stream, fopen('php://output', 'wb'));
}
);
$disposition = $response->headers->makeDisposition(
ResponseHeaderBag::DISPOSITION_INLINE,
$fileName
);
$response->headers->set('Content-Disposition', $disposition);
//$response->headers->set('Content-Type', $mimeType ?: 'application/octet-stream');
return $response;
}
throw new FileNotFoundException($path);
}
}