-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathScanController.php
More file actions
83 lines (76 loc) · 2.71 KB
/
ScanController.php
File metadata and controls
83 lines (76 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
// SPDX-FileCopyrightText: 2025 Lennart Dohmann <lennart.dohmann@gdata.de>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
namespace OCA\GDataVaas\Controller;
use Exception;
use OCA\GDataVaas\AppInfo\Application;
use OCA\GDataVaas\Service\FileService;
use OCA\GDataVaas\Service\VerdictService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\AppFramework\Http\JSONResponse;
use OCP\Files\EntityTooLargeException;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\IAppConfig;
use OCP\IRequest;
use Psr\Log\LoggerInterface;
use VaasSdk\Exceptions\VaasAuthenticationException;
use VaasSdk\Verdict;
class ScanController extends Controller {
private readonly LoggerInterface $logger;
private IAppConfig $config;
private VerdictService $verdictService;
private FileService $fileService;
public function __construct(
$appName,
IRequest $request,
VerdictService $verdictService,
IAppConfig $config,
FileService $fileService,
LoggerInterface $logger,
) {
parent::__construct($appName, $request);
$this->logger = $logger;
$this->config = $config;
$this->verdictService = $verdictService;
$this->fileService = $fileService;
}
/**
* Scans a file for malicious content with G DATA Verdict-as-a-Service and handles the result.
* @param int $fileId
* @return JSONResponse
*/
#[NoAdminRequired]
public function scan(int $fileId): JSONResponse {
try {
$verdict = $this->verdictService->scanFileById($fileId);
if ($verdict->verdict === Verdict::MALICIOUS) {
try {
$this->fileService->setMaliciousPrefixIfActivated($fileId);
$this->fileService->moveFileToQuarantineFolderIfDefined($fileId);
} catch (Exception $e) {
$this->logger->error("Failed to handle malicious file '{$fileId}': {$e->getMessage()}");
}
}
return new JSONResponse(['verdict' => $verdict->verdict->value], 200);
} catch (EntityTooLargeException) {
return new JSONResponse(
['error' => 'File is larger than '
. $this->config->getValueInt(Application::APP_ID, 'maxScanSizeInMB', 256) . 'MB.'], 413);
} catch (NotFoundException) {
return new JSONResponse(['error' => "File $fileId not found"], 404);
} catch (NotPermittedException) {
return new JSONResponse(
['error' => "Current settings do not permit scanning file with ID $fileId"], 403);
} catch (VaasAuthenticationException) {
return new JSONResponse(
['error' => 'Authentication failed. Please check your credentials.'], 401);
} catch (Exception) {
return new JSONResponse(
['error' => "An unexpected error occurred while scanning file $fileId with GData VaaS. Please
check the logs for more information and contact your administrator."], 500);
}
}
}