Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix open locally with files lock and wopi allow list (backport stable26) #3515

Merged
merged 3 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down
30 changes: 29 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,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);
}
}
}
1 change: 1 addition & 0 deletions lib/Service/InitialStateService.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public function provideDocument(Wopi $wopi, array $params): void {
$this->initialState->provideInitialState('theming-customLogo', ($logoSet ?
\OC::$server->getURLGenerator()->getAbsoluteURL(\OC::$server->getThemingDefaults()->getLogo())
: false));
$this->initialState->provideInitialState('open_local_editor', $this->config->getAppValue(Application::APPNAME, 'open_local_editor', 'yes') === 'yes');
}

public function prepareParams(array $params): array {
Expand Down
35 changes: 16 additions & 19 deletions src/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
const checkProxyStatus = () => {
checkingProxyStatus = true
const url = Config.get('urlsrc').slice(0, Config.get('urlsrc').indexOf('proxy.php') + 'proxy.php'.length)
$.get(url + '?status').done(function(val) {

Check warning on line 38 in src/document.js

View workflow job for this annotation

GitHub Actions / eslint

The global property or function $ was deprecated in Nextcloud 19.0.0
if (val && val.status && val.status !== 'OK') {
if (val.status === 'starting' || val.status === 'stopped') {
document.getElementById('proxyLoadingIcon').classList.add('icon-loading-small')
Expand Down Expand Up @@ -326,7 +326,7 @@
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'),
Expand Down Expand Up @@ -481,11 +481,13 @@
break
case 'Get_Views_Resp':
if (documentsMain.openingLocally) {
documentsMain.UI.removeViews(parsed.args)
documentsMain.unlockFile()
.catch(_ => {}) // Unlocking failed, possibly because file was not locked, we want to proceed regardless.
.then(() => {
documentsMain.openLocally()
documentsMain.openLocally().then(() => {
documentsMain.UI.removeViews(parsed.args)
documentsMain.close()
})
})
}
break
Expand Down Expand Up @@ -591,29 +593,24 @@
},

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() {
async openLocally() {
if (documentsMain.openingLocally) {
documentsMain.openingLocally = false

axios.post(
const result = await axios.post(
OC.linkToOCS('apps/files/api/v1', 2) + 'openlocaleditor?format=json',
{ path: documentsMain.fullPath }
).then((result) => {
const url = 'nc://open/'
+ Config.get('userId') + '@' + getNextcloudUrl()
+ OC.encodePath(documentsMain.fullPath)
+ '?token=' + result.data.ocs.data.token

this.showOpenLocalConfirmation(url, window.top)
window.location.href = url
})
)
const url = 'nc://open/'
+ Config.get('userId') + '@' + getNextcloudUrl()
+ OC.encodePath(documentsMain.fullPath)
+ '?token=' + result.data.ocs.data.token

this.showOpenLocalConfirmation(url, window.top)
window.open(url)
}
},

Expand Down
Loading