-
-
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.
fix(files): Use
@nextcloud/files
filename validation to show more d…
…etails This will enable showing more details what exactly is wrong with the filename. Especially with the new capabilities introduced with Nextcloud 30. Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
- Loading branch information
Showing
2 changed files
with
69 additions
and
61 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/*! | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
import { InvalidFilenameError, InvalidFilenameErrorReason, validateFilename } from '@nextcloud/files' | ||
import { t } from '@nextcloud/l10n' | ||
|
||
/** | ||
* Get the validity of a filename (empty if valid). | ||
* This can be used for `setCustomValidity` on input elements | ||
* @param name The filename | ||
*/ | ||
export function getFilenameValidity(name: string): string { | ||
if (name.trim() === '') { | ||
return t('files', 'Filename must not be empty.') | ||
} | ||
|
||
try { | ||
validateFilename(name) | ||
return '' | ||
} catch (error) { | ||
if (!(error instanceof InvalidFilenameError)) { | ||
throw error | ||
} | ||
|
||
switch (error.reason) { | ||
case InvalidFilenameErrorReason.Character: | ||
return t('files', '"{char}" is not allowed inside a filename.', { char: error.segment }) | ||
case InvalidFilenameErrorReason.ReservedName: | ||
return t('files', '"{segment}" is a reserved name and not allowed for filenames.', { segment: error.segment }) | ||
case InvalidFilenameErrorReason.Extension: | ||
if (error.segment.match(/\.[a-z]/i)) { | ||
return t('files', '"{extension}" is not an allowed filetype.', { extension: error.segment }) | ||
} | ||
return t('files', 'Filenames must not end with "{extension}".', { extension: error.segment }) | ||
default: | ||
return t('files', 'Invalid filename.') | ||
} | ||
} | ||
} |