From 68bb63fe6f4bebb50963afcca3baf51f28fae0a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20H=C3=A4rtl?= Date: Wed, 21 Feb 2024 12:48:08 +0100 Subject: [PATCH] fix: Allow to unlock through separate endpoint for edit locally MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- appinfo/routes.php | 1 + lib/Controller/DocumentAPIController.php | 30 +++++++++++++++++++++++- src/document.js | 8 ++----- 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/appinfo/routes.php b/appinfo/routes.php index ca8c44127a..51ce712271 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -72,6 +72,7 @@ 'ocs' => [ // Public pages: new file creation ['name' => 'documentAPI#create', 'url' => '/api/v1/file', 'verb' => 'POST'], + ['name' => 'documentAPI#openLocal', 'url' => '/api/v1/local', 'verb' => 'POST'], // Client API endpoints ['name' => 'OCS#createDirect', 'url' => '/api/v1/document', 'verb' => 'POST'], diff --git a/lib/Controller/DocumentAPIController.php b/lib/Controller/DocumentAPIController.php index 33c28d586e..e0171d2a7d 100644 --- a/lib/Controller/DocumentAPIController.php +++ b/lib/Controller/DocumentAPIController.php @@ -30,11 +30,17 @@ use OCA\Richdocuments\Helper; use OCA\Richdocuments\TemplateManager; use OCP\AppFramework\Http; +use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\Http\JSONResponse; use OCP\Files\Folder; use OCP\Files\IRootFolder; +use OCP\Files\Lock\ILock; +use OCP\Files\Lock\ILockManager; +use OCP\Files\Lock\LockContext; +use OCP\Files\Lock\NoLockProviderException; use OCP\IL10N; use OCP\IRequest; +use OCP\PreConditionNotMetException; use OCP\Share\IManager; use Psr\Log\LoggerInterface; use Throwable; @@ -45,15 +51,17 @@ class DocumentAPIController extends \OCP\AppFramework\OCSController { private $templateManager; private $l10n; private $logger; + private $lockManager; private $userId; - public function __construct(IRequest $request, IRootFolder $rootFolder, IManager $shareManager, TemplateManager $templateManager, IL10N $l10n, LoggerInterface $logger, $userId) { + public function __construct(IRequest $request, IRootFolder $rootFolder, IManager $shareManager, TemplateManager $templateManager, IL10N $l10n, LoggerInterface $logger, ILockManager $lockManager, $userId) { parent::__construct(Application::APPNAME, $request); $this->rootFolder = $rootFolder; $this->shareManager = $shareManager; $this->templateManager = $templateManager; $this->l10n = $l10n; $this->logger = $logger; + $this->lockManager = $lockManager; $this->userId = $userId; } @@ -147,4 +155,24 @@ public function create(string $mimeType, string $fileName, string $directoryPath 'data' => \OCA\Files\Helper::formatFileInfo($file->getFileInfo()) ]); } + + /** + * @NoAdminRequired + */ + public function openLocal(int $fileId): DataResponse { + try { + $files = $this->rootFolder->getUserFolder($this->userId)->getById($fileId); + $file = array_shift($files); + $this->lockManager->unlock(new LockContext( + $file, + ILock::TYPE_APP, + Application::APPNAME + )); + return new DataResponse([]); + } catch (NoLockProviderException|PreConditionNotMetException $e) { + return new DataResponse([], Http::STATUS_BAD_REQUEST); + } catch (\Exception $e) { + return new DataResponse([], Http::STATUS_INTERNAL_SERVER_ERROR); + } + } } diff --git a/src/document.js b/src/document.js index 25c291aa27..47fcaffd2d 100644 --- a/src/document.js +++ b/src/document.js @@ -326,7 +326,7 @@ const documentsMain = { PostMessages.sendWOPIPostMessage('loolframe', 'Hide_Menu_Item', { id: 'insertgraphicremote' }) } - if (Config.get('userId') !== null && !Config.get('isPublicShare')) { + if (Config.get('userId') !== null && !Config.get('isPublicShare') && loadState('richdocuments', 'open_local_editor', true)) { PostMessages.sendWOPIPostMessage('loolframe', 'Insert_Button', { id: 'Open_Local_Editor', imgurl: window.location.protocol + '//' + getNextcloudUrl() + imagePath('richdocuments', 'launch.svg'), @@ -591,11 +591,7 @@ const documentsMain = { }, unlockFile() { - const unlockUrl = getRootUrl() + '/index.php/apps/richdocuments/wopi/files/' + documentsMain.fileId - const unlockConfig = { - headers: { 'X-WOPI-Override': 'UNLOCK' }, - } - return axios.post(unlockUrl, { access_token: documentsMain.token }, unlockConfig) + return axios.post(generateOcsUrl('apps/richdocuments/api/v1/local'), { fileId: this.fileid }) }, openLocally() {