Skip to content

Commit

Permalink
Merge pull request #31 from GDATASoftwareAG/upload-scanning
Browse files Browse the repository at this point in the history
Upload scanning
  • Loading branch information
lennartdohmann authored Jun 4, 2024
2 parents 2c65f48 + a449c42 commit 3033107
Show file tree
Hide file tree
Showing 16 changed files with 991 additions and 1,052 deletions.
7 changes: 6 additions & 1 deletion .php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<?php

require_once './vendor/autoload.php';

use Nextcloud\CodingStandard\Config;


$finder = (new PhpCsFixer\Finder())
->in(__DIR__)
->exclude([
Expand All @@ -9,6 +14,6 @@
])
;

return (new PhpCsFixer\Config())
return (new Config())
->setFinder($finder)
;
32 changes: 16 additions & 16 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@
// SPDX-License-Identifier: AGPL-3.0-or-later

return [
'resources' => [],
'routes' => [
['name' => 'scan#scan', 'url' => '/scan', 'verb' => 'POST'],
['name' => 'settings#setconfig', 'url' => '/setconfig', 'verb' => 'POST'],
['name' => 'settings#setadvancedconfig', 'url' => '/setadvancedconfig', 'verb' => 'POST'],
['name' => 'settings#setAutoScan', 'url' => '/setAutoScan', 'verb' => 'POST'],
['name' => 'settings#getAutoScan', 'url' => '/getAutoScan', 'verb' => 'GET'],
['name' => 'settings#setScanOnlyNewFiles', 'url' => '/setScanOnlyNewFiles', 'verb' => 'POST'],
['name' => 'settings#getScanOnlyNewFiles', 'url' => '/getScanOnlyNewFiles', 'verb' => 'GET'],
['name' => 'settings#setPrefixMalicious', 'url' => '/setPrefixMalicious', 'verb' => 'POST'],
['name' => 'settings#getPrefixMalicious', 'url' => '/getPrefixMalicious', 'verb' => 'GET'],
['name' => 'settings#getAuthMethod', 'url' => '/getAuthMethod', 'verb' => 'GET'],
['name' => 'settings#setDisableUnscannedTag', 'url' => '/setDisableUnscannedTag', 'verb' => 'POST'],
['name' => 'settings#getDisableUnscannedTag', 'url' => '/getDisableUnscannedTag', 'verb' => 'GET'],
['name' => 'settings#resetAllTags', 'url' => '/resetalltags', 'verb' => 'POST']
]
'resources' => [],
'routes' => [
['name' => 'scan#scan', 'url' => '/scan', 'verb' => 'POST'],
['name' => 'settings#setconfig', 'url' => '/setconfig', 'verb' => 'POST'],
['name' => 'settings#setadvancedconfig', 'url' => '/setadvancedconfig', 'verb' => 'POST'],
['name' => 'settings#setAutoScan', 'url' => '/setAutoScan', 'verb' => 'POST'],
['name' => 'settings#getAutoScan', 'url' => '/getAutoScan', 'verb' => 'GET'],
['name' => 'settings#setScanOnlyNewFiles', 'url' => '/setScanOnlyNewFiles', 'verb' => 'POST'],
['name' => 'settings#getScanOnlyNewFiles', 'url' => '/getScanOnlyNewFiles', 'verb' => 'GET'],
['name' => 'settings#setPrefixMalicious', 'url' => '/setPrefixMalicious', 'verb' => 'POST'],
['name' => 'settings#getPrefixMalicious', 'url' => '/getPrefixMalicious', 'verb' => 'GET'],
['name' => 'settings#getAuthMethod', 'url' => '/getAuthMethod', 'verb' => 'GET'],
['name' => 'settings#setDisableUnscannedTag', 'url' => '/setDisableUnscannedTag', 'verb' => 'POST'],
['name' => 'settings#getDisableUnscannedTag', 'url' => '/getDisableUnscannedTag', 'verb' => 'GET'],
['name' => 'settings#resetAllTags', 'url' => '/resetalltags', 'verb' => 'POST']
]
];
220 changes: 113 additions & 107 deletions lib/Activity/Provider.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* @copyright Copyright (c) 2018 Roeland Jago Douma <[email protected]>
*
Expand All @@ -20,6 +21,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\GDataVaas\Activity;

