Skip to content

Commit

Permalink
feat: Add allowed_view_extensions config node
Browse files Browse the repository at this point in the history
Signed-off-by: Kostiantyn Miakshyn <[email protected]>
  • Loading branch information
Koc committed Oct 26, 2024
1 parent 6ce11f8 commit bf2f624
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/Service/ApiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,13 @@ public function create(?int $fileId = null, ?string $filePath = null, ?string $b
if ($storage->instanceOfStorage(SharedStorage::class)) {
/** @var IShare $share */
$share = $storage->getShare();
$shareAttribtues = $share->getAttributes();
if ($shareAttribtues !== null && $shareAttribtues->getAttribute('permissions', 'download') === false) {

$allowedFileExtensions = $this->configService->getAllowedViewFileExtensions();
$isAllowedToViewForExtension = $allowedFileExtensions && in_array($file->getExtension(), $allowedFileExtensions, true);
$shareAttributes = $share->getAttributes();
$isAllowedByShare = $shareAttributes === null || $shareAttributes->getAttribute('permissions', 'download') !== false;

if (!$isAllowedToViewForExtension && !$isAllowedByShare) {
return new DataResponse(['error' => $this->l10n->t('This file cannot be displayed as download is disabled by the share')], Http::STATUS_FORBIDDEN);
}
}
Expand Down
7 changes: 7 additions & 0 deletions lib/Service/AttachmentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public function __construct(
private IMimeTypeDetector $mimeTypeDetector,
private IURLGenerator $urlGenerator,
private IFilenameValidator $filenameValidator,
private ConfigService $configService,
) {
}

Expand Down Expand Up @@ -464,6 +465,12 @@ private function isDownloadDisabled(File $file): bool {
if ($storage->instanceOfStorage(SharedStorage::class)) {
/** @var SharedStorage $storage */
$share = $storage->getShare();

$allowedFileExtensions = $this->configService->getAllowedViewFileExtensions();
if ($allowedFileExtensions && in_array($file->getExtension(), $allowedFileExtensions, true)) {
return false;
}

$attributes = $share->getAttributes();
if ($attributes !== null && $attributes->getAttribute('permissions', 'download') === false) {
return true;
Expand Down
6 changes: 6 additions & 0 deletions lib/Service/ConfigService.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ public function isRichWorkspaceEnabledForUser(?string $userId): bool {

public function isNotifyPushSyncEnabled(): bool {
return $this->appConfig->getValueBool(Application::APP_NAME, 'notify_push');
}

/**
* @return string[]
*/
public function getAllowedViewFileExtensions(): array {
return $this->config->getSystemValue('allowed_view_extensions', []);
}
}

0 comments on commit bf2f624

Please sign in to comment.