diff --git a/src/Bundle/ChillWopiBundle/src/Controller/Test.php b/src/Bundle/ChillWopiBundle/src/Controller/Test.php index ed7cf9c00..ebbe88002 100644 --- a/src/Bundle/ChillWopiBundle/src/Controller/Test.php +++ b/src/Bundle/ChillWopiBundle/src/Controller/Test.php @@ -9,22 +9,82 @@ declare(strict_types=1); namespace Chill\WopiBundle\Controller; -use Chill\WopiBundle\Service\OpenStack\OpenStackClientInterface; +use ChampsLibres\WopiLib\Configuration\WopiConfigurationInterface; +use ChampsLibres\WopiLib\Discovery\WopiDiscoveryInterface; +use Chill\DocStoreBundle\Repository\StoredObjectRepository; +use Chill\WopiBundle\Service\Controller\ResponderInterface; +use Exception; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\Routing\Generator\UrlGeneratorInterface; +use Symfony\Component\Security\Core\Security; final class Test { - private OpenStackClientInterface $client; + private StoredObjectRepository $storedObjectRepository; - public function __construct(OpenStackClientInterface $client) - { - $this->client = $client; + private WopiDiscoveryInterface $wopiDiscovery; + + private WopiConfigurationInterface $wopiConfiguration; + + private ResponderInterface $responder; + + private Security $security; + + public function __construct( + StoredObjectRepository $storedObjectRepository, + WopiConfigurationInterface $wopiConfiguration, + WopiDiscoveryInterface $wopiDiscovery, + ResponderInterface $responder, + Security $security + ) { + $this->storedObjectRepository = $storedObjectRepository; + $this->wopiConfiguration = $wopiConfiguration; + $this->wopiDiscovery = $wopiDiscovery; + $this->responder = $responder; + $this->security = $security; } - public function __invoke() + public function __invoke(string $fileId): Response { - dump($this->client); + $configuration = $this->wopiConfiguration->jsonSerialize(); + + $storedObject = $this->storedObjectRepository->findOneBy(['filename' => $fileId]); + + if (null === $storedObject) { + throw new Exception(sprintf('Unable to find object named %s', $fileId)); + } + + if ([] === $discoverExtension = $this->wopiDiscovery->discoverMimeType($storedObject->getType())) { + throw new Exception(sprintf('Unable to find mime type %s', $storedObject->getType())); + } + + $configuration['access_token'] = $this->security->getUser()->getUsername(); + + $configuration['server'] = $this + ->psr17 + ->createUri($discoverExtension[0]['urlsrc']) + ->withQuery( + http_build_query( + [ + 'WOPISrc' => $this + ->router + ->generate( + 'checkFileInfo', + [ + 'fileId' => $storedObject->getId(), + ], + UrlGeneratorInterface::ABSOLUTE_URL + ), + ] + ) + ); + + return $this + ->responder + ->render( + '@WopiBundle/Editor/page.html.twig', + $configuration + ); - return new Response('fobar'); } } diff --git a/src/Bundle/ChillWopiBundle/src/Resources/config/routes/routes.php b/src/Bundle/ChillWopiBundle/src/Resources/config/routes/routes.php index 4bf8dbf94..e0f3447aa 100644 --- a/src/Bundle/ChillWopiBundle/src/Resources/config/routes/routes.php +++ b/src/Bundle/ChillWopiBundle/src/Resources/config/routes/routes.php @@ -12,6 +12,6 @@ use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator; return static function (RoutingConfigurator $routes) { $routes - ->add('testtest', '/test') + ->add('testtest', '/edit/{fileId}') ->controller(Test::class); }; diff --git a/src/Bundle/ChillWopiBundle/src/Service/Controller/Responder.php b/src/Bundle/ChillWopiBundle/src/Service/Controller/Responder.php new file mode 100644 index 000000000..bc26be62d --- /dev/null +++ b/src/Bundle/ChillWopiBundle/src/Service/Controller/Responder.php @@ -0,0 +1,101 @@ +twig = $twig; + $this->urlGenerator = $urlGenerator; + $this->serializer = $serializer; + } + + public function file( + $file, + ?string $filename = null, + string $disposition = ResponseHeaderBag::DISPOSITION_ATTACHMENT + ): BinaryFileResponse { + $response = new BinaryFileResponse($file); + + $filename ??= $response->getFile()->getFilename(); + $response->setContentDisposition($disposition, $filename); + + return $response; + } + + public function json( + $data, + int $status = 200, + array $headers = [], + array $context = [] + ): JsonResponse { + return new JsonResponse( + $this + ->serializer + ->serialize( + $data, + 'json', + array_merge( + [ + 'json_encode_options' => JsonResponse::DEFAULT_ENCODING_OPTIONS, + ], + $context + ) + ), + $status, + $headers, + true + ); + } + + public function redirect(string $url, int $status = 302, array $headers = []): RedirectResponse + { + return new RedirectResponse($url, $status, $headers); + } + + public function redirectToRoute( + string $route, + array $parameters = [], + int $status = 302, + array $headers = [] + ): RedirectResponse { + return $this->redirect($this->urlGenerator->generate($route, $parameters), $status, $headers); + } + + public function render(string $template, array $context = [], int $status = 200, array $headers = []): Response + { + $response = new Response($this->twig->render($template, $context), $status, $headers); + + if (!$response->headers->has('Content-Type')) { + $response->headers->set('Content-Type', 'text/html; charset=UTF-8'); + } + + return $response; + } +} diff --git a/src/Bundle/ChillWopiBundle/src/Service/Controller/ResponderInterface.php b/src/Bundle/ChillWopiBundle/src/Service/Controller/ResponderInterface.php new file mode 100644 index 000000000..712b3cd59 --- /dev/null +++ b/src/Bundle/ChillWopiBundle/src/Service/Controller/ResponderInterface.php @@ -0,0 +1,74 @@ +|string> $headers + * @param array $context + */ + public function json( + mixed $data, + int $status = 200, + array $headers = [], + array $context = [] + ): JsonResponse; + + /** + * Returns a RedirectResponse to the given URL. + * + * @param array|string> $headers + */ + public function redirect(string $url, int $status = 302, array $headers = []): RedirectResponse; + + /** + * Returns a RedirectResponse to the given route with the given parameters. + * + * @param array $parameters + * @param array> $headers + */ + public function redirectToRoute( + string $route, + array $parameters = [], + int $status = 302, + array $headers = [] + ): RedirectResponse; + + /** + * Render the given twig template and return an HTML response. + * + * @param array|string> $headers + * + * @throws TwigError + */ + public function render(string $template, array $context = [], int $status = 200, array $headers = []): Response; +}