Skip to content

Commit

Permalink
feat(directediting): Allow opening by file id
Browse files Browse the repository at this point in the history
Signed-off-by: Julius Härtl <jus@bitgrid.net>
  • Loading branch information
juliusknorr committed Feb 16, 2023
1 parent 54954cc commit 7ff5b5a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
5 changes: 2 additions & 3 deletions apps/files/lib/Controller/DirectEditingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
use OCP\IURLGenerator;

class DirectEditingController extends OCSController {

/** @var IEventDispatcher */
private $eventDispatcher;

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

try {
$token = $this->directEditingManager->open($path, $editorId);
$token = $this->directEditingManager->open($path, $editorId, $fileId);
return new DataResponse([
'url' => $this->urlGenerator->linkToRouteAbsolute('files.DirectEditingView.edit', ['token' => $token])
]);
Expand Down
3 changes: 2 additions & 1 deletion apps/files/lib/DirectEditingCapabilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public function getCapabilities() {
'files' => [
'directEditing' => [
'url' => $this->urlGenerator->linkToOCSRouteAbsolute('files.DirectEditing.info'),
'etag' => $this->directEditingService->getDirectEditingETag()
'etag' => $this->directEditingService->getDirectEditingETag(),
'supportsFileId' => true,
]
],
];
Expand Down
23 changes: 20 additions & 3 deletions lib/private/DirectEditing/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use OCP\AppFramework\Http\NotFoundResponse;
use OCP\AppFramework\Http\Response;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\Constants;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\DirectEditing\ACreateFromTemplate;
use OCP\DirectEditing\IEditor;
Expand Down Expand Up @@ -149,9 +150,25 @@ public function create(string $path, string $editorId, string $creatorId, $templ
throw new \RuntimeException('No creator found');
}

public function open(string $filePath, string $editorId = null): string {
/** @var File $file */
$file = $this->rootFolder->getUserFolder($this->userId)->get($filePath);
public function open(string $filePath, string $editorId = null, ?int $fileId = null): string {
$userFolder = $this->rootFolder->getUserFolder($this->userId);
$file = $userFolder->get($filePath);
if ($fileId !== null && $file instanceof Folder) {
$files = $file->getById($fileId);

// Workaround to always open files with edit permissions if multiple occurences of
// the same file id are in the user home, ideally we should also track the path of the file when opening
usort($files, function (Node $a, Node $b) {
return ($b->getPermissions() & Constants::PERMISSION_UPDATE) <=> ($a->getPermissions() & Constants::PERMISSION_UPDATE);
});
$file = array_shift($files);
}

if (!$file instanceof File) {
throw new NotFoundException();
}

$filePath = $userFolder->getRelativePath($file->getPath());

if ($editorId === null) {
$editorId = $this->findEditorForFile($file);
Expand Down

0 comments on commit 7ff5b5a

Please sign in to comment.