Skip to content

Commit

Permalink
Implement Directory Check
Browse files Browse the repository at this point in the history
* Partially implements #27591

Signed-off-by: Robin Windey <[email protected]>
  • Loading branch information
R0Wi committed Jan 18, 2025
1 parent 0d3edd2 commit 4654ddb
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
'OCA\\WorkflowEngine\\AppInfo\\Application' => $baseDir . '/../lib/AppInfo/Application.php',
'OCA\\WorkflowEngine\\BackgroundJobs\\Rotate' => $baseDir . '/../lib/BackgroundJobs/Rotate.php',
'OCA\\WorkflowEngine\\Check\\AbstractStringCheck' => $baseDir . '/../lib/Check/AbstractStringCheck.php',
'OCA\\WorkflowEngine\\Check\\Directory' => $baseDir . '/../lib/Check/Directory.php',
'OCA\\WorkflowEngine\\Check\\FileMimeType' => $baseDir . '/../lib/Check/FileMimeType.php',
'OCA\\WorkflowEngine\\Check\\FileName' => $baseDir . '/../lib/Check/FileName.php',
'OCA\\WorkflowEngine\\Check\\FileSize' => $baseDir . '/../lib/Check/FileSize.php',
Expand Down
1 change: 1 addition & 0 deletions apps/workflowengine/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class ComposerStaticInitWorkflowEngine
'OCA\\WorkflowEngine\\AppInfo\\Application' => __DIR__ . '/..' . '/../lib/AppInfo/Application.php',
'OCA\\WorkflowEngine\\BackgroundJobs\\Rotate' => __DIR__ . '/..' . '/../lib/BackgroundJobs/Rotate.php',
'OCA\\WorkflowEngine\\Check\\AbstractStringCheck' => __DIR__ . '/..' . '/../lib/Check/AbstractStringCheck.php',
'OCA\\WorkflowEngine\\Check\\Directory' => __DIR__ . '/..' . '/../lib/Check/Directory.php',
'OCA\\WorkflowEngine\\Check\\FileMimeType' => __DIR__ . '/..' . '/../lib/Check/FileMimeType.php',
'OCA\\WorkflowEngine\\Check\\FileName' => __DIR__ . '/..' . '/../lib/Check/FileName.php',
'OCA\\WorkflowEngine\\Check\\FileSize' => __DIR__ . '/..' . '/../lib/Check/FileSize.php',
Expand Down
58 changes: 58 additions & 0 deletions apps/workflowengine/lib/Check/Directory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\WorkflowEngine\Check;

use OCA\WorkflowEngine\Entity\File;
use OCP\IL10N;
use OCP\WorkflowEngine\IFileCheck;

class Directory extends AbstractStringCheck implements IFileCheck {
use TFileCheck;

/**
* @param IL10N $l
*/
public function __construct(
IL10N $l,
) {
parent::__construct($l);
}

/**
* @return string
*/
protected function getActualValue(): string {
if ($this->path === null) {
return '';
}
// files/some/path -> some/path
return preg_replace('/^files\//', '', pathinfo($this->path, PATHINFO_DIRNAME));
}

/**
* @param string $operator
* @param string $checkValue
* @param string $actualValue
* @return bool
*/
protected function executeStringCheck($operator, $checkValue, $actualValue) {
if ($operator === 'is' || $operator === '!is') {
$checkValue = ltrim(rtrim($checkValue, '/'), '/');
}
return parent::executeStringCheck($operator, $checkValue, $actualValue);
}

public function supportedEntities(): array {
return [ File::class ];
}

public function isAvailableForScope(int $scope): bool {
return true;
}
}
2 changes: 2 additions & 0 deletions apps/workflowengine/lib/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use Doctrine\DBAL\Exception;
use OCA\WorkflowEngine\AppInfo\Application;
use OCA\WorkflowEngine\Check\Directory;
use OCA\WorkflowEngine\Check\FileMimeType;
use OCA\WorkflowEngine\Check\FileName;
use OCA\WorkflowEngine\Check\FileSize;
Expand Down Expand Up @@ -691,6 +692,7 @@ protected function getBuildInOperators(): array {
protected function getBuildInChecks(): array {
try {
return [
$this->container->query(Directory::class),
$this->container->query(FileMimeType::class),
$this->container->query(FileName::class),
$this->container->query(FileSize::class),
Expand Down
13 changes: 13 additions & 0 deletions apps/workflowengine/src/components/Checks/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ const FileChecks = [
validate: stringValidator,
},

{
class: 'OCA\\WorkflowEngine\\Check\\Directory',
name: t('workflowengine', 'Directory'),
operators: stringOrRegexOperators,
placeholder: (check) => {
if (check.operator === 'matches' || check.operator === '!matches') {
return '/^myfolder/.+$/i'
}
return 'myfolder/subfolder'
},
validate: stringValidator,
},

{
class: 'OCA\\WorkflowEngine\\Check\\FileMimeType',
name: t('workflowengine', 'File MIME type'),
Expand Down
68 changes: 68 additions & 0 deletions apps/workflowengine/tests/Check/DirectoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\WorkflowEngine\Tests\Check;

use OCA\WorkflowEngine\Check\Directory;
use OCA\WorkflowEngine\Entity\File;
use OCP\Files\Storage\IStorage;
use OCP\IL10N;
use Test\TestCase;

class DirectoryTest extends TestCase {
/** @var IL10N */
private $l10n;

/** @var IStorage */
private $storage;

/** @var Directory */
private $directory;

protected function setUp(): void {
parent::setUp();
$this->l10n = $this->createMock(IL10N::class);
$this->storage = $this->createMock(IStorage::class);
$this->directory = new Directory($this->l10n);
}

/**
* @dataProvider dataProviderCheck
*/
public function testExecuteStringCheck(string $operator, string $configuredDirectoryPath, string $filePath, bool $expectedResult): void {
$this->directory->setFileInfo($this->storage, $filePath);

$result = $this->directory->executeCheck($operator, $configuredDirectoryPath);

$this->assertEquals($expectedResult, $result);
}

public function testSupportedEntities(): void {
$this->assertSame([File::class], $this->directory->supportedEntities());
}

public function testIsAvailableForScope(): void {
$this->assertTrue($this->directory->isAvailableForScope(1));
}

public function dataProviderCheck(): array {
return [
['is', 'some/path', 'files/some/path/file.txt', true],
['is', '/some/path/', 'files/some/path/file.txt', true],

['!is', 'some/path', 'files/some/path/file.txt', false],
['!is', 'some/path/', 'files/someother/path/file.txt', true],

['matches', '/^some\/path\/.+$/i', 'files/SomE/PATH/subfolder/file.txt', true],
['matches', '/some\/path\/.*\/sub2/', 'files/some/path/subfolder1/sub2/anotherfile.pdf', true],

['!matches', '/some\/path/', 'files/some/path/file.txt', false],
['!matches', '/some\/path/', 'files/another/path/file.txt', true],
];
}
}
4 changes: 2 additions & 2 deletions dist/workflowengine-workflowengine.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/workflowengine-workflowengine.js.map

Large diffs are not rendered by default.

0 comments on commit 4654ddb

Please sign in to comment.