Skip to content

Commit 7ff5b5a

Browse files
committed
feat(directediting): Allow opening by file id
Signed-off-by: Julius Härtl <jus@bitgrid.net>
1 parent 54954cc commit 7ff5b5a

3 files changed

Lines changed: 24 additions & 7 deletions

File tree

apps/files/lib/Controller/DirectEditingController.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
use OCP\IURLGenerator;
3636

3737
class DirectEditingController extends OCSController {
38-
3938
/** @var IEventDispatcher */
4039
private $eventDispatcher;
4140

@@ -94,14 +93,14 @@ public function create(string $path, string $editorId, string $creatorId, string
9493
/**
9594
* @NoAdminRequired
9695
*/
97-
public function open(string $path, string $editorId = null): DataResponse {
96+
public function open(string $path, string $editorId = null, ?int $fileId = null): DataResponse {
9897
if (!$this->directEditingManager->isEnabled()) {
9998
return new DataResponse(['message' => 'Direct editing is not enabled'], Http::STATUS_INTERNAL_SERVER_ERROR);
10099
}
101100
$this->eventDispatcher->dispatchTyped(new RegisterDirectEditorEvent($this->directEditingManager));
102101

103102
try {
104-
$token = $this->directEditingManager->open($path, $editorId);
103+
$token = $this->directEditingManager->open($path, $editorId, $fileId);
105104
return new DataResponse([
106105
'url' => $this->urlGenerator->linkToRouteAbsolute('files.DirectEditingView.edit', ['token' => $token])
107106
]);

apps/files/lib/DirectEditingCapabilities.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ public function getCapabilities() {
4343
'files' => [
4444
'directEditing' => [
4545
'url' => $this->urlGenerator->linkToOCSRouteAbsolute('files.DirectEditing.info'),
46-
'etag' => $this->directEditingService->getDirectEditingETag()
46+
'etag' => $this->directEditingService->getDirectEditingETag(),
47+
'supportsFileId' => true,
4748
]
4849
],
4950
];

lib/private/DirectEditing/Manager.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use OCP\AppFramework\Http\NotFoundResponse;
3131
use OCP\AppFramework\Http\Response;
3232
use OCP\AppFramework\Http\TemplateResponse;
33+
use OCP\Constants;
3334
use OCP\DB\QueryBuilder\IQueryBuilder;
3435
use OCP\DirectEditing\ACreateFromTemplate;
3536
use OCP\DirectEditing\IEditor;
@@ -149,9 +150,25 @@ public function create(string $path, string $editorId, string $creatorId, $templ
149150
throw new \RuntimeException('No creator found');
150151
}
151152

152-
public function open(string $filePath, string $editorId = null): string {
153-
/** @var File $file */
154-
$file = $this->rootFolder->getUserFolder($this->userId)->get($filePath);
153+
public function open(string $filePath, string $editorId = null, ?int $fileId = null): string {
154+
$userFolder = $this->rootFolder->getUserFolder($this->userId);
155+
$file = $userFolder->get($filePath);
156+
if ($fileId !== null && $file instanceof Folder) {
157+
$files = $file->getById($fileId);
158+
159+
// Workaround to always open files with edit permissions if multiple occurences of
160+
// the same file id are in the user home, ideally we should also track the path of the file when opening
161+
usort($files, function (Node $a, Node $b) {
162+
return ($b->getPermissions() & Constants::PERMISSION_UPDATE) <=> ($a->getPermissions() & Constants::PERMISSION_UPDATE);
163+
});
164+
$file = array_shift($files);
165+
}
166+
167+
if (!$file instanceof File) {
168+
throw new NotFoundException();
169+
}
170+
171+
$filePath = $userFolder->getRelativePath($file->getPath());
155172

156173
if ($editorId === null) {
157174
$editorId = $this->findEditorForFile($file);

0 commit comments

Comments
 (0)