diff options
author | Richard Steinmetz <richard@steinmetz.cloud> | 2020-11-22 19:24:21 +0300 |
---|---|---|
committer | Richard Steinmetz <richard@steinmetz.cloud> | 2020-11-22 19:36:13 +0300 |
commit | 56905458eae30b08e77914d16e89f9feb3f02b2d (patch) | |
tree | 987b100f8955ce60f1431ed7ffe1b132c091852a | |
parent | 9f073f18b63821d29d623bb010b8ff67c297e995 (diff) |
Refactor deprecations, improve code style and drop owncloud support
Signed-off-by: Richard Steinmetz <richard@steinmetz.cloud>
-rw-r--r-- | appinfo/app.php | 13 | ||||
-rw-r--r-- | appinfo/routes.php | 30 | ||||
-rw-r--r-- | lib/AppInfo/Application.php | 50 | ||||
-rw-r--r-- | lib/Controller/ChecksumController.php | 205 | ||||
-rw-r--r-- | lib/Listener/LoadAdditionalScriptsListener.php | 40 |
5 files changed, 253 insertions, 85 deletions
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 @@ -<?php -/** - * Load Javascrip - */ - -use OCP\Util; - -$eventDispatcher = \OC::$server->getEventDispatcher(); -$eventDispatcher->addListener('OCA\Files::loadAdditionalScripts', function(){ - Util::addScript('checksum', 'checksum.tabview' ); - Util::addScript('checksum', 'checksum.plugin' ); -}); - diff --git a/appinfo/routes.php b/appinfo/routes.php index 1359b9c..c77435f 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -1,7 +1,29 @@ <?php + +declare(strict_types=1); + /** - * adding route for ajax callback + * @copyright Copyright (C) 2020 Richard Steinmetz <richard@steinmetz.cloud> + * + * @author westberliner + * @author Richard Steinmetz <richard@steinmetz.cloud> + * + * 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 <https://www.gnu.org/licenses/>. */ -return ['routes' => [ - ['name' => 'checksum#check', 'url' => '/check', 'verb' => 'GET'] -]];
\ No newline at end of file + +return [ + 'routes' => [ + ['name' => 'checksum#check', 'url' => '/check', 'verb' => 'GET'] + ] +]; diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php new file mode 100644 index 0000000..f6b9f5c --- /dev/null +++ b/lib/AppInfo/Application.php @@ -0,0 +1,50 @@ +<?php + +declare(strict_types=1); + +/** + * @copyright Copyright (C) 2020 Richard Steinmetz <richard@steinmetz.cloud> + * + * @author Richard Steinmetz <richard@steinmetz.cloud> + * + * 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 <https://www.gnu.org/licenses/>. + */ + +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..91e7f0f 100644 --- a/lib/Controller/ChecksumController.php +++ b/lib/Controller/ChecksumController.php @@ -1,82 +1,151 @@ <?php + +declare(strict_types=1); + +/** + * @copyright Copyright (C) 2020 Richard Steinmetz <richard@steinmetz.cloud> + * + * @author westberliner + * @author Richard Steinmetz <richard@steinmetz.cloud> + * + * 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 <https://www.gnu.org/licenses/>. + */ + namespace OCA\Checksum\Controller; use OCP\AppFramework\Controller; -use OCP\IRequest; -use OC\Files\Filesystem; use OCP\AppFramework\Http\JSONResponse; - +use OCP\Files\Mount\IMountManager; +use OCP\IL10N; +use OCP\IRequest; +use OCP\IUserSession; +use OCP\L10N\IFactory; class ChecksumController extends Controller { + /** + * @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( + array( + 'response' => 'error', + 'msg' => $this->language->t( + 'The algorithm type "%s" is not a valid or supported algorithm type.', + array($type) + ) + ) + ); + } - protected $language; - - public function __construct($appName, IRequest $request) { - - parent::__construct($appName, $request); + $hash = $this->getHash($source, $type); + if ($hash) { + return new JSONResponse( + array( + 'response' => 'success', + 'msg' => $hash + ) + ); + } else { + return new JSONResponse( + array( + 'response' => 'error', + 'msg' => $this->language->t('File not found.') + ) + ); + } + } - // get i10n - $this->language = \OC::$server->getL10N('checksum'); + private function getHash(string $source, string $type): ?string { + $user = $this->userSession->getUser(); + if (!$user) { + return null; + } + $mount = $this->mountManager->find($user->getUID()); + if (!$mount) { + return null; } - /** - * 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' - ); + $path = '/files/' . $source; + $file = $mount->getStorage()->fopen($path, '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 array( + 'md5', + 'sha1', + 'sha256', + 'sha384', + 'sha512', + 'crc32' + ); + } +} diff --git a/lib/Listener/LoadAdditionalScriptsListener.php b/lib/Listener/LoadAdditionalScriptsListener.php new file mode 100644 index 0000000..bb5dac4 --- /dev/null +++ b/lib/Listener/LoadAdditionalScriptsListener.php @@ -0,0 +1,40 @@ +<?php + +declare(strict_types=1); + +/** + * @copyright Copyright (C) 2020 Richard Steinmetz <richard@steinmetz.cloud> + * + * @author Richard Steinmetz <richard@steinmetz.cloud> + * + * 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 <https://www.gnu.org/licenses/>. + */ + +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'); + } +} |