Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions lib/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,19 +229,6 @@ class Constants {
self::ANSWER_GRID_TYPE_RADIO,
];

public const FILENAME_INVALID_CHARS = [
"\n",
'/',
'\\',
':',
'*',
'?',
'"',
'<',
'>',
'|',
];

/**
* !! Keep in sync with src/mixins/ShareTypes.js !!
*/
Expand Down
4 changes: 3 additions & 1 deletion lib/Helper/FilePathHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,22 @@
use OCA\Forms\Constants;
use OCA\Forms\Db\Form;
use OCP\Files\Folder;
use OCP\Files\IFilenameValidator;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;

class FilePathHelper {
public function __construct(
private readonly IRootFolder $rootFolder,
private readonly IFilenameValidator $fileNameValidator,
) {
}

/**
* Normalize a filename by replacing invalid characters
*/
public function normalizeFileName(string $fileName): string {
return trim(str_replace(Constants::FILENAME_INVALID_CHARS, '-', $fileName));
return $this->fileNameValidator->sanitizeFilename($fileName, '-');
}

/**
Expand Down
7 changes: 6 additions & 1 deletion tests/Unit/Controller/ShareApiControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use OCP\AppFramework\OCS\OCSNotFoundException;
use OCP\Files\File;
use OCP\Files\Folder;
use OCP\Files\IFilenameValidator;
use OCP\Files\IRootFolder;
use OCP\IGroup;
use OCP\IGroupManager;
Expand Down Expand Up @@ -61,6 +62,7 @@ class ShareApiControllerTest extends TestCase {
private CirclesService|MockObject $circlesService;
private FilePathHelper $filePathHelper;
private IRootFolder|MockObject $rootFolder;
private IFilenameValidator|MockObject $filenameValidator;
private IManager|MockObject $shareManager;

public function setUp(): void {
Expand All @@ -76,7 +78,10 @@ public function setUp(): void {
$this->circlesService = $this->createMock(CirclesService::class);

$this->rootFolder = $this->createMock(IRootFolder::class);
$this->filePathHelper = new FilePathHelper($this->rootFolder);
$this->filenameValidator = $this->createMock(IFilenameValidator::class);
$this->filenameValidator->method('sanitizeFilename')
->willReturnCallback(static fn (string $fileName, string $replacement): string => str_replace(['/', '\\'], $replacement, trim($fileName)));
$this->filePathHelper = new FilePathHelper($this->rootFolder, $this->filenameValidator);
$this->shareManager = $this->createMock(IManager::class);

$this->shareApiController = new ShareApiController(
Expand Down
7 changes: 6 additions & 1 deletion tests/Unit/Helper/FilePathHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,24 @@
use OCA\Forms\Db\Form;
use OCA\Forms\Helper\FilePathHelper;
use OCP\Files\Folder;
use OCP\Files\IFilenameValidator;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;

class FilePathHelperTest extends TestCase {
private FilePathHelper $filePathHelper;
private IFilenameValidator|MockObject $filenameValidator;
private IRootFolder|MockObject $rootFolder;

public function setUp(): void {
parent::setUp();
$this->rootFolder = $this->createMock(IRootFolder::class);
$this->filePathHelper = new FilePathHelper($this->rootFolder);
$this->filenameValidator = $this->createMock(IFilenameValidator::class);
$this->filenameValidator->method('sanitizeFilename')
->willReturnCallback(static fn (string $fileName, string $replacement): string => str_replace(['/', '\\'], $replacement, trim($fileName)));
$this->filePathHelper = new FilePathHelper($this->rootFolder, $this->filenameValidator);
}

public function testNormalizeFileName() {
Expand Down
7 changes: 6 additions & 1 deletion tests/Unit/Service/FormsServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ function microtime(bool|float $asFloat = false) {
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\Folder;
use OCP\Files\IFilenameValidator;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
use OCP\IGroup;
Expand Down Expand Up @@ -80,6 +81,7 @@ class FormsServiceTest extends TestCase {
private CirclesService|MockObject $circlesService;
private FilePathHelper|MockObject $filePathHelper;
private IRootFolder|MockObject $rootFolder;
private IFilenameValidator|MockObject $filenameValidator;
private IL10N|MockObject $l10n;
private LoggerInterface|MockObject $logger;

Expand Down Expand Up @@ -112,7 +114,10 @@ public function setUp(): void {
->willReturn($user);

$this->rootFolder = $this->createMock(IRootFolder::class);
$this->filePathHelper = new FilePathHelper($this->rootFolder);
$this->filenameValidator = $this->createMock(IFilenameValidator::class);
$this->filenameValidator->method('sanitizeFilename')
->willReturnCallback(static fn (string $fileName, string $replacement): string => str_replace(['/', '\\', "\n"], $replacement, trim($fileName)));
$this->filePathHelper = new FilePathHelper($this->rootFolder, $this->filenameValidator);

$this->l10n = $this->createMock(IL10N::class);
$this->l10n->expects($this->any())
Expand Down
Loading