-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(files): Allow to configure Windows filename compatibility in the…
… settings This adds an admin setting to toggle Windows filename compatibility. Signed-off-by: Ferdinand Thiessen <[email protected]>
- Loading branch information
Showing
10 changed files
with
214 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
apps/files/lib/Listener/DeclarativeSettingsGetValueEventListener.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
namespace OCA\Files\Listener; | ||
|
||
use OCA\Files\AppInfo\Application; | ||
use OCA\Files\Service\SettingsService; | ||
use OCP\EventDispatcher\Event; | ||
use OCP\EventDispatcher\IEventListener; | ||
use OCP\Settings\Events\DeclarativeSettingsGetValueEvent; | ||
|
||
/** @template-implements IEventListener<DeclarativeSettingsGetValueEvent> */ | ||
class DeclarativeSettingsGetValueEventListener implements IEventListener { | ||
|
||
public function __construct( | ||
private SettingsService $service, | ||
) { | ||
} | ||
|
||
public function handle(Event $event): void { | ||
if (!($event instanceof DeclarativeSettingsGetValueEvent)) { | ||
return; | ||
} | ||
|
||
if ($event->getApp() !== Application::APP_ID) { | ||
return; | ||
} | ||
|
||
$event->setValue( | ||
match($event->getFieldId()) { | ||
'windows_support' => $this->service->hasFilesWindowsSupport(), | ||
} | ||
); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
apps/files/lib/Listener/DeclarativeSettingsRegisterFormEventListener.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
namespace OCA\Files\Listener; | ||
|
||
use OCA\Files\AppInfo\Application; | ||
use OCP\EventDispatcher\Event; | ||
use OCP\EventDispatcher\IEventListener; | ||
use OCP\IL10N; | ||
use OCP\Settings\DeclarativeSettingsTypes; | ||
use OCP\Settings\Events\DeclarativeSettingsRegisterFormEvent; | ||
|
||
/** @template-implements IEventListener<DeclarativeSettingsRegisterFormEvent> */ | ||
class DeclarativeSettingsRegisterFormEventListener implements IEventListener { | ||
|
||
public function __construct( | ||
private IL10N $l, | ||
) { | ||
} | ||
|
||
public function handle(Event $event): void { | ||
if (!($event instanceof DeclarativeSettingsRegisterFormEvent)) { | ||
return; | ||
} | ||
|
||
$event->registerSchema(Application::APP_ID, [ | ||
'id' => 'files-filename-support', | ||
'priority' => 10, | ||
'section_type' => DeclarativeSettingsTypes::SECTION_TYPE_ADMIN, | ||
'section_id' => 'server', | ||
'storage_type' => DeclarativeSettingsTypes::STORAGE_TYPE_EXTERNAL, | ||
'title' => $this->l->t('Files compatibility'), | ||
'description' => $this->l->t('Allow to restrict filenames to ensure files can be synced with all clients. By default all filenames valid on POSIX (e.g. Linux or macOS) are allowed.'), | ||
|
||
'fields' => [ | ||
[ | ||
'id' => 'windows_support', | ||
'title' => $this->l->t('Enforce Windows compatibility'), | ||
'description' => $this->l->t('This will block filenames not valid on Windows systems, like using reserved names or special characters. But this will not enforce compatibility of case sensitivity.'), | ||
'type' => DeclarativeSettingsTypes::CHECKBOX, | ||
'default' => false, | ||
], | ||
], | ||
]); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
apps/files/lib/Listener/DeclarativeSettingsSetValueEventListener.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
namespace OCA\Files\Listener; | ||
|
||
use OCA\Files\AppInfo\Application; | ||
use OCA\Files\Service\SettingsService; | ||
use OCP\EventDispatcher\Event; | ||
use OCP\EventDispatcher\IEventListener; | ||
use OCP\Settings\Events\DeclarativeSettingsSetValueEvent; | ||
|
||
/** @template-implements IEventListener<DeclarativeSettingsSetValueEvent> */ | ||
class DeclarativeSettingsSetValueEventListener implements IEventListener { | ||
|
||
public function __construct( | ||
private SettingsService $service, | ||
) { | ||
} | ||
|
||
public function handle(Event $event): void { | ||
if (!($event instanceof DeclarativeSettingsSetValueEvent)) { | ||
return; | ||
} | ||
|
||
if ($event->getApp() !== Application::APP_ID) { | ||
return; | ||
} | ||
|
||
switch ($event->getFieldId()) { | ||
case 'windows_support': | ||
$this->service->setFilesWindowsSupport((bool) $event->getValue()); | ||
$event->stopPropagation(); | ||
break; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
namespace OCA\Files\Service; | ||
|
||
use OC\Files\FilenameValidator; | ||
use OCP\IConfig; | ||
use Psr\Log\LoggerInterface; | ||
|
||
class SettingsService { | ||
|
||
protected const WINDOWS_EXTENSION = [ | ||
' ', | ||
'.', | ||
]; | ||
|
||
protected const WINDOWS_BASENAMES = [ | ||
'con', 'prn', 'aux', 'nul', 'com0', 'com1', 'com2', 'com3', 'com4', 'com5', | ||
'com6', 'com7', 'com8', 'com9', 'com¹', 'com²', 'com³', 'lpt0', 'lpt1', 'lpt2', | ||
'lpt3', 'lpt4', 'lpt5', 'lpt6', 'lpt7', 'lpt8', 'lpt9', 'lpt¹', 'lpt²', 'lpt³', | ||
]; | ||
|
||
protected const WINDOWS_CHARACTERS = [ | ||
'<', '>', ':', | ||
'"', '|', '?', | ||
'*', | ||
]; | ||
|
||
public function __construct( | ||
private IConfig $config, | ||
private FilenameValidator $filenameValidator, | ||
private LoggerInterface $logger, | ||
) { | ||
} | ||
|
||
public function hasFilesWindowsSupport(): bool { | ||
return empty(array_diff(self::WINDOWS_BASENAMES, $this->filenameValidator->getForbiddenBasenames())) | ||
&& empty(array_diff(self::WINDOWS_CHARACTERS, $this->filenameValidator->getForbiddenCharacters())) | ||
&& empty(array_diff(self::WINDOWS_EXTENSION, $this->filenameValidator->getForbiddenExtensions())); | ||
} | ||
|
||
public function setFilesWindowsSupport(bool $enabled = true): void { | ||
if ($enabled) { | ||
$basenames = array_unique(array_merge(self::WINDOWS_BASENAMES, $this->filenameValidator->getForbiddenBasenames())); | ||
$characters = array_unique(array_merge(self::WINDOWS_CHARACTERS, $this->filenameValidator->getForbiddenCharacters())); | ||
$extensions = array_unique(array_merge(self::WINDOWS_EXTENSION, $this->filenameValidator->getForbiddenExtensions())); | ||
} else { | ||
$basenames = array_unique(array_values(array_diff($this->filenameValidator->getForbiddenBasenames(), self::WINDOWS_BASENAMES))); | ||
$characters = array_unique(array_values(array_diff($this->filenameValidator->getForbiddenCharacters(), self::WINDOWS_CHARACTERS))); | ||
$extensions = array_unique(array_values(array_diff($this->filenameValidator->getForbiddenExtensions(), self::WINDOWS_EXTENSION))); | ||
} | ||
$values = [ | ||
'forbidden_filename_basenames' => empty($basenames) ? null : $basenames, | ||
'forbidden_filename_characters' => empty($characters) ? null : $characters, | ||
'forbidden_filename_extensions' => empty($extensions) ? null : $extensions, | ||
]; | ||
$this->config->setSystemValues($values); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters