parent
8578c1f803
commit
3a76aa3574
@ -1,142 +0,0 @@ |
||||
<?php |
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
namespace Chamilo\CoreBundle\Controller; |
||||
|
||||
use League\Flysystem\Adapter\Local; |
||||
use League\Flysystem\Filesystem; |
||||
use Symfony\Component\HttpFoundation\BinaryFileResponse; |
||||
use Symfony\Component\HttpFoundation\File\MimeType\FileinfoMimeTypeGuesser; |
||||
use Symfony\Component\HttpFoundation\Request; |
||||
use Symfony\Component\HttpFoundation\Response; |
||||
use Symfony\Component\HttpFoundation\ResponseHeaderBag; |
||||
use Symfony\Component\Routing\Annotation\Route; |
||||
|
||||
/** |
||||
* Class ResourceController. |
||||
* |
||||
* @author Julio Montoya <gugli100@gmail.com>. |
||||
* |
||||
* @Route("/resource") |
||||
*/ |
||||
class ResourceDownloadController extends BaseController |
||||
{ |
||||
/** |
||||
* Upload form. |
||||
* |
||||
* @Route("/upload/{type}/{id}", name="resource_upload", methods={"GET", "POST"}, options={"expose"=true}) |
||||
* |
||||
* @return Response |
||||
*/ |
||||
public function showUploadFormAction($type, $id): Response |
||||
{ |
||||
//$helper = $this->container->get('oneup_uploader.templating.uploader_helper'); |
||||
//$endpoint = $helper->endpoint('courses'); |
||||
return $this->render( |
||||
'@ChamiloCore/Resource/upload.html.twig', |
||||
[ |
||||
'identifier' => $id, |
||||
'type' => $type, |
||||
] |
||||
); |
||||
} |
||||
|
||||
/** |
||||
* Downloads the file courses/MATHS/document/file.jpg to the user. |
||||
* |
||||
* @Route("/download/{course}/", name="resource_download", methods={"GET"}, options={"expose"=true}) |
||||
* |
||||
* @todo check permissions |
||||
* |
||||
* @param string $course |
||||
* |
||||
* @return \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||||
*/ |
||||
public function downloadFileAction(Request $request, $course) |
||||
{ |
||||
try { |
||||
/** @var Filesystem $fs */ |
||||
$fs = $this->container->get('oneup_flysystem.resources_filesystem'); |
||||
$file = $request->get('file'); |
||||
|
||||
$path = $course.'/document/'.$file; |
||||
|
||||
// Has folder |
||||
if (!$fs->has($course)) { |
||||
return $this->abort(); |
||||
} |
||||
|
||||
// Has file |
||||
if (!$fs->has($path)) { |
||||
return $this->abort(); |
||||
} |
||||
|
||||
/** @var Local $adapter */ |
||||
$adapter = $fs->getAdapter(); |
||||
$filePath = $adapter->getPathPrefix().$path; |
||||
|
||||
$response = new BinaryFileResponse($filePath); |
||||
|
||||
// To generate a file download, you need the mimetype of the file |
||||
$mimeTypeGuesser = new FileinfoMimeTypeGuesser(); |
||||
|
||||
// Set the mimetype with the guesser or manually |
||||
if ($mimeTypeGuesser->isSupported()) { |
||||
// Guess the mimetype of the file according to the extension of the file |
||||
$response->headers->set('Content-Type', $mimeTypeGuesser->guess($filePath)); |
||||
} else { |
||||
// Set the mimetype of the file manually, in this case for a text file is text/plain |
||||
$response->headers->set('Content-Type', 'text/plain'); |
||||
} |
||||
|
||||
$response->setContentDisposition( |
||||
ResponseHeaderBag::DISPOSITION_ATTACHMENT, |
||||
basename($filePath) |
||||
); |
||||
|
||||
return $response; |
||||
} catch (\InvalidArgumentException $e) { |
||||
return $this->abort(); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Gets a document in browser courses/MATHS/document/file.jpg to the user. |
||||
* |
||||
* @Route("/get/{course}/", name="resource_get", methods={"GET"}, options={"expose"=true}) |
||||
* |
||||
* @todo check permissions |
||||
* |
||||
* @param string $course |
||||
* |
||||
* @return \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||||
*/ |
||||
public function getFileAction(Request $request, $course) |
||||
{ |
||||
try { |
||||
/** @var Filesystem $fs */ |
||||
$fs = $this->container->get('oneup_flysystem.resources_filesystem'); |
||||
$file = $request->get('file'); |
||||
|
||||
$path = $course.'/document/'.$file; |
||||
|
||||
// Has folder |
||||
if (!$fs->has($course)) { |
||||
return $this->abort(); |
||||
} |
||||
|
||||
// Has file |
||||
if (!$fs->has($path)) { |
||||
return $this->abort(); |
||||
} |
||||
|
||||
/** @var Local $adapter */ |
||||
$adapter = $fs->getAdapter(); |
||||
$filePath = $adapter->getPathPrefix().$path; |
||||
|
||||
return $this->file($filePath, null, ResponseHeaderBag::DISPOSITION_INLINE); |
||||
} catch (\InvalidArgumentException $e) { |
||||
return $this->abort(); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,26 @@ |
||||
{% extends "@ChamiloTheme/Layout/layout_one_col.html.twig" %} |
||||
|
||||
{% block content %} |
||||
{% autoescape false %} |
||||
<h3>{{ resource_node.name }} </h3> |
||||
|
||||
Type: {{ resource_node.resourceType }} <br /> |
||||
|
||||
{% if resource_node.resourceFile %} |
||||
File: |
||||
<a href="{{ url('resources_get_file', {id: resource_node.id } ) }}"> |
||||
{{ resource_node.resourceFile }} |
||||
</a> |
||||
<br /> |
||||
Size: {{ resource_node.resourceFile.size }} |
||||
<br /><br /> |
||||
{% endif %} |
||||
|
||||
Updated at: {{ resource_node.updatedAt | date_to_time_ago }} |
||||
<br /> |
||||
Created at: {{ resource_node.createdAt | date_to_time_ago }}<br /> |
||||
<br /> |
||||
Creator: {{ resource_node.creator }} |
||||
|
||||
{% endautoescape %} |
||||
{% endblock %} |
Loading…
Reference in new issue