Skip to content

Commit

Permalink
fix: linting issue for CI
Browse files Browse the repository at this point in the history
Signed-off-by: codewithvk <[email protected]>
  • Loading branch information
codewithvk committed Jan 23, 2025
1 parent bf5a8bf commit e7023ad
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 174 deletions.
21 changes: 10 additions & 11 deletions lib/Controller/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@

use OCA\Richdocuments\AppConfig;
use OCA\Richdocuments\Capabilities;
use OCA\Richdocuments\Service\SettingsService;
use OCA\Richdocuments\Db\WopiMapper;
use OCA\Richdocuments\Service\CapabilitiesService;
use OCA\Richdocuments\Service\ConnectivityService;
use OCA\Richdocuments\Service\DemoService;
use OCA\Richdocuments\Service\DiscoveryService;
use OCA\Richdocuments\Service\FontService;
use OCA\Richdocuments\Service\SettingsService;
use OCA\Richdocuments\UploadException;
use OCA\Richdocuments\Db\WopiMapper;
use OCP\App\IAppManager;
use OCP\IGroupManager;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
Expand All @@ -27,12 +26,12 @@
use OCP\Files\NotPermittedException;
use OCP\Files\SimpleFS\ISimpleFile;
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\IL10N;
use OCP\IRequest;
use OCP\IURLGenerator;
use OCP\PreConditionNotMetException;
use OCP\Util;
use OCA\Richdocuments\WOPI\SettingsUrl;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Output\NullOutput;

