2021-09-29 22:38:48 +02:00

198 lines
6.8 KiB
PHP

<?php
/**
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\WopiBundle\Service\Wopi;
use ChampsLibres\WopiLib\Contract\Service\DocumentManagerInterface;
use ChampsLibres\WopiLib\Contract\Service\WopiInterface;
use loophp\psr17\Psr17Interface;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\User\UserProviderInterface;
final class ChillWopi implements WopiInterface
{
private CacheItemPoolInterface $cache;
private DocumentManagerInterface $documentManager;
private Psr17Interface $psr17;
private UserProviderInterface $userProvider;
private WopiInterface $wopi;
public function __construct(
CacheItemPoolInterface $cache,
DocumentManagerInterface $documentManager,
Psr17Interface $psr17,
UserProviderInterface $userProvider,
WopiInterface $wopi
) {
$this->cache = $cache;
$this->documentManager = $documentManager;
$this->psr17 = $psr17;
$this->userProvider = $userProvider;
$this->wopi = $wopi;
}
public function checkFileInfo(
string $fileId,
?string $accessToken,
RequestInterface $request
): ResponseInterface {
try {
$user = $this->userProvider->loadUserByUsername($accessToken);
} catch (UsernameNotFoundException $e) {
return $this
->psr17
->createResponse(401);
}
// @ TODO : Replace this with a call to decorated object once authentication is done.
$document = $this->documentManager->findByDocumentId($fileId);
$userIdentifier = $user->getUsername();
$userCacheKey = sprintf('wopi_putUserInfo_%s', $userIdentifier);
return $this
->psr17
->createResponse()
->withHeader('Content-Type', 'application/json')
->withBody($this->psr17->createStream((string) json_encode(
[
'BaseFileName' => $this->documentManager->getBasename($document),
'OwnerId' => 'Symfony',
'Size' => $this->documentManager->getSize($document),
'UserId' => $userIdentifier,
'ReadOnly' => false,
'UserCanAttend' => true,
'UserCanPresent' => true,
'UserCanRename' => true,
'UserCanWrite' => true,
'UserCanNotWriteRelative' => false,
'SupportsUserInfo' => true,
'SupportsDeleteFile' => true,
'SupportsLocks' => true,
'SupportsGetLock' => true,
'SupportsExtendedLockLength' => true,
'UserFriendlyName' => $userIdentifier,
'SupportsUpdate' => true,
'SupportsRename' => true,
'DisablePrint' => false,
'AllowExternalMarketplace' => true,
'SupportedShareUrlTypes' => [
'ReadOnly',
],
'SHA256' => $this->documentManager->getSha256($document),
'UserInfo' => (string) $this->cache->getItem($userCacheKey)->get(),
]
)));
}
public function deleteFile(string $fileId, ?string $accessToken, RequestInterface $request): ResponseInterface
{
return $this->wopi->deleteFile($fileId, $accessToken, $request);
}
public function enumerateAncestors(
string $fileId,
?string $accessToken,
RequestInterface $request
): ResponseInterface {
return $this->wopi->enumerateAncestors($fileId, $accessToken, $request);
}
public function getFile(string $fileId, ?string $accessToken, RequestInterface $request): ResponseInterface
{
return $this->wopi->getFile($fileId, $accessToken, $request);
}
public function getLock(string $fileId, ?string $accessToken, RequestInterface $request): ResponseInterface
{
return $this->wopi->getLock($fileId, $accessToken, $request);
}
public function getShareUrl(
string $fileId,
?string $accessToken,
RequestInterface $request
): ResponseInterface {
return $this->wopi->getShareUrl($fileId, $accessToken, $request);
}
public function lock(
string $fileId,
?string $accessToken,
string $xWopiLock,
RequestInterface $request
): ResponseInterface {
return $this->wopi->lock($fileId, $accessToken, $xWopiLock, $request);
}
public function putFile(
string $fileId,
?string $accessToken,
string $xWopiLock,
string $xWopiEditors,
RequestInterface $request
): ResponseInterface {
return $this->wopi->putFile($fileId, $accessToken, $xWopiLock, $xWopiEditors, $request);
}
public function putRelativeFile(string $fileId, string $accessToken, ?string $suggestedTarget, ?string $relativeTarget, bool $overwriteRelativeTarget, int $size, RequestInterface $request): ResponseInterface
{
return $this->wopi->putRelativeFile($fileId, $accessToken, $suggestedTarget, $relativeTarget, $overwriteRelativeTarget, $size, $request);
}
public function putUserInfo(string $fileId, ?string $accessToken, RequestInterface $request): ResponseInterface
{
return $this->wopi->putUserInfo($fileId, $accessToken, $request);
}
public function refreshLock(
string $fileId,
?string $accessToken,
string $xWopiLock,
RequestInterface $request
): ResponseInterface {
return $this->wopi->refreshLock($fileId, $accessToken, $xWopiLock, $request);
}
public function renameFile(
string $fileId,
?string $accessToken,
string $xWopiLock,
string $xWopiRequestedName,
RequestInterface $request
): ResponseInterface {
return $this->wopi->renameFile($fileId, $accessToken, $xWopiLock, $xWopiRequestedName, $request);
}
public function unlock(
string $fileId,
?string $accessToken,
string $xWopiLock,
RequestInterface $request
): ResponseInterface {
return $this->wopi->unlock($fileId, $accessToken, $xWopiLock, $request);
}
public function unlockAndRelock(
string $fileId,
?string $accessToken,
string $xWopiLock,
string $xWopiOldLock,
RequestInterface $request
): ResponseInterface {
return $this->wopi->unlockAndRelock($fileId, $accessToken, $xWopiLock, $xWopiOldLock, $request);
}
}