Let ConvertController return a pdf file directly without trying to converting it

Those changes improve performance when the file is already in pdf format.
This commit is contained in:
Julien Fastré 2024-11-14 08:49:26 +01:00
parent f90fae4e14
commit 9b661c3b8f
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
2 changed files with 13 additions and 0 deletions

View File

@ -12,6 +12,7 @@ declare(strict_types=1);
namespace Chill\WopiBundle\Controller; namespace Chill\WopiBundle\Controller;
use Chill\DocStoreBundle\Entity\StoredObject; use Chill\DocStoreBundle\Entity\StoredObject;
use Chill\DocStoreBundle\Security\Authorization\StoredObjectRoleEnum;
use Chill\DocStoreBundle\Service\StoredObjectManager; use Chill\DocStoreBundle\Service\StoredObjectManager;
use Chill\DocStoreBundle\Service\StoredObjectManagerInterface; use Chill\DocStoreBundle\Service\StoredObjectManagerInterface;
use Chill\WopiBundle\Service\WopiConverter; use Chill\WopiBundle\Service\WopiConverter;
@ -41,7 +42,16 @@ class ConvertController
throw new AccessDeniedHttpException('User must be authenticated'); throw new AccessDeniedHttpException('User must be authenticated');
} }
if (!$this->security->isGranted(StoredObjectRoleEnum::SEE->value, $storedObject)) {
throw new AccessDeniedHttpException('not allowed to see this document');
}
$content = $this->storedObjectManager->read($storedObject); $content = $this->storedObjectManager->read($storedObject);
if ('application/pdf' === $storedObject->getType()) {
return new Response($content, Response::HTTP_OK, ['Content-Type' => 'application/pdf']);
}
$lang = $request->getLocale(); $lang = $request->getLocale();
try { try {

View File

@ -12,6 +12,7 @@ declare(strict_types=1);
namespace Chill\WopiBundle\Tests\Controller; namespace Chill\WopiBundle\Tests\Controller;
use Chill\DocStoreBundle\Entity\StoredObject; use Chill\DocStoreBundle\Entity\StoredObject;
use Chill\DocStoreBundle\Security\Authorization\StoredObjectRoleEnum;
use Chill\DocStoreBundle\Service\StoredObjectManagerInterface; use Chill\DocStoreBundle\Service\StoredObjectManagerInterface;
use Chill\WopiBundle\Controller\ConvertController; use Chill\WopiBundle\Controller\ConvertController;
use Chill\WopiBundle\Service\WopiConverter; use Chill\WopiBundle\Service\WopiConverter;
@ -37,6 +38,7 @@ final class ConvertControllerTest extends TestCase
$security = $this->prophesize(Security::class); $security = $this->prophesize(Security::class);
$security->isGranted('ROLE_USER')->willReturn(true); $security->isGranted('ROLE_USER')->willReturn(true);
$security->isGranted(StoredObjectRoleEnum::SEE->value, $storedObject)->willReturn(true);
$storeManager = $this->prophesize(StoredObjectManagerInterface::class); $storeManager = $this->prophesize(StoredObjectManagerInterface::class);
$storeManager->read($storedObject)->willReturn('content'); $storeManager->read($storedObject)->willReturn('content');
@ -67,6 +69,7 @@ final class ConvertControllerTest extends TestCase
$security = $this->prophesize(Security::class); $security = $this->prophesize(Security::class);
$security->isGranted('ROLE_USER')->willReturn(true); $security->isGranted('ROLE_USER')->willReturn(true);
$security->isGranted(StoredObjectRoleEnum::SEE->value, $storedObject)->willReturn(true);
$storeManager = $this->prophesize(StoredObjectManagerInterface::class); $storeManager = $this->prophesize(StoredObjectManagerInterface::class);
$storeManager->read($storedObject)->willReturn('content'); $storeManager->read($storedObject)->willReturn('content');