From cefc4096c518ec1bcd1921f0322c77c27f25997d Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Tue, 17 Aug 2021 14:33:28 +0200 Subject: [PATCH] refactor: Update ChillWopiBundle - Work in progress. --- .../src/Resources/config/services.php | 2 - .../src/Service/OpenStack/Client.php | 1 + .../OpenstackWopi.php => Wopi/ChillWopi.php} | 108 ++++++++---------- 3 files changed, 46 insertions(+), 65 deletions(-) rename src/Bundle/ChillWopiBundle/src/Service/{WOPI/OpenstackWopi.php => Wopi/ChillWopi.php} (77%) diff --git a/src/Bundle/ChillWopiBundle/src/Resources/config/services.php b/src/Bundle/ChillWopiBundle/src/Resources/config/services.php index 98cd4f002..11dd44233 100644 --- a/src/Bundle/ChillWopiBundle/src/Resources/config/services.php +++ b/src/Bundle/ChillWopiBundle/src/Resources/config/services.php @@ -9,8 +9,6 @@ declare(strict_types=1); namespace Symfony\Component\DependencyInjection\Loader\Configurator; -use OpenStack\OpenStack; - return static function (ContainerConfigurator $container) { $services = $container ->services(); diff --git a/src/Bundle/ChillWopiBundle/src/Service/OpenStack/Client.php b/src/Bundle/ChillWopiBundle/src/Service/OpenStack/Client.php index 29cda11dd..faf612bfb 100644 --- a/src/Bundle/ChillWopiBundle/src/Service/OpenStack/Client.php +++ b/src/Bundle/ChillWopiBundle/src/Service/OpenStack/Client.php @@ -7,6 +7,7 @@ namespace Chill\WopiBundle\Service\OpenStack; use OpenStack\OpenStack; use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; +// TODO: Remove extends - Ugly. final class Client extends OpenStack implements OpenStackClientInterface { private ParameterBagInterface $parameterBag; diff --git a/src/Bundle/ChillWopiBundle/src/Service/WOPI/OpenstackWopi.php b/src/Bundle/ChillWopiBundle/src/Service/Wopi/ChillWopi.php similarity index 77% rename from src/Bundle/ChillWopiBundle/src/Service/WOPI/OpenstackWopi.php rename to src/Bundle/ChillWopiBundle/src/Service/Wopi/ChillWopi.php index 852882f3a..938f62577 100644 --- a/src/Bundle/ChillWopiBundle/src/Service/WOPI/OpenstackWopi.php +++ b/src/Bundle/ChillWopiBundle/src/Service/Wopi/ChillWopi.php @@ -7,36 +7,42 @@ declare(strict_types=1); -namespace Chill\WopiBundle\Service; +namespace Chill\WopiBundle\Service\Wopi; +use ChampsLibres\AsyncUploaderBundle\TempUrl\TempUrlGeneratorInterface; use ChampsLibres\WopiLib\Discovery\WopiDiscoveryInterface; use ChampsLibres\WopiLib\WopiInterface; +use Chill\DocStoreBundle\Repository\StoredObjectRepository; +use Exception; use loophp\psr17\Psr17Interface; +use Psr\Http\Client\ClientInterface; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; -use Symfony\Component\Filesystem\Filesystem; -use Symfony\Component\HttpKernel\KernelInterface; -final class OpenstackWopi implements WopiInterface +final class ChillWopi implements WopiInterface { - private string $filesRepository; - - private Filesystem $fs; - - private KernelInterface $kernel; - private Psr17Interface $psr17; private WopiDiscoveryInterface $wopiDiscovery; - public function __construct(Psr17Interface $psr17, KernelInterface $kernel, WopiDiscoveryInterface $wopiDiscovery) - { - $this->kernel = $kernel; + private StoredObjectRepository $storedObjectRepository; + + private ClientInterface $httpClient; + + private TempUrlGeneratorInterface $tempUrlGeneratorInterface; + + public function __construct( + Psr17Interface $psr17, + WopiDiscoveryInterface $wopiDiscovery, + StoredObjectRepository $storedObjectRepository, + ClientInterface $httpClient, + TempUrlGeneratorInterface $tempUrlGeneratorInterface + ) { $this->psr17 = $psr17; $this->wopiDiscovery = $wopiDiscovery; - $this->fs = new Filesystem(); - $this->filesRepository = sprintf('%s/files', $this->kernel->getCacheDir()); - $this->fs->mkdir($this->filesRepository); + $this->storedObjectRepository = $storedObjectRepository; + $this->httpClient = $httpClient; + $this->tempUrlGeneratorInterface = $tempUrlGeneratorInterface; } public function checkFileInfo( @@ -44,19 +50,10 @@ final class OpenstackWopi implements WopiInterface ?string $accessToken, RequestInterface $request ): ResponseInterface { - $filepath = sprintf( - '%s/%s', - $this->filesRepository, - $fileId - ); + $storedObject = $this->storedObjectRepository->findOneBy(['filename' => $fileId]); + $mimeType = $storedObject->getType(); - if (!$this->fs->exists($filepath)) { - $this->fs->touch($filepath); - } - - $filepathInfo = pathinfo($filepath); - - if (false !== current($this->wopiDiscovery->discoverExtension($filepathInfo['extension']))) { + if (false !== current($this->wopiDiscovery->discoverMimeType($mimeType))) { // TODO Exception. } @@ -66,9 +63,9 @@ final class OpenstackWopi implements WopiInterface ->withHeader('Content-Type', 'application/json') ->withBody($this->psr17->createStream((string) json_encode( [ - 'BaseFileName' => $filepathInfo['basename'], + 'BaseFileName' => $storedObject->getFilename(), 'OwnerId' => uniqid(), - 'Size' => filesize($filepath), + 'Size' => 0, 'UserId' => uniqid(), 'Version' => 'v' . uniqid(), 'ReadOnly' => false, @@ -77,7 +74,7 @@ final class OpenstackWopi implements WopiInterface 'SupportsLocks' => true, 'UserFriendlyName' => 'User Name ' . uniqid(), 'UserExtraInfo' => [], - 'LastModifiedTime' => date('Y-m-d\TH:i:s.u\Z', filemtime($filepath)), + 'LastModifiedTime' => date('Y-m-d\TH:i:s.u\Z', $storedObject->getCreationDate()), 'CloseButtonClosesWindow' => true, 'EnableInsertRemoteImage' => true, 'EnableShare' => true, @@ -105,11 +102,13 @@ final class OpenstackWopi implements WopiInterface public function getFile(string $fileId, ?string $accessToken, RequestInterface $request): ResponseInterface { - $filepath = sprintf( - '%s/%s', - $this->filesRepository, - $fileId - ); + $storedObject = $this->storedObjectRepository->findOneBy(['filename' => $fileId]); + + // TODO: Add strict typing in champs-libres/async-uploader-bundle + /** @var StdClass $object */ + $object = $this->tempUrlGeneratorInterface->generate('GET', $storedObject->getFilename()); + + $response = $this->httpClient->sendRequest($this->psr17->createRequest('GET', $object->url)); return $this ->psr17 @@ -120,9 +119,9 @@ final class OpenstackWopi implements WopiInterface ) ->withHeader( 'Content-Disposition', - sprintf('attachment; filename=%s', basename($filepath)) + sprintf('attachment; filename=%s', $storedObject->getFilename()) ) - ->withBody($this->psr17->createStreamFromFile($filepath)); + ->withBody($response->getBody()); } public function getLock(string $fileId, ?string $accessToken, RequestInterface $request): ResponseInterface @@ -196,34 +195,17 @@ final class OpenstackWopi implements WopiInterface string $xWopiEditors, RequestInterface $request ): ResponseInterface { - $filepath = sprintf( - '%s/%s', - $this->filesRepository, - $fileId - ); + $storedObject = $this->storedObjectRepository->findOneBy(['filename' => $fileId]); - $lockFilepath = $this->getLockFilepath($fileId); + // TODO: Add strict typing in champs-libres/async-uploader-bundle + /** @var StdClass $object */ + $object = $this->tempUrlGeneratorInterface->generate('PUT', $storedObject->getFilename()); - if ($this->fs->exists($lockFilepath)) { - $previousLockData = json_decode(file_get_contents($lockFilepath)); + $response = $this->httpClient->sendRequest($this->psr17->createRequest('PUT', $object->url)->withBody($request->getBody())); - if ($previousLockData->lock !== $xWopiLock) { - return $this - ->psr17 - ->createResponse(409) - ->withAddedHeader('X-WOPI-Lock', $previousLockData->lock); - } - } - - $return = file_put_contents( - $filepath, - (string) $request->getBody() - ); - - if (false === $return) { - return $this - ->psr17 - ->createResponse(500); + if (200 !== $response->getStatusCode()) + { + throw new Exception('Error in response.'); } return $this