mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-20 22:53:49 +00:00
Feature: allow to convert to PDF from Chill and group action button on document
BREAKING CHANGE: avoid using the macro for download button. To keep the UI clean, use always the new "group of action buttons".
This commit is contained in:
108
src/Bundle/ChillWopiBundle/src/Controller/Convert.php
Normal file
108
src/Bundle/ChillWopiBundle/src/Controller/Convert.php
Normal file
@@ -0,0 +1,108 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\WopiBundle\Controller;
|
||||
|
||||
use Chill\DocStoreBundle\Entity\StoredObject;
|
||||
use Chill\DocStoreBundle\Service\StoredObjectManager;
|
||||
use Chill\DocStoreBundle\Service\StoredObjectManagerInterface;
|
||||
use Chill\MainBundle\Entity\User;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
||||
use Symfony\Component\Mime\Part\DataPart;
|
||||
use Symfony\Component\Mime\Part\Multipart\FormDataPart;
|
||||
use Symfony\Component\Security\Core\Security;
|
||||
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
use Symfony\Contracts\HttpClient\ResponseInterface;
|
||||
|
||||
class Convert
|
||||
{
|
||||
private const LOG_PREFIX = '[convert] ';
|
||||
|
||||
private string $collaboraDomain;
|
||||
|
||||
private HttpClientInterface $httpClient;
|
||||
|
||||
private LoggerInterface $logger;
|
||||
|
||||
private Security $security;
|
||||
|
||||
private StoredObjectManagerInterface $storedObjectManager;
|
||||
|
||||
/**
|
||||
* @param StoredObjectManager $storedObjectManager
|
||||
*/
|
||||
public function __construct(
|
||||
HttpClientInterface $httpClient,
|
||||
Security $security,
|
||||
StoredObjectManagerInterface $storedObjectManager,
|
||||
LoggerInterface $logger,
|
||||
ParameterBagInterface $parameters
|
||||
) {
|
||||
$this->httpClient = $httpClient;
|
||||
$this->security = $security;
|
||||
$this->storedObjectManager = $storedObjectManager;
|
||||
$this->logger = $logger;
|
||||
$this->collaboraDomain = $parameters->get('wopi')['server'];
|
||||
}
|
||||
|
||||
public function __invoke(StoredObject $storedObject): Response
|
||||
{
|
||||
if (!$this->security->getUser() instanceof User) {
|
||||
throw new AccessDeniedHttpException('User must be authenticated');
|
||||
}
|
||||
|
||||
$content = $this->storedObjectManager->read($storedObject);
|
||||
|
||||
try {
|
||||
$url = sprintf('%s/cool/convert-to/pdf', $this->collaboraDomain);
|
||||
$form = new FormDataPart([
|
||||
'data' => new DataPart($content, $storedObject->getUuid()->toString(), $storedObject->getType()),
|
||||
]);
|
||||
$response = $this->httpClient->request('POST', $url, [
|
||||
'headers' => $form->getPreparedHeaders()->toArray(),
|
||||
'body' => $form->bodyToString(),
|
||||
'timeout' => 10,
|
||||
]);
|
||||
|
||||
return new Response($response->getContent(), Response::HTTP_OK, [
|
||||
'Content-Type' => 'application/pdf',
|
||||
]);
|
||||
} catch (ClientExceptionInterface $exception) {
|
||||
return $this->onConversionFailed($url, $response);
|
||||
} catch (RedirectionExceptionInterface $e) {
|
||||
return $this->onConversionFailed($url, $response);
|
||||
} catch (ServerExceptionInterface $e) {
|
||||
return $this->onConversionFailed($url, $response);
|
||||
} catch (TransportExceptionInterface $e) {
|
||||
return $this->onConversionFailed($url, $response);
|
||||
}
|
||||
}
|
||||
|
||||
private function onConversionFailed(string $url, ResponseInterface $response): JsonResponse
|
||||
{
|
||||
$this->logger->error(self::LOG_PREFIX . ' could not convert document', [
|
||||
'response_status' => $response->getStatusCode(),
|
||||
'message' => $response->getContent(false),
|
||||
'server' => $this->collaboraDomain,
|
||||
'url' => $url,
|
||||
]);
|
||||
|
||||
return new JsonResponse(['message' => 'conversion failed : ' . $response->getContent(false)], Response::HTTP_SERVICE_UNAVAILABLE);
|
||||
}
|
||||
}
|
@@ -16,4 +16,8 @@ return static function (RoutingConfigurator $routes) {
|
||||
$routes
|
||||
->add('chill_wopi_file_edit', '/edit/{fileId}')
|
||||
->controller(Editor::class);
|
||||
|
||||
$routes
|
||||
->add('chill_wopi_object_convert', '/convert/{uuid}')
|
||||
->controller(\Chill\WopiBundle\Controller\Convert::class);
|
||||
};
|
||||
|
92
src/Bundle/ChillWopiBundle/tests/Controller/ConverTest.php
Normal file
92
src/Bundle/ChillWopiBundle/tests/Controller/ConverTest.php
Normal file
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\WopiBundle\Tests\Controller;
|
||||
|
||||
use Chill\DocStoreBundle\Entity\StoredObject;
|
||||
use Chill\DocStoreBundle\Service\StoredObjectManagerInterface;
|
||||
use Chill\MainBundle\Entity\User;
|
||||
use Chill\WopiBundle\Controller\Convert;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Prophecy\PhpUnit\ProphecyTrait;
|
||||
use Psr\Log\NullLogger;
|
||||
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
|
||||
use Symfony\Component\HttpClient\MockHttpClient;
|
||||
use Symfony\Component\HttpClient\Response\MockResponse;
|
||||
use Symfony\Component\Security\Core\Security;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @coversNothing
|
||||
*/
|
||||
final class ConverTest extends TestCase
|
||||
{
|
||||
use ProphecyTrait;
|
||||
|
||||
public function testConversionFailed(): void
|
||||
{
|
||||
$storedObject = (new StoredObject())->setType('application/vnd.oasis.opendocument.text');
|
||||
|
||||
$httpClient = new MockHttpClient([
|
||||
new MockResponse('not authorized', ['http_code' => 401]),
|
||||
], 'http://collabora:9980');
|
||||
|
||||
$security = $this->prophesize(Security::class);
|
||||
$security->getUser()->willReturn(new User());
|
||||
|
||||
$storeManager = $this->prophesize(StoredObjectManagerInterface::class);
|
||||
$storeManager->read($storedObject)->willReturn('content');
|
||||
|
||||
$parameterBag = new ParameterBag(['wopi' => ['server' => 'http://collabora:9980']]);
|
||||
|
||||
$convert = new Convert(
|
||||
$httpClient,
|
||||
$security->reveal(),
|
||||
$storeManager->reveal(),
|
||||
new NullLogger(),
|
||||
$parameterBag
|
||||
);
|
||||
|
||||
$response = $convert($storedObject);
|
||||
|
||||
$this->assertNotEquals(200, $response->getStatusCode());
|
||||
}
|
||||
|
||||
public function testEverythingWentFine(): void
|
||||
{
|
||||
$storedObject = (new StoredObject())->setType('application/vnd.oasis.opendocument.text');
|
||||
|
||||
$httpClient = new MockHttpClient([
|
||||
new MockResponse('1234', ['http_code' => 200]),
|
||||
], 'http://collabora:9980');
|
||||
|
||||
$security = $this->prophesize(Security::class);
|
||||
$security->getUser()->willReturn(new User());
|
||||
|
||||
$storeManager = $this->prophesize(StoredObjectManagerInterface::class);
|
||||
$storeManager->read($storedObject)->willReturn('content');
|
||||
|
||||
$parameterBag = new ParameterBag(['wopi' => ['server' => 'http://collabora:9980']]);
|
||||
|
||||
$convert = new Convert(
|
||||
$httpClient,
|
||||
$security->reveal(),
|
||||
$storeManager->reveal(),
|
||||
new NullLogger(),
|
||||
$parameterBag
|
||||
);
|
||||
|
||||
$response = $convert($storedObject);
|
||||
|
||||
$this->assertEquals(200, $response->getStatusCode());
|
||||
$this->assertEquals('1234', $response->getContent());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user