Skip to content

Commit d681c89

Browse files
R0Wiskjnldsv
authored andcommitted
feat: Implement Directory Check
* Partially implements #27591 Signed-off-by: Robin Windey <ro.windey@gmail.com>
1 parent 6f0255d commit d681c89

File tree

8 files changed

+146
-3
lines changed

8 files changed

+146
-3
lines changed

apps/workflowengine/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
'OCA\\WorkflowEngine\\AppInfo\\Application' => $baseDir . '/../lib/AppInfo/Application.php',
1111
'OCA\\WorkflowEngine\\BackgroundJobs\\Rotate' => $baseDir . '/../lib/BackgroundJobs/Rotate.php',
1212
'OCA\\WorkflowEngine\\Check\\AbstractStringCheck' => $baseDir . '/../lib/Check/AbstractStringCheck.php',
13+
'OCA\\WorkflowEngine\\Check\\Directory' => $baseDir . '/../lib/Check/Directory.php',
1314
'OCA\\WorkflowEngine\\Check\\FileMimeType' => $baseDir . '/../lib/Check/FileMimeType.php',
1415
'OCA\\WorkflowEngine\\Check\\FileName' => $baseDir . '/../lib/Check/FileName.php',
1516
'OCA\\WorkflowEngine\\Check\\FileSize' => $baseDir . '/../lib/Check/FileSize.php',

apps/workflowengine/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class ComposerStaticInitWorkflowEngine
2525
'OCA\\WorkflowEngine\\AppInfo\\Application' => __DIR__ . '/..' . '/../lib/AppInfo/Application.php',
2626
'OCA\\WorkflowEngine\\BackgroundJobs\\Rotate' => __DIR__ . '/..' . '/../lib/BackgroundJobs/Rotate.php',
2727
'OCA\\WorkflowEngine\\Check\\AbstractStringCheck' => __DIR__ . '/..' . '/../lib/Check/AbstractStringCheck.php',
28+
'OCA\\WorkflowEngine\\Check\\Directory' => __DIR__ . '/..' . '/../lib/Check/Directory.php',
2829
'OCA\\WorkflowEngine\\Check\\FileMimeType' => __DIR__ . '/..' . '/../lib/Check/FileMimeType.php',
2930
'OCA\\WorkflowEngine\\Check\\FileName' => __DIR__ . '/..' . '/../lib/Check/FileName.php',
3031
'OCA\\WorkflowEngine\\Check\\FileSize' => __DIR__ . '/..' . '/../lib/Check/FileSize.php',
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
8+
*/
9+
namespace OCA\WorkflowEngine\Check;
10+
11+
use OCA\WorkflowEngine\Entity\File;
12+
use OCP\IL10N;
13+
use OCP\WorkflowEngine\IFileCheck;
14+
15+
class Directory extends AbstractStringCheck implements IFileCheck {
16+
use TFileCheck;
17+
18+
/**
19+
* @param IL10N $l
20+
*/
21+
public function __construct(
22+
IL10N $l,
23+
) {
24+
parent::__construct($l);
25+
}
26+
27+
/**
28+
* @return string
29+
*/
30+
protected function getActualValue(): string {
31+
if ($this->path === null) {
32+
return '';
33+
}
34+
// files/some/path -> some/path
35+
return preg_replace('/^files\//', '', pathinfo($this->path, PATHINFO_DIRNAME));
36+
}
37+
38+
/**
39+
* @param string $operator
40+
* @param string $checkValue
41+
* @param string $actualValue
42+
* @return bool
43+
*/
44+
protected function executeStringCheck($operator, $checkValue, $actualValue) {
45+
if ($operator === 'is' || $operator === '!is') {
46+
$checkValue = ltrim(rtrim($checkValue, '/'), '/');
47+
}
48+
return parent::executeStringCheck($operator, $checkValue, $actualValue);
49+
}
50+
51+
public function supportedEntities(): array {
52+
return [ File::class ];
53+
}
54+
55+
public function isAvailableForScope(int $scope): bool {
56+
return true;
57+
}
58+
}

