From 75b0f24e17b80ecce2e4bddf55084aa9e7c9c44b Mon Sep 17 00:00:00 2001 From: Richard Steinmetz Date: Wed, 9 Dec 2020 18:07:23 +0100 Subject: Refactor deprecations, improve code style and drop owncloud support Signed-off-by: Richard Steinmetz --- README.md | 2 +- appinfo/app.php | 13 -- appinfo/info.xml | 4 +- lib/AppInfo/Application.php | 50 +++++++ lib/Controller/ChecksumController.php | 197 ++++++++++++++++--------- lib/Listener/LoadAdditionalScriptsListener.php | 41 +++++ 6 files changed, 218 insertions(+), 89 deletions(-) delete mode 100644 appinfo/app.php create mode 100644 lib/AppInfo/Application.php create mode 100644 lib/Listener/LoadAdditionalScriptsListener.php diff --git a/README.md b/README.md index 407ffed..c0da838 100644 --- a/README.md +++ b/README.md @@ -27,5 +27,5 @@ Possible algorithms are md5, sha1, sha256, sha384, sha512 and crc32. Compatibility ------------- -- I only tested the app for the current version of owncloud (9.x) and Nextcloud from 10 to 19. +- This app was only tested on Nextcloud v20 to v21 and php v7.4. - I tried to use the current api as much as possible. It should be safe for future versions. diff --git a/appinfo/app.php b/appinfo/app.php deleted file mode 100644 index 10e3c7e..0000000 --- a/appinfo/app.php +++ /dev/null @@ -1,13 +0,0 @@ -getEventDispatcher(); -$eventDispatcher->addListener('OCA\Files::loadAdditionalScripts', function(){ - Util::addScript('checksum', 'checksum.tabview' ); - Util::addScript('checksum', 'checksum.plugin' ); -}); - diff --git a/appinfo/info.xml b/appinfo/info.xml index 8604ed1..b9f3051 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -28,7 +28,7 @@ https://github.com/westberliner/owncloud-checksum/issues https://raw.githubusercontent.com/westberliner/owncloud-checksum/master/screenshots/checksum.gif - - + + diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php new file mode 100644 index 0000000..9feaf47 --- /dev/null +++ b/lib/AppInfo/Application.php @@ -0,0 +1,50 @@ + + * + * @author Richard Steinmetz + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +namespace OCA\Checksum\AppInfo; + +use OCA\Checksum\Listener\LoadAdditionalScriptsListener; +use OCA\Files\Event\LoadAdditionalScriptsEvent; +use OCP\AppFramework\App; +use OCP\AppFramework\Bootstrap\IBootContext; +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( + LoadAdditionalScriptsEvent::class, + LoadAdditionalScriptsListener::class + ); + } + + public function boot(IBootContext $context): void { + } +} diff --git a/lib/Controller/ChecksumController.php b/lib/Controller/ChecksumController.php index da31f59..8953ba1 100644 --- a/lib/Controller/ChecksumController.php +++ b/lib/Controller/ChecksumController.php @@ -1,82 +1,133 @@ language = \OC::$server->getL10N('checksum'); - - } - - /** - * callback function to get md5 hash of a file - * @NoAdminRequired - * @param (string) $source - filename - * @param (string) $type - hash algorithm type - */ - public function check($source, $type) { - if(!$this->checkAlgorithmType($type)) { - return new JSONResponse( - array( - 'response' => 'error', - 'msg' => $this->language->t('The algorithm type "%s" is not a valid or supported algorithm type.', array($type)) - ) - ); - } - - if($hash = $this->getHash($source, $type)){ - return new JSONResponse( - array( - 'response' => 'success', - 'msg' => $hash - ) - ); - } else { - return new JSONResponse( - array( - 'response' => 'error', - 'msg' => $this->language->t('File not found.') - ) - ); - }; - - } - - protected function getHash($source, $type) { - - if($info = Filesystem::getLocalFile($source)) { - return hash_file($type, $info); - } - - return false; - } - - protected function checkAlgorithmType($type) { - $list_algos = hash_algos(); - return in_array($type, $this->getAllowedAlgorithmTypes()) && in_array($type, $list_algos); - } - - protected function getAllowedAlgorithmTypes() { - return array( - 'md5', - 'sha1', - 'sha256', - 'sha384', - 'sha512', - 'crc32' - ); - } + /** + * @var IL10N + */ + private IL10N $language; + + /** + * @var IMountManager + */ + private IMountManager $mountManager; + + /** + * @var IUserSession + */ + private IUserSession $userSession; + + /** + * ChecksumController constructor. + * + * @param string $appName + * @param IRequest $request + * @param IFactory $languageFactory + * @param IMountManager $mountManager + * @param IUserSession $userSession + */ + public function __construct( + string $appName, + IRequest $request, + IFactory $languageFactory, + IMountManager $mountManager, + IUserSession $userSession + ) { + parent::__construct($appName, $request); + + $this->language = $languageFactory->get('checksum'); + $this->mountManager = $mountManager; + $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; + } + + $absPath = $user->getUID() . '/files/' . $source; + $mount = $this->mountManager->find($absPath); + if (!$mount) { + return null; + } + + $internalPath = $mount->getInternalPath($absPath); + $file = $mount->getStorage()->fopen($internalPath, 'rb'); + if (!$file) { + return null; + } + + $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' + ]; + } } - diff --git a/lib/Listener/LoadAdditionalScriptsListener.php b/lib/Listener/LoadAdditionalScriptsListener.php new file mode 100644 index 0000000..9a28498 --- /dev/null +++ b/lib/Listener/LoadAdditionalScriptsListener.php @@ -0,0 +1,41 @@ + + * + * @author Richard Steinmetz + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +namespace OCA\Checksum\Listener; + +use OCA\Files\Event\LoadAdditionalScriptsEvent; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; +use OCP\Util; + +class LoadAdditionalScriptsListener implements IEventListener { + + public function handle(Event $event): void { + if (!($event instanceof LoadAdditionalScriptsEvent)) { + return; + } + + Util::addScript('checksum', 'checksum.tabview'); + Util::addScript('checksum', 'checksum.plugin'); + } +} -- cgit v1.2.3