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 15, 2023
1 parent 54954cc commit 6ff560a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 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
21 changes: 19 additions & 2 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 */
public function open(string $filePath, string $editorId = null, ?int $fileId = null): string {
$userFolder = $this->rootFolder->getUserFolder($this->userId);
$file = $this->rootFolder->getUserFolder($this->userId)->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 6ff560a

Please sign in to comment.