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\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\User\UserProviderInterface;
@ -60,7 +61,13 @@ final class ChillWopi implements WopiInterface
?string $accessToken,
RequestInterface $request
): 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]);
@ -87,16 +94,16 @@ final class ChillWopi implements WopiInterface
// 'Version' => 'v' . uniqid(),
'ReadOnly' => false,
'UserCanWrite' => true,
'UserCanNotWriteRelative' => false,
'SupportsLocks' => true,
'UserCanNotWriteRelative' => true,
'SupportsLocks' => false,
'UserFriendlyName' => sprintf('User %s', $user->getUsername()),
'UserExtraInfo' => [],
'LastModifiedTime' => date('Y-m-d\TH:i:s.u\Z', $storedObject->getCreationDate()->getTimestamp()),
'CloseButtonClosesWindow' => true,
'EnableInsertRemoteImage' => true,
'EnableShare' => true,
'EnableShare' => false,
'SupportsUpdate' => true,
'SupportsRename' => true,
'SupportsRename' => false,
'DisablePrint' => false,
'DisableExport' => false,
'DisableCopy' => false,
@ -119,14 +126,35 @@ final class ChillWopi implements WopiInterface
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]);
if (null === $storedObject) {
return $this
->psr17
->createResponse(404);
}
// 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));
if (200 !== $response->getStatusCode())
{
return $this
->psr17
->createResponse(500);
}
return $this
->psr17
->createResponse()
@ -143,19 +171,7 @@ final class ChillWopi implements WopiInterface
public function getLock(string $fileId, ?string $accessToken, RequestInterface $request): ResponseInterface
{
$lockFilepath = $this->getLockFilepath($fileId);
$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);
return $this->getDebugResponse(__FUNCTION__, $request);
}
public function getShareUrl(
@ -163,15 +179,7 @@ final class ChillWopi implements WopiInterface
?string $accessToken,
RequestInterface $request
): ResponseInterface {
$data = [
'ShareUrl' => 'TODO',
];
return $this
->psr17
->createResponse()
->withHeader('Content-Type', 'application/json')
->withBody($this->psr17->createStream((string) json_encode($data)));
return $this->getDebugResponse(__FUNCTION__, $request);
}
public function lock(
@ -180,29 +188,7 @@ final class ChillWopi implements WopiInterface
string $xWopiLock,
RequestInterface $request
): ResponseInterface {
$lockFilepath = $this->getLockFilepath($fileId);
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();
return $this->getDebugResponse(__FUNCTION__, $request);
}
public function putFile(
@ -212,8 +198,20 @@ final class ChillWopi implements WopiInterface
string $xWopiEditors,
RequestInterface $request
): ResponseInterface {
try {
$user = $this->userProvider->loadUserByUsername($accessToken);
} catch (UsernameNotFoundException $e) {
return $this
->psr17
->createResponse(401);
}
$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
/** @var StdClass $object */
$object = $this->tempUrlGeneratorInterface->generate('PUT', $storedObject->getFilename());
@ -222,7 +220,9 @@ final class ChillWopi implements WopiInterface
if (200 !== $response->getStatusCode())
{
throw new Exception('Error in response.');
return $this
->psr17
->createResponse(500);
}
return $this
@ -235,26 +235,7 @@ final class ChillWopi implements WopiInterface
public function putRelativeFile(string $fileId, ?string $accessToken, RequestInterface $request): ResponseInterface
{
$filepath = sprintf(
'%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.
return $this->getDebugResponse(__FUNCTION__, $request);
}
public function putUserInfo(string $fileId, ?string $accessToken, RequestInterface $request): ResponseInterface
@ -287,25 +268,7 @@ final class ChillWopi implements WopiInterface
string $xWopiLock,
RequestInterface $request
): ResponseInterface {
$lockFilepath = $this->getLockFilepath($fileId);
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', '');
return $this->getDebugResponse(__FUNCTION__, $request);
}
public function unlockAndRelock(