Repository navigation
Expand file tree
/
Copy pathfilenameValidity.ts
More file actions
58 lines (55 loc) · 2.18 KB
/
Copy pathfilenameValidity.ts
File metadata and controls
58 lines (55 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*!
* 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
* @param escape Escape the matched string in the error (only set when used in HTML)
* @param isFolder Whether the filename is for a folder
*/
export function getFilenameValidity(name: string, escape = false, isFolder = false): string {
if (name.trim() === '') {
if (isFolder) {
return t('files', 'Folder name must not be empty.')
}
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:
if (isFolder) {
return t('files', '"{char}" is not allowed inside a folder name.', { char: error.segment }, { escape })
}
return t('files', '"{char}" is not allowed inside a filename.', { char: error.segment }, { escape })
case InvalidFilenameErrorReason.ReservedName:
if (isFolder) {
return t('files', '"{segment}" is a reserved name and not allowed for folder names.', { segment: error.segment }, { escape: false })
}
return t('files', '"{segment}" is a reserved name and not allowed for filenames.', { segment: error.segment }, { escape: false })
case InvalidFilenameErrorReason.Extension:
if (!isFolder && error.segment.match(/\.[a-z]/i)) {
return t('files', '"{extension}" is not an allowed filetype.', { extension: error.segment }, { escape: false })
}
if (isFolder) {
return t('files', 'Folder names must not end with "{extension}".', { extension: error.segment }, { escape: false })
}
return t('files', 'Filenames must not end with "{extension}".', { extension: error.segment }, { escape: false })
default:
if (isFolder) {
return t('files', 'Invalid folder name.')
}
return t('files', 'Invalid filename.')
}
}
}