Skip to content

Commit e46c36a

Browse files
committed
fix(files_sharing): show message when nickname is not valid
Signed-off-by: skjnldsv <skjnldsv@protonmail.com>
1 parent 8f2d3fc commit e46c36a

2 files changed

Lines changed: 64 additions & 4 deletions

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*!
2+
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
3+
* SPDX-License-Identifier: AGPL-3.0-or-later
4+
*/
5+
import { InvalidFilenameError, InvalidFilenameErrorReason, validateFilename } from '@nextcloud/files'
6+
import { t } from '@nextcloud/l10n'
7+
8+
/**
9+
* Get the validity of a filename (empty if valid).
10+
* This can be used for `setCustomValidity` on input elements
11+
* @param name The filename
12+
* @param escape Escape the matched string in the error (only set when used in HTML)
13+
*/
14+
export function getGuestNameValidity(name: string, escape = false): string {
15+
if (name.trim() === '') {
16+
return t('files', 'Filename must not be empty.')
17+
}
18+
19+
if (name.startsWith('.')) {
20+
return t('files', 'Names must not start with a dot.')
21+
}
22+
23+
try {
24+
validateFilename(name)
25+
return ''
26+
} catch (error) {
27+
if (!(error instanceof InvalidFilenameError)) {
28+
throw error
29+
}
30+
31+
switch (error.reason) {
32+
case InvalidFilenameErrorReason.Character:
33+
return t('files', '"{char}" is not allowed inside a name.', { char: error.segment }, undefined, { escape })
34+
case InvalidFilenameErrorReason.ReservedName:
35+
return t('files', '"{segment}" is a reserved name and not allowed.', { segment: error.segment }, undefined, { escape: false })
36+
case InvalidFilenameErrorReason.Extension:
37+
if (error.segment.match(/\.[a-z]/i)) {
38+
return t('files', '"{extension}" is not an allowed name.', { extension: error.segment }, undefined, { escape: false })
39+
}
40+
return t('files', 'Names must not end with "{extension}".', { extension: error.segment }, undefined, { escape: false })
41+
default:
42+
return t('files', 'Invalid name.')
43+
}
44+
}
45+
}

apps/files_sharing/src/views/PublicAuthPrompt.vue

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,14 @@
3535

3636
<script lang="ts">
3737
import { defineComponent } from 'vue'
38+
import { loadState } from '@nextcloud/initial-state'
3839
import { t } from '@nextcloud/l10n'
3940
40-
import NcDialog from '@nextcloud/vue/dist/Components/NcDialog.js'
41-
import NcNoteCard from '@nextcloud/vue/dist/Components/NcNoteCard.js'
42-
import NcTextField from '@nextcloud/vue/dist/Components/NcTextField.js'
43-
import { loadState } from '@nextcloud/initial-state'
41+
import NcDialog from '@nextcloud/vue/components/NcDialog'
42+
import NcNoteCard from '@nextcloud/vue/components/NcNoteCard'
43+
import NcTextField from '@nextcloud/vue/components/NcTextField'
44+
45+
import { getGuestNameValidity } from '../services/GuestNameValidity'
4446
4547
export default defineComponent({
4648
name: 'PublicAuthPrompt',
@@ -101,6 +103,19 @@ export default defineComponent({
101103
},
102104
immediate: true,
103105
},
106+
107+
name() {
108+
// Check validity of the new name
109+
const newName = this.name.trim?.() || ''
110+
const input = (this.$refs.input as Vue|undefined)?.$el.querySelector('input')
111+
if (!input) {
112+
return
113+
}
114+
115+
const validity = getGuestNameValidity(newName)
116+
input.setCustomValidity(validity)
117+
input.reportValidity()
118+
},
104119
},
105120
})
106121
</script>

0 commit comments

Comments
 (0)