Skip to content

Commit d3f9fe3

Browse files
committed
chore(cleanup): error handling
* `checkSharePermissions` has not been throwing `NotPermittedException` for a while. See #3765 . * `InvalidArgumentException` was not being handled. * throw `NotFoundException` if file cannot be found and there is no share token. Signed-off-by: Max <max@nextcloud.com>
1 parent 3333b1e commit d3f9fe3

3 files changed

Lines changed: 17 additions & 14 deletions

File tree

lib/Context/FileContextFactory.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,20 @@ public function buildForId(
6262
}
6363

6464
/**
65-
* @throws NotPermittedException if not logged in
66-
* @throws NotFoundException if the file cannot be found
65+
* @throws NotFoundException if the share cannot be found or is not readable (file drop)
66+
* @throws \InvalidArgumentException if the share is not a file share and no file path is given
6767
*/
6868
public function buildForShareWithPath(
6969
string $token,
7070
?string $filePath,
7171
?string $baseVersionEtag,
7272
): FileContext {
7373
$file = $this->fileService->getFileByShareToken($token, $filePath);
74-
$this->fileService->checkSharePermissions($token, Constants::PERMISSION_READ);
74+
/*
75+
* Check if we have proper read access (files drop)
76+
* If not then well 404 it is.
77+
*/
78+
$this->fileService->checkSharePermissions($token);
7579
return $this->build($file, $baseVersionEtag, $token);
7680
}
7781

lib/Controller/PublicSessionController.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
use OCP\AppFramework\Http\DataResponse;
2020
use OCP\AppFramework\PublicShareController;
2121
use OCP\Files\NotFoundException;
22-
use OCP\Files\NotPermittedException;
2322
use OCP\IL10N;
2423
use OCP\IRequest;
2524
use OCP\ISession;
@@ -74,17 +73,13 @@ protected function isPasswordProtected(): bool {
7473
#[NoAdminRequired]
7574
#[PublicPage]
7675
public function create(string $token, ?string $filePath = null, ?string $baseVersionEtag = null, ?string $guestName = null): DataResponse {
77-
/*
78-
* Check if we have proper read access (files drop)
79-
* If not then well 404 it is.
80-
*/
8176
try {
8277
$context = $this->fileContextFactory->buildForShareWithPath($token, $filePath, $baseVersionEtag);
8378
return $this->apiService->create($context, $guestName);
8479
} catch (NotFoundException) {
8580
return new DataResponse([], Http::STATUS_NOT_FOUND);
86-
} catch (NotPermittedException) {
87-
return new DataResponse(['error' => $this->l10n->t('This file cannot be displayed as download is disabled by the share')], Http::STATUS_NOT_FOUND);
81+
} catch (\InvalidArgumentException) {
82+
return new DataResponse([], Http::STATUS_UNPROCESSABLE_ENTITY);
8883
}
8984

9085
}

lib/Service/FileService.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ public function getFileForSession(Session $session, ?string $shareToken = null):
4343
if (!$session->isGuest()) {
4444
try {
4545
return $this->getFileById($session->getDocumentId(), $session->getUserId());
46-
} catch (NotFoundException) {
46+
} catch (NotFoundException $e) {
47+
if ($shareToken === null) {
48+
throw $e;
49+
}
4750
// We may still have a user session but on a public share link so move on
4851
}
4952
}
@@ -118,7 +121,8 @@ public function getFileById(int $fileId, string $userId): File {
118121
}
119122

120123
/**
121-
* @throws NotFoundException
124+
* @throws NotFoundException if the share cannot be found based on the token
125+
* @throws \InvalidArgumentException if the share is not a File share and path is omitted.
122126
*/
123127
public function getFileByShareToken(string $shareToken, ?string $path = null): File {
124128
try {
@@ -134,7 +138,7 @@ public function getFileByShareToken(string $shareToken, ?string $path = null): F
134138
if ($node instanceof File) {
135139
return $node;
136140
}
137-
throw new \InvalidArgumentException('No proper share data');
141+
throw new \InvalidArgumentException('Invalid share data.');
138142
}
139143

140144
public function isReadOnly(File $file, ?string $token): bool {
@@ -169,7 +173,7 @@ public function isDownloadDisabled(File $file): bool {
169173
*
170174
* @return void
171175
*
172-
* @throws NotFoundException|NotPermittedException
176+
* @throws NotFoundException
173177
*
174178
* @psalm-param 1|2 $permission
175179
*/

0 commit comments

Comments
 (0)