Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/westberliner/checksum.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Herzberg <patrick@westberliner.net>2020-12-10 23:07:22 +0300
committerGitHub <noreply@github.com>2020-12-10 23:07:22 +0300
commit6aea96a5c922bc71c3bcae69ff80bfdf99b496af (patch)
treeb2711fe183f2c92df68f31a3a023b06aac2b5fd8
parent9f073f18b63821d29d623bb010b8ff67c297e995 (diff)
parent75b0f24e17b80ecce2e4bddf55084aa9e7c9c44b (diff)
Merge pull request #47 from st3iny/fix/46/nextcloud-compatibility
Refactor deprecations, improve code style and drop owncloud support
-rw-r--r--README.md2
-rw-r--r--appinfo/app.php13
-rw-r--r--appinfo/info.xml4
-rw-r--r--lib/AppInfo/Application.php50
-rw-r--r--lib/Controller/ChecksumController.php197
-rw-r--r--lib/Listener/LoadAdditionalScriptsListener.php41
6 files changed, 218 insertions, 89 deletions
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 @@
-<?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/info.xml b/appinfo/info.xml
index 8604ed1..b9f3051 100644
--- a/appinfo/info.xml
+++ b/appinfo/info.xml
@@ -28,7 +28,7 @@
<bugs>https://github.com/westberliner/owncloud-checksum/issues</bugs>
<screenshot>https://raw.githubusercontent.com/westberliner/owncloud-checksum/master/screenshots/checksum.gif</screenshot>
<dependencies>
- <owncloud min-version="9.0" max-version="11" />
- <nextcloud min-version="9" max-version="19" />
+ <php min-version="7.4" />
+ <nextcloud min-version="20" max-version="21" />
</dependencies>
</info>
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 @@
+<?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..8953ba1 100644
--- a/lib/Controller/ChecksumController.php
+++ b/lib/Controller/ChecksumController.php
@@ -1,82 +1,133 @@
<?php
+
+declare(strict_types=1);
+
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 {
- protected $language;
-
- public function __construct($appName, IRequest $request) {
-
- parent::__construct($appName, $request);
-
- // get i10n
- $this->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 @@
+<?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');
+ }
+}