fix: Update checkFileInfo, getFile and putFile. Remove obsolete code in unsupported methods.

This commit is contained in:
Pol Dellaiera 2021-08-19 16:03:19 +02:00 committed by Marc Ducobu
parent eba8a3c260
commit aaf5e0e601

View File

@ -18,6 +18,7 @@ use loophp\psr17\Psr17Interface;
use Psr\Http\Client\ClientInterface; use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestInterface; use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\Security; use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\User\UserProviderInterface; use Symfony\Component\Security\Core\User\UserProviderInterface;
@ -60,7 +61,13 @@ final class ChillWopi implements WopiInterface
?string $accessToken, ?string $accessToken,
RequestInterface $request RequestInterface $request
): ResponseInterface { ): ResponseInterface {
$user = $this->userProvider->loadUserByUsername($accessToken); try {
$user = $this->userProvider->loadUserByUsername($accessToken);
} catch (UsernameNotFoundException $e) {
return $this
->psr17
->createResponse(401);
}
$storedObject = $this->storedObjectRepository->findOneBy(['filename' => $fileId]); $storedObject = $this->storedObjectRepository->findOneBy(['filename' => $fileId]);
@ -87,16 +94,16 @@ final class ChillWopi implements WopiInterface
// 'Version' => 'v' . uniqid(), // 'Version' => 'v' . uniqid(),
'ReadOnly' => false, 'ReadOnly' => false,
'UserCanWrite' => true, 'UserCanWrite' => true,
'UserCanNotWriteRelative' => false, 'UserCanNotWriteRelative' => true,
'SupportsLocks' => true, 'SupportsLocks' => false,
'UserFriendlyName' => sprintf('User %s', $user->getUsername()), 'UserFriendlyName' => sprintf('User %s', $user->getUsername()),
'UserExtraInfo' => [], 'UserExtraInfo' => [],
'LastModifiedTime' => date('Y-m-d\TH:i:s.u\Z', $storedObject->getCreationDate()->getTimestamp()), 'LastModifiedTime' => date('Y-m-d\TH:i:s.u\Z', $storedObject->getCreationDate()->getTimestamp()),
'CloseButtonClosesWindow' => true, 'CloseButtonClosesWindow' => true,
'EnableInsertRemoteImage' => true, 'EnableInsertRemoteImage' => true,
'EnableShare' => true, 'EnableShare' => false,
'SupportsUpdate' => true, 'SupportsUpdate' => true,
'SupportsRename' => true, 'SupportsRename' => false,
'DisablePrint' => false, 'DisablePrint' => false,
'DisableExport' => false, 'DisableExport' => false,
'DisableCopy' => false, 'DisableCopy' => false,
@ -119,14 +126,35 @@ final class ChillWopi implements WopiInterface
public function getFile(string $fileId, ?string $accessToken, RequestInterface $request): ResponseInterface public function getFile(string $fileId, ?string $accessToken, RequestInterface $request): ResponseInterface
{ {
try {
$user = $this->userProvider->loadUserByUsername($accessToken);
} catch (UsernameNotFoundException $e) {
return $this
->psr17
->createResponse(401);
}
$storedObject = $this->storedObjectRepository->findOneBy(['filename' => $fileId]); $storedObject = $this->storedObjectRepository->findOneBy(['filename' => $fileId]);
if (null === $storedObject) {
return $this
->psr17
->createResponse(404);
}
// TODO: Add strict typing in champs-libres/async-uploader-bundle // TODO: Add strict typing in champs-libres/async-uploader-bundle
/** @var StdClass $object */ /** @var StdClass $object */
$object = $this->tempUrlGeneratorInterface->generate('GET', $storedObject->getFilename()); $object = $this->tempUrlGeneratorInterface->generate('GET', $storedObject->getFilename());
$response = $this->httpClient->sendRequest($this->psr17->createRequest('GET', $object->url)); $response = $this->httpClient->sendRequest($this->psr17->createRequest('GET', $object->url));
if (200 !== $response->getStatusCode())
{
return $this
->psr17
->createResponse(500);
}
return $this return $this
->psr17 ->psr17
->createResponse() ->createResponse()
@ -143,19 +171,7 @@ final class ChillWopi implements WopiInterface
public function getLock(string $fileId, ?string $accessToken, RequestInterface $request): ResponseInterface public function getLock(string $fileId, ?string $accessToken, RequestInterface $request): ResponseInterface
{ {
$lockFilepath = $this->getLockFilepath($fileId); return $this->getDebugResponse(__FUNCTION__, $request);
$lock = '';
if ($this->fs->exists($lockFilepath)) {
$lockData = json_decode(file_get_contents($lockFilepath));
$lock = $lockData->lock;
}
return $this
->psr17
->createResponse()
->withHeader('X-WOPI-Lock', $lock);
} }
public function getShareUrl( public function getShareUrl(
@ -163,15 +179,7 @@ final class ChillWopi implements WopiInterface
?string $accessToken, ?string $accessToken,
RequestInterface $request RequestInterface $request
): ResponseInterface { ): ResponseInterface {
$data = [ return $this->getDebugResponse(__FUNCTION__, $request);
'ShareUrl' => 'TODO',
];
return $this
->psr17
->createResponse()
->withHeader('Content-Type', 'application/json')
->withBody($this->psr17->createStream((string) json_encode($data)));
} }
public function lock( public function lock(
@ -180,29 +188,7 @@ final class ChillWopi implements WopiInterface
string $xWopiLock, string $xWopiLock,
RequestInterface $request RequestInterface $request
): ResponseInterface { ): ResponseInterface {
$lockFilepath = $this->getLockFilepath($fileId); return $this->getDebugResponse(__FUNCTION__, $request);
if ($this->fs->exists($lockFilepath)) {
$previousLockData = json_decode(file_get_contents($lockFilepath));
if ($previousLockData->lock !== $xWopiLock) {
return $this
->psr17
->createResponse(409)
->withAddedHeader('X-WOPI-Lock', $previousLockData->lock);
}
}
$data = [
'lock' => $xWopiLock,
'time' => microtime(),
];
$this->fs->dumpFile($lockFilepath, json_encode($data));
return $this
->psr17
->createResponse();
} }
public function putFile( public function putFile(
@ -212,8 +198,20 @@ final class ChillWopi implements WopiInterface
string $xWopiEditors, string $xWopiEditors,
RequestInterface $request RequestInterface $request
): ResponseInterface { ): ResponseInterface {
try {
$user = $this->userProvider->loadUserByUsername($accessToken);
} catch (UsernameNotFoundException $e) {
return $this
->psr17
->createResponse(401);
}
$storedObject = $this->storedObjectRepository->findOneBy(['filename' => $fileId]); $storedObject = $this->storedObjectRepository->findOneBy(['filename' => $fileId]);
if (null === $storedObject) {
throw new Exception(sprintf('Unable to find object named %s', $fileId));
}
// TODO: Add strict typing in champs-libres/async-uploader-bundle // TODO: Add strict typing in champs-libres/async-uploader-bundle
/** @var StdClass $object */ /** @var StdClass $object */
$object = $this->tempUrlGeneratorInterface->generate('PUT', $storedObject->getFilename()); $object = $this->tempUrlGeneratorInterface->generate('PUT', $storedObject->getFilename());
@ -222,7 +220,9 @@ final class ChillWopi implements WopiInterface
if (200 !== $response->getStatusCode()) if (200 !== $response->getStatusCode())
{ {
throw new Exception('Error in response.'); return $this
->psr17
->createResponse(500);
} }
return $this return $this
@ -235,26 +235,7 @@ final class ChillWopi implements WopiInterface
public function putRelativeFile(string $fileId, ?string $accessToken, RequestInterface $request): ResponseInterface public function putRelativeFile(string $fileId, ?string $accessToken, RequestInterface $request): ResponseInterface
{ {
$filepath = sprintf( return $this->getDebugResponse(__FUNCTION__, $request);
'%s/%s',
$this->filesRepository,
$request->getHeaderLine('X-WOPI-SuggestedTarget')
);
$return = file_put_contents(
$filepath,
(string) $request->getBody()
);
if (false === $return) {
return $this
->psr17
->createResponse(500);
}
return $this
->psr17
->createResponse(500); // Not supported yet.
} }
public function putUserInfo(string $fileId, ?string $accessToken, RequestInterface $request): ResponseInterface public function putUserInfo(string $fileId, ?string $accessToken, RequestInterface $request): ResponseInterface
@ -287,25 +268,7 @@ final class ChillWopi implements WopiInterface
string $xWopiLock, string $xWopiLock,
RequestInterface $request RequestInterface $request
): ResponseInterface { ): ResponseInterface {
$lockFilepath = $this->getLockFilepath($fileId); return $this->getDebugResponse(__FUNCTION__, $request);
if ($this->fs->exists($lockFilepath)) {
$previousLockData = json_decode(file_get_contents($lockFilepath));
if ($previousLockData->lock !== $xWopiLock) {
return $this
->psr17
->createResponse(409)
->withAddedHeader('X-WOPI-Lock', $previousLockData->lock);
}
}
$this->fs->remove($lockFilepath);
return $this
->psr17
->createResponse()
->withAddedHeader('X-WOPI-Lock', '');
} }
public function unlockAndRelock( public function unlockAndRelock(