From 0957e8c44b01c70bfa31e1b11bf86066d23ed98f Mon Sep 17 00:00:00 2001 From: Julio Montoya Date: Sun, 27 Dec 2020 11:23:27 +0100 Subject: [PATCH] Use resource node repository --- src/CoreBundle/Controller/ResourceController.php | 15 ++++++++------- src/CoreBundle/Traits/ControllerTrait.php | 5 +++-- src/CoreBundle/Traits/ResourceControllerTrait.php | 8 +++++++- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/CoreBundle/Controller/ResourceController.php b/src/CoreBundle/Controller/ResourceController.php index 8788cff981..3470775db8 100644 --- a/src/CoreBundle/Controller/ResourceController.php +++ b/src/CoreBundle/Controller/ResourceController.php @@ -495,7 +495,7 @@ class ResourceController extends AbstractResourceController implements CourseCon $em = $this->getDoctrine(); /** @var ResourceNode $resourceNode */ - $resourceNode = $em->getRepository(ResourceNode::class)->find($id); + $resourceNode = $this->getResourceNodeRepository()->find($id); if (null === $resourceNode) { throw new FileNotFoundException('Resource not found'); @@ -536,7 +536,7 @@ class ResourceController extends AbstractResourceController implements CourseCon $filter = $request->get('filter'); // See filters definitions in /config/services.yml $em = $this->getDoctrine(); /** @var ResourceNode $resourceNode */ - $resourceNode = $em->getRepository(ResourceNode::class)->find($id); + $resourceNode = $this->getResourceNodeRepository()->find($id); if (null === $resourceNode) { throw new FileNotFoundException('Resource not found'); @@ -563,7 +563,7 @@ class ResourceController extends AbstractResourceController implements CourseCon $id = $request->get('id'); $em = $this->getDoctrine(); /** @var ResourceNode $resourceNode */ - $resourceNode = $em->getRepository(ResourceNode::class)->find($id); + $resourceNode = $this->getResourceNodeRepository()->find($id); if (null === $resourceNode) { throw new FileNotFoundException('Resource not found'); @@ -591,7 +591,7 @@ class ResourceController extends AbstractResourceController implements CourseCon $em = $this->getDoctrine(); /** @var ResourceNode $resourceNode */ - $resourceNode = $em->getRepository(ResourceNode::class)->find($id); + $resourceNode = $this->getResourceNodeRepository()->find($id); if (null === $resourceNode) { throw new FileNotFoundException('Resource not found'); @@ -701,7 +701,7 @@ class ResourceController extends AbstractResourceController implements CourseCon $this->trans('Unauthorised view access to resource') ); - $repo = $this->getRepositoryFromRequest($request); + //$repo = $this->getRepositoryFromRequest($request); $resourceFile = $resourceNode->getResourceFile(); if (!$resourceFile) { @@ -710,6 +710,7 @@ class ResourceController extends AbstractResourceController implements CourseCon $fileName = $resourceNode->getResourceFile()->getOriginalName(); $mimeType = $resourceFile->getMimeType(); + $resourceNodeRepo = $this->getResourceNodeRepository(); switch ($mode) { case 'download': @@ -737,7 +738,7 @@ class ResourceController extends AbstractResourceController implements CourseCon $params['crop'] = $crop; } - $fileName = $repo->getResourceNodeRepository()->getFilename($resourceFile); + $fileName = $resourceNodeRepo->getFilename($resourceFile); return $server->getImageResponse($fileName, $params); } @@ -745,7 +746,7 @@ class ResourceController extends AbstractResourceController implements CourseCon break; } - $stream = $repo->getResourceNodeFileStream($resourceNode); + $stream = $resourceNodeRepo->getResourceNodeFileStream($resourceNode); $response = new StreamedResponse( function () use ($stream): void { diff --git a/src/CoreBundle/Traits/ControllerTrait.php b/src/CoreBundle/Traits/ControllerTrait.php index 5a9d868796..43fe0f401b 100644 --- a/src/CoreBundle/Traits/ControllerTrait.php +++ b/src/CoreBundle/Traits/ControllerTrait.php @@ -8,6 +8,7 @@ use Chamilo\CoreBundle\Component\Utils\Glide; use Chamilo\CoreBundle\Manager\SettingsManager; use Chamilo\CoreBundle\Repository\Node\IllustrationRepository; use Chamilo\CoreBundle\Repository\ResourceFactory; +use Chamilo\CoreBundle\Repository\ResourceNodeRepository; use Chamilo\CourseBundle\Repository\CAnnouncementAttachmentRepository; use Chamilo\CourseBundle\Repository\CAnnouncementRepository; use Chamilo\CourseBundle\Repository\CAttendanceRepository; @@ -32,7 +33,6 @@ trait ControllerTrait { $services = parent::getSubscribedServices(); $services['translator'] = TranslatorInterface::class; - //$services['resource_factory'] = ResourceFactory::class; $services['glide'] = Glide::class; $services['chamilo.settings.manager'] = SettingsManager::class; $services['chamilo_settings.form_factory.settings'] = SettingsFormFactory::class; @@ -41,7 +41,8 @@ trait ControllerTrait The following classes are needed in order to load the resources files when using the /r/ path For example: http://my.chamilomaster.net/r/agenda/event_attachments/96/download?cid=1&sid=0&gid=0 */ - + $services[] = ResourceFactory::class; + $services[] = ResourceNodeRepository::class; $services[] = CAnnouncementRepository::class; $services[] = CAnnouncementAttachmentRepository::class; $services[] = CAttendanceRepository::class; diff --git a/src/CoreBundle/Traits/ResourceControllerTrait.php b/src/CoreBundle/Traits/ResourceControllerTrait.php index cf6365cfa3..1d4ee7db5c 100644 --- a/src/CoreBundle/Traits/ResourceControllerTrait.php +++ b/src/CoreBundle/Traits/ResourceControllerTrait.php @@ -9,6 +9,7 @@ use Chamilo\CoreBundle\Entity\ResourceInterface; use Chamilo\CoreBundle\Entity\ResourceNode; use Chamilo\CoreBundle\Entity\User; use Chamilo\CoreBundle\Repository\ResourceFactory; +use Chamilo\CoreBundle\Repository\ResourceNodeRepository; use Chamilo\CoreBundle\Repository\ResourceRepository; use Doctrine\ORM\EntityNotFoundException; use Symfony\Component\HttpFoundation\Request; @@ -24,9 +25,14 @@ trait ResourceControllerTrait return $this->getRepository($tool, $type); } + public function getResourceNodeRepository(): ResourceNodeRepository + { + return $this->container->get(ResourceNodeRepository::class); + } + public function getResourceRepositoryFactory(): ResourceFactory { - return $this->container->get('resource_factory'); + return $this->container->get(ResourceFactory::class); } public function getRepository($tool, $type): ResourceRepository