Skip to content

Commit 8a77984

Browse files
committed
chore(tweak): return first editable file early
Addresses #9028 (comment) . Signed-off-by: Max <max@nextcloud.com>
1 parent 039336c commit 8a77984

1 file changed

Lines changed: 14 additions & 12 deletions

File tree

lib/Service/FileService.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,27 +74,29 @@ public function getFileById(int $fileId, string $userId): File {
7474
return $file;
7575
}
7676

77-
// Ideally we'd optimize this part in the future by storing the path and getting the acutal target directly
78-
$files = $userFolder->getById($fileId);
77+
// Ideally we'd optimize this part in the future by storing the path and getting the actual target directly
78+
$files = array_filter($userFolder->getById($fileId), fn (Node $f) => $f instanceof File);
7979
if (count($files) === 0) {
8080
throw new NotFoundException();
8181
}
8282

8383
// Workaround to always open files with edit permissions if multiple occurrences of
8484
// the same file id are in the user home, ideally we should also track the path of the file when opening
85-
usort($files, static fn (Node $a, Node $b) => ($b->getPermissions() & Constants::PERMISSION_UPDATE) <=> ($a->getPermissions() & Constants::PERMISSION_UPDATE));
86-
87-
$file = array_shift($files);
88-
89-
if (!$file instanceof File) {
90-
throw new NotFoundException();
85+
$readableFile = null;
86+
foreach ($files as $file) {
87+
$permissions = $file->getPermissions();
88+
if ($permissions & Constants::PERMISSION_READ && $permissions & Constants::PERMISSION_UPDATE) {
89+
return $file;
90+
}
91+
if ($permissions & Constants::PERMISSION_READ) {
92+
$readableFile = $file;
93+
}
9194
}
92-
93-
if (($file->getPermissions() & Constants::PERMISSION_READ) !== Constants::PERMISSION_READ) {
94-
throw new NotPermittedException();
95+
if ($readableFile !== null) {
96+
return $readableFile;
9597
}
9698

97-
return $file;
99+
throw new NotPermittedException();
98100
}
99101

100102
/**

0 commit comments

Comments
 (0)