|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OCA\Text\Controller; |
| 11 | + |
| 12 | +use OCA\Text\Context\FileContextFactory; |
| 13 | +use OCA\Text\Service\ApiService; |
| 14 | +use OCP\AppFramework\Http; |
| 15 | +use OCP\AppFramework\Http\Attribute\NoAdminRequired; |
| 16 | +use OCP\AppFramework\Http\Attribute\PublicPage; |
| 17 | +use OCP\AppFramework\Http\DataResponse; |
| 18 | +use OCP\AppFramework\OCSController; |
| 19 | +use OCP\DirectEditing\IManager; |
| 20 | +use OCP\Files\InvalidPathException; |
| 21 | +use OCP\Files\NotFoundException; |
| 22 | +use OCP\Files\NotPermittedException; |
| 23 | +use OCP\IRequest; |
| 24 | +use OCP\IUserManager; |
| 25 | +use OCP\IUserSession; |
| 26 | +use Psr\Log\LoggerInterface; |
| 27 | + |
| 28 | +class DirectEditingController extends OCSController { |
| 29 | + public function __construct( |
| 30 | + string $appName, |
| 31 | + IRequest $request, |
| 32 | + private readonly IManager $directEditingManager, |
| 33 | + private readonly IUserSession $userSession, |
| 34 | + private readonly ApiService $apiService, |
| 35 | + private readonly IUserManager $userManager, |
| 36 | + private readonly FileContextFactory $fileContextFactory, |
| 37 | + private readonly LoggerInterface $logger, |
| 38 | + ) { |
| 39 | + parent::__construct($appName, $request); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Open a direct editing token and create an editing session. |
| 44 | + * |
| 45 | + * This allows navigating from one direct editing file to the next without reloading the page. |
| 46 | + * |
| 47 | + * Returns 200 with file metadata and session data if the token is found. |
| 48 | + * |
| 49 | + * @param string $url Direct editing url as provided by the server |
| 50 | + */ |
| 51 | + #[NoAdminRequired] |
| 52 | + #[PublicPage] |
| 53 | + public function edit(string $token): DataResponse { |
| 54 | + $token = $this->directEditingManager->getToken($token); |
| 55 | + try { |
| 56 | + $token->useTokenScope(); |
| 57 | + $user = $this->userManager->get($token->getUser()); |
| 58 | + if ($user === null) { |
| 59 | + throw new NotFoundException(); |
| 60 | + } |
| 61 | + $file = $token->getFile(); |
| 62 | + $context = $this->fileContextFactory->build($user, 'file', $file->getId(), $file); |
| 63 | + $session = $this->apiService->create($context, null); |
| 64 | + $this->logger->debug('Started direct editing session.', ['token' => $token->getToken()]); |
| 65 | + return new DataResponse([ |
| 66 | + 'fileId' => $token->getFile()->getId(), |
| 67 | + 'mimetype' => $token->getFile()->getMimeType(), |
| 68 | + 'session' => $session->getData(), |
| 69 | + ]); |
| 70 | + } catch (InvalidPathException|NotFoundException|NotPermittedException) { |
| 71 | + return new DataResponse([], Http::STATUS_NOT_FOUND); |
| 72 | + } finally { |
| 73 | + $this->revertTokenScope(); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + private function revertTokenScope(): void { |
| 78 | + $this->userSession->setUser(null); |
| 79 | + } |
| 80 | + |
| 81 | +} |
0 commit comments