diff options
author | Patrick <patrick@westberliner.net> | 2022-05-15 12:53:48 +0300 |
---|---|---|
committer | Patrick <patrick@westberliner.net> | 2022-05-15 12:53:48 +0300 |
commit | dbcdb821bc3632e6db0cfbead5e356c7f6582c60 (patch) | |
tree | 9f3cc8d6f9b843ba868dd3e55469c1f115ba969e /lib | |
parent | c00a4ec2c696dee729f9dfa747853b2fbbecbc5c (diff) |
Add qa tools. CS Fix files.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/AppInfo/Application.php | 35 | ||||
-rw-r--r-- | lib/Controller/ChecksumController.php | 238 | ||||
-rw-r--r-- | lib/Listener/LoadSidebarListener.php | 13 |
3 files changed, 146 insertions, 140 deletions
diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 89c9a5e..7b06252 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -31,20 +31,23 @@ use OCP\AppFramework\Bootstrap\IBootstrap; use OCP\AppFramework\Bootstrap\IRegistrationContext; class Application extends App implements IBootstrap { - public const APP_ID = 'checksum'; - - public function __construct(array $urlParams = []) { - parent::__construct(self::APP_ID, $urlParams); - } - - public function register(IRegistrationContext $context): void { - // Load scripts for sidebar. - $context->registerEventListener( - LoadSidebar::class, - LoadSidebarListener::class - ); - } - - public function boot(IBootContext $context): void { - } + public const APP_ID = 'checksum'; + + /** + * @param array<string> $urlParams + */ + public function __construct(array $urlParams = []) { + parent::__construct(self::APP_ID, $urlParams); + } + + public function register(IRegistrationContext $context): void { + // Load scripts for sidebar. + $context->registerEventListener( + LoadSidebar::class, + LoadSidebarListener::class + ); + } + + public function boot(IBootContext $context): void { + } } diff --git a/lib/Controller/ChecksumController.php b/lib/Controller/ChecksumController.php index 0f7d78d..6a5cd92 100644 --- a/lib/Controller/ChecksumController.php +++ b/lib/Controller/ChecksumController.php @@ -18,121 +18,125 @@ use OCP\L10N\IFactory; class ChecksumController extends Controller { - /** - * @var IL10N - */ - private $language; - - /** - * @var IRootFolder - */ - private $rootFolder; - - /** - * @var IUserSession - */ - private $userSession; - - /** - * ChecksumController constructor. - * - * @param string $appName - * @param IRequest $request - * @param IFactory $languageFactory - * @param IRootFolder $rootFolder - * @param IUserSession $userSession - */ - public function __construct( - string $appName, - IRequest $request, - IFactory $languageFactory, - IRootFolder $rootFolder, - IUserSession $userSession - ) { - parent::__construct($appName, $request); - - $this->language = $languageFactory->get('checksum'); - $this->rootFolder = $rootFolder; - $this->userSession = $userSession; - } - - /** - * Compute the hash of a file. - * - * @NoAdminRequired - * @param string $source file path relative to user home - * @param string $type hash algorithm - * @return JSONResponse - */ - public function check(string $source, string $type): JSONResponse { - if (!$this->checkAlgorithmType($type)) { - return new JSONResponse( - [ - 'response' => 'error', - 'msg' => $this->language->t( - 'The algorithm type "%s" is not a valid or supported algorithm type.', - [$type] - ) - ] - ); - } - - $hash = $this->getHash($source, $type); - if ($hash) { - return new JSONResponse( - [ - 'response' => 'success', - 'msg' => $hash - ] - ); - } - - return new JSONResponse( - [ - 'response' => 'error', - 'msg' => $this->language->t('File not found.') - ] - ); - } - - private function getHash(string $source, string $type): ?string { - $user = $this->userSession->getUser(); - if (!$user) { - return null; - } - - try { - $home = $this->rootFolder->getUserFolder($user->getUID()); - $node = $home->get($source); - } catch (NotPermittedException | NoUserException | NotFoundException $e) { - return null; - } - - if ($node->getType() !== FileInfo::TYPE_FILE) { - return null; - } - - $file = $node->fopen('rb'); - $hash = hash_init($type); - hash_update_stream($hash, $file); - fclose($file); - - return hash_final($hash); - } - - private function checkAlgorithmType(string $type): bool { - return in_array($type, $this->getAllowedAlgorithmTypes()) && in_array($type, hash_algos()); - } - - private function getAllowedAlgorithmTypes(): array { - return [ - 'md5', - 'sha1', - 'sha256', - 'sha384', - 'sha512', - 'crc32', - 'crc32b' - ]; - } + /** + * @var IL10N + */ + private $language; + + /** + * @var IRootFolder + */ + private $rootFolder; + + /** + * @var IUserSession + */ + private $userSession; + + /** + * ChecksumController constructor. + * + * @param string $appName + * @param IRequest $request + * @param IFactory $languageFactory + * @param IRootFolder $rootFolder + * @param IUserSession $userSession + */ + public function __construct( + string $appName, + IRequest $request, + IFactory $languageFactory, + IRootFolder $rootFolder, + IUserSession $userSession + ) { + parent::__construct($appName, $request); + + $this->language = $languageFactory->get('checksum'); + $this->rootFolder = $rootFolder; + $this->userSession = $userSession; + } + + /** + * Compute the hash of a file. + * + * @NoAdminRequired + * @param string $source file path relative to user home + * @param string $type hash algorithm + * @return JSONResponse + */ + public function check(string $source, string $type): JSONResponse { + if (!$this->checkAlgorithmType($type)) { + return new JSONResponse( + [ + 'response' => 'error', + 'msg' => $this->language->t( + 'The algorithm type "%s" is not a valid or supported algorithm type.', + [$type] + ) + ] + ); + } + + $hash = $this->getHash($source, $type); + if ($hash) { + return new JSONResponse( + [ + 'response' => 'success', + 'msg' => $hash + ] + ); + } + + return new JSONResponse( + [ + 'response' => 'error', + 'msg' => $this->language->t('File not found.') + ] + ); + } + + private function getHash(string $source, string $type): ?string { + $user = $this->userSession->getUser(); + if (!$user) { + return null; + } + + try { + $home = $this->rootFolder->getUserFolder($user->getUID()); + /** @var \OC\Files\Node\File $node */ + $node = $home->get($source); + } catch (NotPermittedException | NoUserException | NotFoundException $e) { + return null; + } + + if ($node->getType() !== FileInfo::TYPE_FILE) { + return null; + } + + $file = $node->fopen('rb'); + $hash = hash_init($type); + hash_update_stream($hash, $file); + fclose($file); + + return hash_final($hash); + } + + private function checkAlgorithmType(string $type): bool { + return in_array($type, $this->getAllowedAlgorithmTypes()) && in_array($type, hash_algos()); + } + + /** + * @return array<string> + */ + private function getAllowedAlgorithmTypes(): array { + return [ + 'md5', + 'sha1', + 'sha256', + 'sha384', + 'sha512', + 'crc32', + 'crc32b' + ]; + } } diff --git a/lib/Listener/LoadSidebarListener.php b/lib/Listener/LoadSidebarListener.php index 381d735..1925f3d 100644 --- a/lib/Listener/LoadSidebarListener.php +++ b/lib/Listener/LoadSidebarListener.php @@ -30,12 +30,11 @@ use OCP\EventDispatcher\IEventListener; use OCP\Util; class LoadSidebarListener implements IEventListener { + public function handle(Event $event): void { + if (!($event instanceof LoadSidebar)) { + return; + } - public function handle(Event $event): void { - if (!($event instanceof LoadSidebar)) { - return; - } - - Util::addScript(Application::APP_ID, 'checksum.main'); - } + Util::addScript(Application::APP_ID, 'checksum.main'); + } } |