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 <patrick@westberliner.net>2022-05-15 12:53:48 +0300
committerPatrick <patrick@westberliner.net>2022-05-15 12:53:48 +0300
commitdbcdb821bc3632e6db0cfbead5e356c7f6582c60 (patch)
tree9f3cc8d6f9b843ba868dd3e55469c1f115ba969e /lib/Controller
parentc00a4ec2c696dee729f9dfa747853b2fbbecbc5c (diff)
Add qa tools. CS Fix files.
Diffstat (limited to 'lib/Controller')
-rw-r--r--lib/Controller/ChecksumController.php238
1 files changed, 121 insertions, 117 deletions
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'
+ ];
+ }
}