apps/workflowengine/lib/Manager.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
use Doctrine\DBAL\Exception;
1010
use OCA\WorkflowEngine\AppInfo\Application;
11+
use OCA\WorkflowEngine\Check\Directory;
1112
use OCA\WorkflowEngine\Check\FileMimeType;
1213
use OCA\WorkflowEngine\Check\FileName;
1314
use OCA\WorkflowEngine\Check\FileSize;
@@ -692,6 +693,7 @@ protected function getBuildInOperators(): array {
692693
protected function getBuildInChecks(): array {
693694
try {
694695
return [
696+
$this->container->query(Directory::class),
695697
$this->container->query(FileMimeType::class),
696698
$this->container->query(FileName::class),
697699
$this->container->query(FileSize::class),

apps/workflowengine/src/components/Checks/file.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,19 @@ const FileChecks = [
3131
validate: stringValidator,
3232
},
3333

34+
{
35+
class: 'OCA\\WorkflowEngine\\Check\\Directory',
36+
name: t('workflowengine', 'Directory'),
37+
operators: stringOrRegexOperators,
38+
placeholder: (check) => {
39+
if (check.operator === 'matches' || check.operator === '!matches') {
40+
return '/^myfolder/.+$/i'
41+
}
42+
return 'myfolder/subfolder'
43+
},
44+
validate: stringValidator,
45+
},
46+
3447
{
3548
class: 'OCA\\WorkflowEngine\\Check\\FileMimeType',
3649
name: t('workflowengine', 'File MIME type'),
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
6+
* SPDX-License-Identifier: AGPL-3.0-or-later
7+
*/
8+
9+
namespace OCA\WorkflowEngine\Tests\Check;
10+
11+
use OCA\WorkflowEngine\Check\Directory;
12+
use OCA\WorkflowEngine\Entity\File;
13+
use OCP\Files\Storage\IStorage;
14+
use OCP\IL10N;
15+
use Test\TestCase;
16+
17+
class DirectoryTest extends TestCase {
18+
/** @var IL10N */
19+
private $l10n;
20+
21+
/** @var IStorage */
22+
private $storage;
23+
24+
/** @var Directory */
25+
private $directory;
26+
27+
protected function setUp(): void {
28+
parent::setUp();
29+
$this->l10n = $this->createMock(IL10N::class);
30+
$this->storage = $this->createMock(IStorage::class);
31+
$this->directory = new Directory($this->l10n);
32+
}
33+
34+
/**
35+
* @dataProvider dataProviderCheck
36+
*/
37+
public function testExecuteStringCheck(string $operator, string $configuredDirectoryPath, string $filePath, bool $expectedResult): void {
38+
$this->directory->setFileInfo($this->storage, $filePath);
39+
40+
$result = $this->directory->executeCheck($operator, $configuredDirectoryPath);
41+
42+
$this->assertEquals($expectedResult, $result);
43+
}
44+
45+
public function testSupportedEntities(): void {
46+
$this->assertSame([File::class], $this->directory->supportedEntities());
47+
}
48+
49+
public function testIsAvailableForScope(): void {
50+
$this->assertTrue($this->directory->isAvailableForScope(1));
51+
}
52+
53+
public function dataProviderCheck(): array {
54+
return [
55+
['is', 'some/path', 'files/some/path/file.txt', true],
56+
['is', '/some/path/', 'files/some/path/file.txt', true],
57+
58+
['!is', 'some/path', 'files/some/path/file.txt', false],
59+
['!is', 'some/path/', 'files/someother/path/file.txt', true],
60+
61+
['matches', '/^some\/path\/.+$/i', 'files/SomE/PATH/subfolder/file.txt', true],
62+
['matches', '/some\/path\/.*\/sub2/', 'files/some/path/subfolder1/sub2/anotherfile.pdf', true],
63+
64+
['!matches', '/some\/path/', 'files/some/path/file.txt', false],
65+
['!matches', '/some\/path/', 'files/another/path/file.txt', true],
66+
];
67+
}
68+
}

dist/workflowengine-workflowengine.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/workflowengine-workflowengine.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)