Skip to content

Commit

Permalink
fixup! chore: refactor iframes to load collabora directly
Browse files Browse the repository at this point in the history
  • Loading branch information
juliusknorr committed Aug 31, 2023
1 parent ff577ac commit 3049ae2
Showing 1 changed file with 48 additions and 82 deletions.
130 changes: 48 additions & 82 deletions lib/Controller/DocumentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use \OCP\AppFramework\Http\TemplateResponse;
use \OCP\IConfig;
use \OCP\IRequest;
use Exception;
use OC\User\NoUserException;
use OCA\Richdocuments\Service\FederationService;
use OCA\Richdocuments\Service\InitialStateService;
Expand Down Expand Up @@ -45,59 +46,23 @@ class DocumentController extends Controller {

public const SESSION_FILE_TARGET = 'richdocuments_openfile_target';

/** @var ?string */
private $uid;
/** @var IConfig */
private $config;
/** @var AppConfig */
private $appConfig;
/** @var LoggerInterface */
private $logger;
/** @var IManager */
private $shareManager;
/** @var TokenManager */
private $tokenManager;
/** @var ISession */
private $session;
/** @var IRootFolder */
private $rootFolder;
/** @var TemplateManager */
private $templateManager;
/** @var FederationService */
private $federationService;
/** @var InitialStateService */
private $initialState;
private IURLGenerator $urlGenerator;

public function __construct(
$appName,
string $appName,
IRequest $request,
IConfig $config,
AppConfig $appConfig,
IManager $shareManager,
TokenManager $tokenManager,
IRootFolder $rootFolder,
ISession $session,
$UserId,
LoggerInterface $logger,
TemplateManager $templateManager,
FederationService $federationService,
InitialStateService $initialState,
IURLGenerator $urlGenerator
private IConfig $config,
private AppConfig $appConfig,
private IManager $shareManager,
private TokenManager $tokenManager,
private IRootFolder $rootFolder,
private ISession $session,
private ?string $userId,
private LoggerInterface $logger,
private TemplateManager $templateManager,
private FederationService $federationService,
private InitialStateService $initialState,
private IURLGenerator $urlGenerator
) {
parent::__construct($appName, $request);
$this->uid = $UserId;
$this->config = $config;
$this->appConfig = $appConfig;
$this->shareManager = $shareManager;
$this->tokenManager = $tokenManager;
$this->rootFolder = $rootFolder;
$this->session = $session;
$this->logger = $logger;
$this->templateManager = $templateManager;
$this->federationService = $federationService;
$this->initialState = $initialState;
$this->urlGenerator = $urlGenerator;
}

/**
Expand All @@ -110,7 +75,7 @@ public function __construct(
*
* @return array access_token, urlsrc
*/
public function extAppGetData(int $fileId) {
public function extAppGetData(int $fileId): array {
$secretToken = $this->request->getParam('secret_token');
$apps = array_filter(explode(',', $this->appConfig->getAppValue('external_apps')));
foreach ($apps as $app) {
Expand All @@ -122,18 +87,18 @@ public function extAppGetData(int $fileId) {
'fileId' => $fileId
]);
try {
$folder = $this->rootFolder->getUserFolder($this->uid);
$folder = $this->rootFolder->getUserFolder($this->userId);
$item = $folder->getById($fileId)[0];
if (!($item instanceof Node)) {
throw new \Exception();
throw new Exception();
}
list($urlSrc, $token) = $this->tokenManager->getToken($item->getId());
return [
'status' => 'success',
'urlsrc' => $urlSrc,
'token' => $token
];
} catch (\Exception $e) {
} catch (Exception $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
}
}
Expand All @@ -154,7 +119,7 @@ public function extAppGetData(int $fileId) {
*/
public function index($fileId, ?string $path = null) {
try {
$folder = $this->rootFolder->getUserFolder($this->uid);
$folder = $this->rootFolder->getUserFolder($this->userId);

if ($path !== null) {
$item = $folder->get($path);
Expand All @@ -163,7 +128,7 @@ public function index($fileId, ?string $path = null) {
}

if (!($item instanceof File)) {
throw new \Exception();
throw new Exception();
}

/**
Expand All @@ -179,7 +144,7 @@ public function index($fileId, ?string $path = null) {

$templateFile = $this->templateManager->getTemplateSource($item->getId());
if ($templateFile) {
list($urlSrc, $wopi) = $this->tokenManager->getTokenForTemplate($templateFile, $this->uid, $item->getId());
list($urlSrc, $wopi) = $this->tokenManager->getTokenForTemplate($templateFile, $this->userId, $item->getId());
$token = $wopi->getToken();
} else {
list($urlSrc, $token, $wopi) = $this->tokenManager->getToken($item->getId());
Expand Down Expand Up @@ -214,7 +179,7 @@ public function index($fileId, ?string $path = null) {
}

return $this->documentTemplateResponse($wopi, $params);
} catch (\Exception $e) {
} catch (Exception $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
return $this->renderErrorPage('Failed to open the requested file.');
}
Expand All @@ -238,7 +203,7 @@ public function createFromTemplate($templateId, $fileName, $dir) {
return new TemplateResponse('core', '403', [], 'guest');
}

$userFolder = $this->rootFolder->getUserFolder($this->uid);
$userFolder = $this->rootFolder->getUserFolder($this->userId);
try {
$folder = $userFolder->get($dir);
} catch (NotFoundException $e) {
Expand All @@ -252,7 +217,7 @@ public function createFromTemplate($templateId, $fileName, $dir) {
$file = $folder->newFile($fileName);

$template = $this->templateManager->get($templateId);
list($urlSrc, $wopi) = $this->tokenManager->getTokenForTemplate($template, $this->uid, $file->getId());
list($urlSrc, $wopi) = $this->tokenManager->getTokenForTemplate($template, $this->userId, $file->getId());

$wopiFileId = $wopi->getFileid() . '_' . $this->config->getSystemValue('instanceid');

Expand All @@ -276,7 +241,7 @@ public function createFromTemplate($templateId, $fileName, $dir) {
* @param string $shareToken
* @param string $fileName
* @return TemplateResponse|RedirectResponse
* @throws \Exception
* @throws Exception
*/
public function publicPage($shareToken, $fileName, $fileId) {
try {
Expand All @@ -286,7 +251,7 @@ public function publicPage($shareToken, $fileName, $fileId) {
if (!$this->session->exists('public_link_authenticated')
|| $this->session->get('public_link_authenticated') !== (string)$share->getId()
) {
throw new \Exception('Invalid password');
throw new Exception('Invalid password');
}
}

Expand Down Expand Up @@ -319,7 +284,7 @@ public function publicPage($shareToken, $fileName, $fileId) {
if ($templateFile) {
list($urlSrc, $wopi) = $this->tokenManager->getTokenForTemplate($templateFile, $share->getShareOwner(), $item->getId());
} else {
list($urlSrc, $token, $wopi) = $this->tokenManager->getToken($item->getId(), $shareToken, $this->uid);
list($urlSrc, $token, $wopi) = $this->tokenManager->getToken($item->getId(), $shareToken, $this->userId);
}
$params['token'] = $wopi->getToken();
$params['token_ttl'] = $wopi->getExpiry();
Expand All @@ -328,7 +293,7 @@ public function publicPage($shareToken, $fileName, $fileId) {

return $this->documentTemplateResponse($wopi, $params);
}
} catch (\Exception $e) {
} catch (Exception $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
return $this->renderErrorPage('Failed to open the requested file.');
}
Expand Down Expand Up @@ -356,7 +321,7 @@ public function remote($shareToken, $remoteServer, $remoteServerToken, $filePath
if (!$this->session->exists('public_link_authenticated')
|| $this->session->get('public_link_authenticated') !== (string)$share->getId()
) {
throw new \Exception('Invalid password');
throw new Exception('Invalid password');
}
}

Expand All @@ -370,11 +335,11 @@ public function remote($shareToken, $remoteServer, $remoteServerToken, $filePath
}

if ($node instanceof Node) {
list($urlSrc, $token, $wopi) = $this->tokenManager->getToken($node->getId(), $shareToken, $this->uid);
list($urlSrc, $token, $wopi) = $this->tokenManager->getToken($node->getId(), $shareToken, $this->userId);

$remoteWopi = $this->federationService->getRemoteFileDetails($remoteServer, $remoteServerToken);
if ($remoteWopi === null) {
throw new \Exception('Invalid remote file details for ' . $remoteServerToken);
throw new Exception('Invalid remote file details for ' . $remoteServerToken);
}
$this->tokenManager->upgradeToRemoteToken($wopi, $remoteWopi, $shareToken, $remoteServer, $remoteServerToken);

Expand All @@ -398,7 +363,7 @@ public function remote($shareToken, $remoteServer, $remoteServerToken, $filePath
}
} catch (ShareNotFound $e) {
return new TemplateResponse('core', '404', [], 'guest');
} catch (\Exception $e) {
} catch (Exception $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
return $this->renderErrorPage('Failed to open the requested file.');
}
Expand Down Expand Up @@ -426,10 +391,10 @@ public function editOnline(string $path = null, ?string $userId = null, ?string
}

if ($userId === null) {
$userId = $this->uid;
$userId = $this->userId;
}

if ($userId !== null && $userId !== $this->uid) {
if ($userId !== null && $userId !== $this->userId) {
return $this->renderErrorPage('You are trying to open a file from another user account than the one you are currently logged in with.');
}

Expand Down Expand Up @@ -466,12 +431,12 @@ public function editOnline(string $path = null, ?string $userId = null, ?string
* @UseSession
*/
public function editOnlineTarget(int $fileId, ?string $target = null) {
if (!$this->uid) {
if (!$this->userId) {
return $this->renderErrorPage('File not found', Http::STATUS_NOT_FOUND);
}

try {
$userFolder = $this->rootFolder->getUserFolder($this->uid);
$userFolder = $this->rootFolder->getUserFolder($this->userId);
$files = $userFolder->getById($fileId);
$file = array_shift($files);
if (!$file) {
Expand All @@ -486,9 +451,7 @@ public function editOnlineTarget(int $fileId, ?string $target = null) {
}
$redirectUrl = $this->urlGenerator->getAbsoluteURL('/index.php/f/' . $file->getId());
return new RedirectResponse($redirectUrl);
} catch (NotFoundException $e) {
} catch (NotPermittedException $e) {
} catch (NoUserException $e) {
} catch (NotFoundException|NotPermittedException|NoUserException) {
}

return $this->renderErrorPage('File not found', Http::STATUS_NOT_FOUND);
Expand All @@ -498,13 +461,16 @@ public function editOnlineTarget(int $fileId, ?string $target = null) {
#[NoCSRFRequired]
#[PublicPage]
public function token(int $fileId, ?string $shareToken = null): DataResponse {
[$urlSrc, $token, $wopi] = $this->tokenManager->getToken((string)$fileId, $shareToken, $this->uid, false);

return new DataResponse(array_merge(
[
'urlSrc' => $urlSrc,
],
$wopi->jsonSerialize(),
));
try {
[$urlSrc, , $wopi] = $this->tokenManager->getToken((string)$fileId, $shareToken, $this->userId);

return new DataResponse(array_merge(
[ 'urlSrc' => $urlSrc ],
$wopi->jsonSerialize(),
));
} catch (Exception $e) {
$this->logger->error('Failed to generate token for file', [ 'exception' => $e ]);
return new DataResponse('Failed to generate token', Http::STATUS_INTERNAL_SERVER_ERROR);
}
}
}

0 comments on commit 3049ae2

Please sign in to comment.