Expand Down Expand Up @@ -422,7 +421,7 @@ public function getFontFileOverview(string $name): DataDisplayResponse {
* @NoCSRFRequired
*
* @param string $type - Type is 'admin' or 'user'
* @return DataDisplayResponse
* @return DataResponse
*/
public function generateIframeToken(string $type) : DataResponse {
$userId = $this->userId;
Expand Down Expand Up @@ -486,12 +485,12 @@ public function uploadFontFile(): JSONResponse {
}

/**
* @param string $type
* @param string $category
* @param string $name
*
* @return DataDisplayResponse
*
* @param string $type
* @param string $category
* @param string $name
*
* @return DataDisplayResponse
*
* @NoAdminRequired
* @PublicPage
* @NoCSRFRequired
Expand Down
17 changes: 9 additions & 8 deletions lib/Controller/WopiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
use OCA\Richdocuments\Helper;
use OCA\Richdocuments\PermissionManager;
use OCA\Richdocuments\Service\FederationService;
use OCA\Richdocuments\Service\SettingsService;
use OCA\Richdocuments\Service\UserScopeService;
use OCA\Richdocuments\TaskProcessingManager;
use OCA\Richdocuments\TemplateManager;
use OCA\Richdocuments\TokenManager;
use OCA\Richdocuments\WOPI\SettingsUrl;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\FrontpageRoute;
Expand All @@ -44,6 +46,7 @@
use OCP\Files\Lock\OwnerLockedException;
use OCP\Files\Node;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\IRequest;
Expand All @@ -57,8 +60,6 @@
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Log\LoggerInterface;
use OCA\Richdocuments\Service\SettingsService;
use \OCA\Richdocuments\WOPI\SettingsUrl;

#[RestrictToWopiServer]
class WopiController extends Controller {
Expand Down Expand Up @@ -108,9 +109,9 @@ public function checkFileInfo(string $fileId, string $access_token): JSONRespons
$userSettingsUri = $this->generateUserSettingsUri($wopi);

if ($fileId == "-1" && $wopi->getTokenType() == WOPI::TOKEN_TYPE_SETTING_AUTH) {
if ($fileId == '-1' && $wopi->getTokenType() == WOPI::TOKEN_TYPE_SETTING_AUTH) {
$response = [
"UserSettingsUri" => $userSettingsUri,
'UserSettingsUri' => $userSettingsUri,
];

return new JSONResponse($response);
Expand Down Expand Up @@ -181,7 +182,7 @@ public function checkFileInfo(string $fileId, string $access_token): JSONRespons
'EnableRemoteAIContent' => $isTaskProcessingEnabled,
'HasContentRange' => true,
'ServerPrivateInfo' => [],
"UserSettingsUri" => $userSettingsUri,
'UserSettingsUri' => $userSettingsUri,
];

$enableZotero = $this->config->getAppValue(Application::APPNAME, 'zoteroEnabled', 'yes') === 'yes';
Expand Down Expand Up @@ -420,7 +421,7 @@ public function getSettings(string $type, string $access_token): JSONResponse {

$userConfig = $this->settingsService->generateSettingsConfig($type);
return new JSONResponse($userConfig, Http::STATUS_OK);
} catch (UnknownTokenException | ExpiredTokenException $e) {
} catch (UnknownTokenException|ExpiredTokenException $e) {
$this->logger->debug($e->getMessage(), ['exception' => $e]);
return new JSONResponse(['error' => 'Unauthorized'], Http::STATUS_UNAUTHORIZED);
} catch (\Exception $e) {
Expand All @@ -445,7 +446,7 @@ public function uploadSettingsFile(string $fileId, string $access_token): JSONRe

$content = fopen('php://input', 'rb');
if (!$content) {
throw new \Exception("Failed to read input stream.");
throw new \Exception('Failed to read input stream.');
}

$fileContent = stream_get_contents($content);
Expand Down Expand Up @@ -992,6 +993,6 @@ private function getWopiUrlForTemplate(Wopi $wopi): string {
// todo extract nextcloud url from everything
private function generateUserSettingsUri(Wopi $wopi): string {
$nextcloudUrl = $this->appConfig->getNextcloudUrl() ?: trim($this->urlGenerator->getAbsoluteURL(''), '/');
return $nextcloudUrl . '/index.php/apps/richdocuments/wopi/settings' . '?type=userconfig' . '&access_token=' . $wopi->getToken() . '&fileId=' . '-1';
return $nextcloudUrl . '/index.php/apps/richdocuments/wopi/settings' . '?type=userconfig' . '&access_token=' . $wopi->getToken() . '&fileId=' . '-1';
}
}
2 changes: 1 addition & 1 deletion lib/Db/WopiMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function generateUserSettingsToken($fileId, $userId, $version, $serverHos
'serverHost' => $serverHost,
'token' => $token,
'expiry' => $this->calculateNewTokenExpiry(),
'templateId' => "0",
'templateId' => '0',
'tokenType' => Wopi::TOKEN_TYPE_SETTING_AUTH,
]);

Expand Down
136 changes: 70 additions & 66 deletions lib/Service/SettingsService.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
<?php
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

declare(strict_types=1);

namespace OCA\Richdocuments\Service;

use Exception;
use OCA\Richdocuments\AppInfo\Application;
use OCA\Richdocuments\WOPI\SettingsUrl;
use OCP\Files\IAppData;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\Files\SimpleFS\ISimpleFile;
use OCP\Files\SimpleFS\ISimpleFolder;
use OCA\Richdocuments\WOPI\SettingsUrl;
use OCP\ICacheFactory;
use OCP\IConfig;
use OCP\IURLGenerator;
use \OCP\Files\NotPermittedException;

/**
* A generic service to manage "system-wide" files
Expand Down Expand Up @@ -48,44 +51,44 @@ public function __construct(
*/

public function ensureDirectory(SettingsUrl $settingsUrl): ISimpleFolder {
$type = $settingsUrl->getType();
$category = $settingsUrl->getCategory();
$type = $settingsUrl->getType();
$category = $settingsUrl->getCategory();

try {
$baseFolder = $this->appData->getFolder($type);
} catch (NotFoundException $e) {
$baseFolder = $this->appData->newFolder($type);
}
try {
$baseFolder = $this->appData->getFolder($type);
} catch (NotFoundException $e) {
$baseFolder = $this->appData->newFolder($type);
}

try {
$categoryFolder = $baseFolder->getFolder($category);
} catch (NotFoundException $e) {
$categoryFolder = $baseFolder->newFolder($category);
}
try {
$categoryFolder = $baseFolder->getFolder($category);
} catch (NotFoundException $e) {
$categoryFolder = $baseFolder->newFolder($category);
}

return $categoryFolder;
}
return $categoryFolder;
}

/**
* Upload a file to the settings directory.
* ex. $type/$category/$filename
*
* @param SettingsUrl $settingsUrl
* @param resource $fileData
* @param string $fileData
* @return array ['stamp' => string, 'uri' => string]
*/

public function uploadFile(SettingsUrl $settingsUrl, $fileData): array {
$categoryFolder = $this->ensureDirectory($settingsUrl);
public function uploadFile(SettingsUrl $settingsUrl, string $fileData): array {
$categoryFolder = $this->ensureDirectory($settingsUrl);
$fileName = $settingsUrl->getFileName();
$newFile = $categoryFolder->newFile($fileName, $fileData);
$fileUri = $this->generateFileUri($settingsUrl->getType(), $settingsUrl->getCategory(), $fileName);
$newFile = $categoryFolder->newFile($fileName, $fileData);
$fileUri = $this->generateFileUri($settingsUrl->getType(), $settingsUrl->getCategory(), $fileName);

return [
'stamp' => $newFile->getETag(),
'uri' => $fileUri,
];
}
return [
'stamp' => $newFile->getETag(),
'uri' => $fileUri,
];
}

/**
* Get list of files in a setting category.
Expand All @@ -95,21 +98,21 @@ public function uploadFile(SettingsUrl $settingsUrl, $fileData): array {
* @return array Each item has 'stamp' and 'uri'.
*/
public function getCategoryFileList(string $type, string $category): array {
try {
$categoryFolder = $this->appData->getFolder($type . '/' . $category);
} catch (NotFoundException $e) {
return [];
}

$files = $categoryFolder->getDirectoryListing();

return array_map(function(ISimpleFile $file) use ($type, $category) {
return [
'stamp' => $file->getETag(),
'uri' => $this->generateFileUri($type, $category, $file->getName()),
];
}, $files);
}
try {
$categoryFolder = $this->appData->getFolder($type . '/' . $category);
} catch (NotFoundException $e) {
return [];
}

$files = $categoryFolder->getDirectoryListing();

return array_map(function (ISimpleFile $file) use ($type, $category) {
return [
'stamp' => $file->getETag(),
'uri' => $this->generateFileUri($type, $category, $file->getName()),
];
}, $files);
}

