Skip to content

Commit

Permalink
Merge pull request #3503 from nextcloud/backport/3489/stable28
Browse files Browse the repository at this point in the history
[stable28] Fix open locally with files lock and wopi allow list
  • Loading branch information
max-nextcloud authored Feb 26, 2024
2 parents 6ab3515 + 7bef992 commit 8ad76d0
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 15 deletions.
1 change: 1 addition & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,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'],
Expand Down
29 changes: 28 additions & 1 deletion lib/Controller/DocumentAPIController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down Expand Up @@ -147,4 +155,23 @@ public function create(string $mimeType, string $fileName, string $directoryPath
'data' => \OCA\Files\Helper::formatFileInfo($file->getFileInfo())
]);
}

#[Http\Attribute\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);
}

}
}
2 changes: 2 additions & 0 deletions lib/Service/InitialStateService.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,7 @@ private function provideOptions(): void {
$this->initialState->provideInitialState('theming-customLogo', ($logoSet ?
$this->urlGenerator->getAbsoluteURL($this->themingDefaults->getLogo())
: false));

$this->initialState->provideInitialState('open_local_editor', $this->config->getAppValue(Application::APPNAME, 'open_local_editor', 'yes') === 'yes');
}
}
8 changes: 2 additions & 6 deletions src/mixins/openLocal.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import { getCurrentUser } from '@nextcloud/auth'
import axios from '@nextcloud/axios'
import { encodePath } from '@nextcloud/paths'
import { getRootUrl, generateOcsUrl } from '@nextcloud/router'
import { generateOcsUrl } from '@nextcloud/router'
import { getNextcloudUrl } from '../helpers/url.js'

// FIXME: Migrate to vue component
Expand Down Expand Up @@ -92,11 +92,7 @@ export default {
},

unlockFile() {
const unlockUrl = getRootUrl() + '/index.php/apps/richdocuments/wopi/files/' + this.fileid
const unlockConfig = {
headers: { 'X-WOPI-Override': 'UNLOCK' },
}
return axios.post(unlockUrl, { access_token: this.formData.accessToken }, unlockConfig)
return axios.post(generateOcsUrl('apps/richdocuments/api/v1/local'), { fileId: this.fileid })
},

openLocally() {
Expand Down
18 changes: 10 additions & 8 deletions src/view/Office.vue
Original file line number Diff line number Diff line change
Expand Up @@ -304,14 +304,16 @@ export default {
this.loading = LOADING_STATE.DOCUMENT_READY
clearTimeout(this.loadingTimeout)
this.sendPostMessage('Host_PostmessageReady')
this.sendPostMessage('Insert_Button', {
id: 'Open_Local_Editor',
imgurl: window.location.protocol + '//' + getNextcloudUrl() + imagePath('richdocuments', 'launch.svg'),
mobile: false,
label: t('richdocuments', 'Open in local editor'),
hint: t('richdocuments', 'Open in local editor'),
insertBefore: 'print',
})
if (loadState('richdocuments', 'open_local_editor', true)) {
this.sendPostMessage('Insert_Button', {
id: 'Open_Local_Editor',
imgurl: window.location.protocol + '//' + getNextcloudUrl() + imagePath('richdocuments', 'launch.svg'),
mobile: false,
label: t('richdocuments', 'Open in local editor'),
hint: t('richdocuments', 'Open in local editor'),
insertBefore: 'print',
})
}
},
async share() {
FilesAppIntegration.share()
Expand Down

0 comments on commit 8ad76d0

Please sign in to comment.