use OCA\GDataVaas\AppInfo\Application;
Expand All @@ -29,111 +31,115 @@
use OCP\L10N\IFactory;
use Psr\Log\LoggerInterface;

class Provider implements IProvider {
public const TYPE_VIRUS_DETECTED = 'virus_detected';

public const SUBJECT_VIRUS_DETECTED = 'virus_detected';
public const SUBJECT_VIRUS_DETECTED_UPLOAD = 'virus_detected_upload';
public const SUBJECT_VIRUS_DETECTED_SCAN = 'virus_detected_scan';

public const MESSAGE_FILE_DELETED = 'file_deleted';

/** @var IFactory */
private $languageFactory;

/** @var IURLGenerator */
private $urlGenerator;
private LoggerInterface $logger;

public function __construct(IFactory $languageFactory, IURLGenerator $urlGenerator, LoggerInterface $logger) {
$this->languageFactory = $languageFactory;
$this->urlGenerator = $urlGenerator;
$this->logger = $logger;
}

public function parse($language, IEvent $event, IEvent $previousEvent = null) {
if ($event->getApp() !== Application::APP_ID || $event->getType() !== self::TYPE_VIRUS_DETECTED) {
throw new \InvalidArgumentException();
}

$parameters = [];
$subject = '';

if ($event->getSubject() === self::SUBJECT_VIRUS_DETECTED) {
$subject = 'File {file} is infected with {virus}';

$params = $event->getSubjectParameters();
$parameters['virus'] = [
'type' => 'highlight',
'id' => $params[1],
'name' => $params[1],
];

$parameters['file'] = [
'type' => 'highlight',
'id' => $event->getObjectName(),
'name' => basename($event->getObjectName()),
];
$event->setIcon($this->urlGenerator->imagePath('gdatavaas', 'favicon.svg'));

if ($event->getMessage() === self::MESSAGE_FILE_DELETED) {
$event->setParsedMessage('The file has been removed');
}
} elseif ($event->getSubject() === self::SUBJECT_VIRUS_DETECTED_UPLOAD) {
$subject = 'File containing {virus} detected';

$params = $event->getSubjectParameters();
$parameters['virus'] = [
'type' => 'highlight',
'id' => $params[0],
'name' => $params[0],
];

$event->setParsedSubject($subject);
$event->setRichSubject($subject);
$event->setIcon($this->urlGenerator->imagePath('gdatavaas', 'favicon.svg'));

if ($event->getMessage() === self::MESSAGE_FILE_DELETED) {
$event->setParsedMessage('The file has been removed');
}
} elseif ($event->getSubject() === self::SUBJECT_VIRUS_DETECTED_SCAN) {
$subject = 'File {file} is infected with {virus}';

$params = $event->getSubjectParameters();
$parameters['virus'] = [
'type' => 'highlight',
'id' => $params[0],
'name' => $params[0],
];
$parameters['file'] = [
'type' => 'highlight',
'id' => $event->getObjectName(),
'name' => $event->getObjectName(),
];
$event->setIcon($this->urlGenerator->imagePath('gdatavaas', 'favicon.svg'));

if ($event->getMessage() === self::MESSAGE_FILE_DELETED) {
$event->setParsedMessage('The file has been removed');
}
}

$this->setSubjects($event, $subject, $parameters);

return $event;
}

private function setSubjects(IEvent $event, string $subject, array $parameters): void {
$placeholders = $replacements = [];
foreach ($parameters as $placeholder => $parameter) {
$placeholders[] = '{' . $placeholder . '}';
if ($parameter['type'] === 'file') {
$replacements[] = $parameter['path'];
} else {
$replacements[] = $parameter['name'];
}
}

$event->setParsedSubject(str_replace($placeholders, $replacements, $subject))
->setRichSubject($subject, $parameters);
}
class Provider implements IProvider
{
public const TYPE_VIRUS_DETECTED = 'virus_detected';

public const SUBJECT_VIRUS_DETECTED = 'virus_detected';
public const SUBJECT_VIRUS_DETECTED_UPLOAD = 'virus_detected_upload';
public const SUBJECT_VIRUS_DETECTED_SCAN = 'virus_detected_scan';

public const MESSAGE_FILE_DELETED = 'file_deleted';

/** @var IFactory */
private $languageFactory;

/** @var IURLGenerator */
private $urlGenerator;
private LoggerInterface $logger;

public function __construct(IFactory $languageFactory, IURLGenerator $urlGenerator, LoggerInterface $logger)
{
$this->languageFactory = $languageFactory;
$this->urlGenerator = $urlGenerator;
$this->logger = $logger;
}

public function parse($language, IEvent $event, ?IEvent $previousEvent = null)
{
if ($event->getApp() !== Application::APP_ID || $event->getType() !== self::TYPE_VIRUS_DETECTED) {
throw new \InvalidArgumentException();
}

$parameters = [];
$subject = '';

if ($event->getSubject() === self::SUBJECT_VIRUS_DETECTED) {
$subject = 'File {file} is infected with {virus}';

$params = $event->getSubjectParameters();
$parameters['virus'] = [
'type' => 'highlight',
'id' => $params[1],
'name' => $params[1],
];

$parameters['file'] = [
'type' => 'highlight',
'id' => $event->getObjectName(),
'name' => basename($event->getObjectName()),
];
$event->setIcon($this->urlGenerator->imagePath('gdatavaas', 'favicon.svg'));

if ($event->getMessage() === self::MESSAGE_FILE_DELETED) {
$event->setParsedMessage('The file has been removed');
}
} elseif ($event->getSubject() === self::SUBJECT_VIRUS_DETECTED_UPLOAD) {
$subject = 'File containing {virus} detected';

$params = $event->getSubjectParameters();
$parameters['virus'] = [
'type' => 'highlight',
'id' => $params[0],
'name' => $params[0],
];

$event->setParsedSubject($subject);
$event->setRichSubject($subject);
$event->setIcon($this->urlGenerator->imagePath('gdatavaas', 'favicon.svg'));

if ($event->getMessage() === self::MESSAGE_FILE_DELETED) {
$event->setParsedMessage('The file has been removed');
}
} elseif ($event->getSubject() === self::SUBJECT_VIRUS_DETECTED_SCAN) {
$subject = 'File {file} is infected with {virus}';

$params = $event->getSubjectParameters();
$parameters['virus'] = [
'type' => 'highlight',
'id' => $params[0],
'name' => $params[0],
];
$parameters['file'] = [
'type' => 'highlight',
'id' => $event->getObjectName(),
'name' => $event->getObjectName(),
];
$event->setIcon($this->urlGenerator->imagePath('gdatavaas', 'favicon.svg'));

if ($event->getMessage() === self::MESSAGE_FILE_DELETED) {
$event->setParsedMessage('The file has been removed');
}
}

$this->setSubjects($event, $subject, $parameters);

return $event;
}

private function setSubjects(IEvent $event, string $subject, array $parameters): void
{
$placeholders = $replacements = [];
foreach ($parameters as $placeholder => $parameter) {
$placeholders[] = '{' . $placeholder . '}';
if ($parameter['type'] === 'file') {
$replacements[] = $parameter['path'];
} else {
$replacements[] = $parameter['name'];
}
}

$event->setParsedSubject(str_replace($placeholders, $replacements, $subject))
->setRichSubject($subject, $parameters);
}
}
Loading

0 comments on commit 3033107

Please sign in to comment.