/**
* generate setting config
Expand All @@ -120,40 +123,41 @@ public function getCategoryFileList(string $type, string $category): array {
public function generateSettingsConfig(string $type): array {
$kind = $type === 'userconfig' ? 'user' : 'shared';

$config = [
'kind' => $kind,
];
$config = [
'kind' => $kind,
];

$categories = $this->getAllCategories($type);
$categories = $this->getAllCategories($type);

foreach ($categories as $category) {
$files = $this->getCategoryFileList($type, $category);
foreach ($categories as $category) {
$files = $this->getCategoryFileList($type, $category);
$config[$category] = $files;
}
}

return $config;
}
return $config;
}

/**
* Get all setting categories for a setting type.
*
* @param string $type
* @return string[]
*/
private function getAllCategories(string $type): array {
try {
$categories = [];
$directories = $this->appData->getFolder($type)->getFullDirectoryListing();
foreach ($directories as $dir) {
if ($dir instanceof ISimpleFolder) {
$categories[] = $dir->getName();
}
}
return $categories;
} catch (NotFoundException $e) {
return [];
}
}
private function getAllCategories(string $type): array {
try {
$categories = [];
$folder = $this->appData->getFolder($type);
$directories = $folder->getFullDirectoryListing();
foreach ($directories as $dir) {
if ($dir instanceof ISimpleFolder) {
$categories[] = $dir->getName();
}
}
return $categories;
} catch (NotFoundException $e) {
return [];
}
}

/**
* Generate file URL.
Expand Down Expand Up @@ -234,6 +238,6 @@ public function deleteSettingsFile(string $type, string $category, string $name)
}
}

// TODO: Handle installDefaultSystemFiles setting
// TODO: Handle installDefaultSystemFiles setting

}
Loading

0 comments on commit e7023ad

Please sign in to comment.