From e7023ad811b755a7a3228d793f9c522f3cfc3c9d Mon Sep 17 00:00:00 2001 From: codewithvk Date: Thu, 23 Jan 2025 11:57:21 +0530 Subject: [PATCH] fix: linting issue for CI Signed-off-by: codewithvk --- lib/Controller/SettingsController.php | 21 ++- lib/Controller/WopiController.php | 17 +-- lib/Db/WopiMapper.php | 2 +- lib/Service/SettingsService.php | 136 ++++++++++---------- lib/WOPI/SettingsUrl.php | 178 +++++++++++++------------- src/components/CoolFrame.vue | 5 +- 6 files changed, 185 insertions(+), 174 deletions(-) diff --git a/lib/Controller/SettingsController.php b/lib/Controller/SettingsController.php index 2a7fd5cc14..1c77181223 100644 --- a/lib/Controller/SettingsController.php +++ b/lib/Controller/SettingsController.php @@ -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; @@ -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; @@ -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; @@ -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 diff --git a/lib/Controller/WopiController.php b/lib/Controller/WopiController.php index 2919944779..70b46dffdf 100644 --- a/lib/Controller/WopiController.php +++ b/lib/Controller/WopiController.php @@ -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; @@ -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; @@ -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 { @@ -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); @@ -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'; @@ -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) { @@ -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); @@ -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'; } } diff --git a/lib/Db/WopiMapper.php b/lib/Db/WopiMapper.php index 97f9e26bd4..5bd3cc5cbf 100644 --- a/lib/Db/WopiMapper.php +++ b/lib/Db/WopiMapper.php @@ -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, ]); diff --git a/lib/Service/SettingsService.php b/lib/Service/SettingsService.php index 97efd30aa3..1c09508b71 100644 --- a/lib/Service/SettingsService.php +++ b/lib/Service/SettingsService.php @@ -1,20 +1,23 @@ 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. @@ -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 @@ -120,19 +123,19 @@ 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. @@ -140,20 +143,21 @@ public function generateSettingsConfig(string $type): array { * @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. @@ -234,6 +238,6 @@ public function deleteSettingsFile(string $type, string $category, string $name) } } - // TODO: Handle installDefaultSystemFiles setting + // TODO: Handle installDefaultSystemFiles setting } diff --git a/lib/WOPI/SettingsUrl.php b/lib/WOPI/SettingsUrl.php index c130b5a488..1f941b1c9a 100644 --- a/lib/WOPI/SettingsUrl.php +++ b/lib/WOPI/SettingsUrl.php @@ -1,4 +1,8 @@ rawUrl = $url; - $this->parseUrl($url); - } - - /** - * Factory method to create a SettingsUrl instance based on individual parameters. - */ - public static function fromComponents(string $type, string $category, string $fileName): self { - $rawUrl = "settings/$type/$category/$fileName"; - return new self($rawUrl); - } - - /** - * Parses the settings URL and extracts type, category, and filename. - * - * @param string - * @throws InvalidArgumentException - */ - private function parseUrl(string $url): void { - $decodedUrl = urldecode($url); - - $parsedUrl = parse_url($decodedUrl); - if (!isset($parsedUrl['path'])) { - throw new InvalidArgumentException("Invalid URL: Path not found."); - } - - $path = $parsedUrl['path']; - - $settingsIndex = strpos($path, '/settings/'); - if ($settingsIndex === false) { - throw new InvalidArgumentException("Invalid settings URL format: '/settings/' segment missing."); - } - - $relevantPath = substr($path, $settingsIndex + strlen('/settings/')); - - $pathParts = explode('/', $relevantPath); - - if (count($pathParts) < 3) { - throw new InvalidArgumentException("Invalid settings URL format: Expected 'type/category/fileName'."); - } - - $this->type = $pathParts[0]; - $this->category = $pathParts[1]; - $this->fileName = implode('/', array_slice($pathParts, 2)); - } - - /** - * Get the setting type from the URL. - * - * @return string - */ - public function getType(): string { - return $this->type; - } - - /** - * Get the setting category from the URL. - * - * @return string - */ - public function getCategory(): string { - return $this->category; - } - - /** - * Get the original filename from the URL. - * - * @return string - */ - public function getFileName(): string { - return $this->fileName; - } - - /** - * Get the raw URL. - * - * @return string - */ - public function getRawUrl(): string { - return $this->rawUrl; - } + private string $type; + private string $category; + private string $fileName; + private string $rawUrl; + + public function __construct(string $url) { + $this->rawUrl = $url; + $this->parseUrl($url); + } + + /** + * Factory method to create a SettingsUrl instance based on individual parameters. + */ + public static function fromComponents(string $type, string $category, string $fileName): self { + $rawUrl = "settings/$type/$category/$fileName"; + return new self($rawUrl); + } + + /** + * Parses the settings URL and extracts type, category, and filename. + * + * @param string $url The settings URL to parse. + * @throws InvalidArgumentException If the URL is invalid or incorrectly formatted. + */ + private function parseUrl(string $url): void { + $decodedUrl = urldecode($url); + + $parsedUrl = parse_url($decodedUrl); + if (!isset($parsedUrl['path'])) { + throw new InvalidArgumentException('Invalid URL: Path not found.'); + } + + $path = $parsedUrl['path']; + + $settingsIndex = strpos($path, '/settings/'); + if ($settingsIndex === false) { + throw new InvalidArgumentException("Invalid settings URL format: '/settings/' segment missing."); + } + + $relevantPath = substr($path, $settingsIndex + strlen('/settings/')); + + $pathParts = explode('/', $relevantPath); + + if (count($pathParts) < 3) { + throw new InvalidArgumentException("Invalid settings URL format: Expected 'type/category/fileName'."); + } + + $this->type = $pathParts[0]; + $this->category = $pathParts[1]; + $this->fileName = implode('/', array_slice($pathParts, 2)); + } + + /** + * Get the setting type from the URL. + * + * @return string + */ + public function getType(): string { + return $this->type; + } + + /** + * Get the setting category from the URL. + * + * @return string + */ + public function getCategory(): string { + return $this->category; + } + + /** + * Get the original filename from the URL. + * + * @return string + */ + public function getFileName(): string { + return $this->fileName; + } + + /** + * Get the raw URL. + * + * @return string + */ + public function getRawUrl(): string { + return $this->rawUrl; + } } diff --git a/src/components/CoolFrame.vue b/src/components/CoolFrame.vue index 442b5a3b82..1e01737623 100644 --- a/src/components/CoolFrame.vue +++ b/src/components/CoolFrame.vue @@ -1,4 +1,7 @@ - +