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
31 changes: 18 additions & 13 deletions packages/web-pkg/src/components/CreateShortcutModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
</template>
</oc-list>
</oc-drop>
<div v-if="inputFilename" class="flex w-full mt-4">
<div v-show="inputFilename" class="flex w-full mt-4">
<oc-text-input
id="create-shortcut-modal-filename-input"
v-model="inputFilename"
Expand Down Expand Up @@ -126,10 +126,11 @@ import {
useTemplateRef,
watch
} from 'vue'
import { SpaceResource, urlJoin } from '@opencloud-eu/web-client'
import { Resource, SpaceResource, urlJoin } from '@opencloud-eu/web-client'
import {
Modal,
useClientService,
useIsResourceNameValid,
useMessages,
useResourcesStore,
useRouter,
Expand Down Expand Up @@ -164,6 +165,7 @@ const { $gettext } = useGettext()
const { showMessage, showErrorMessage } = useMessages()
const router = useRouter()
const { search } = useSearch()
const { isFileNameValid } = useIsResourceNameValid()

const resourcesStore = useResourcesStore()
const { resources, currentFolder } = storeToRefs(resourcesStore)
Expand All @@ -187,12 +189,19 @@ const dropItemUrl = computed(() => {
return getInputUrlWithProtocol(unref(inputUrl))
})

const fileAlreadyExists = computed(
() => !!unref(resources).find((file) => file.name === `${unref(inputFilename)}.url`)
)
const inputFilenameValidation = computed(() => {
const newName = `${unref(inputFilename)}.url`
const resource = {
path: urlJoin(unref(currentFolder)?.path || '/', newName),
name: newName,
extension: 'url'
} as Resource

return isFileNameValid(resource, newName, unref(resources))
})

const confirmButtonDisabled = computed(
() => unref(fileAlreadyExists) || !unref(inputFilename) || !unref(inputUrl)
() => !unref(inputUrl) || !unref(inputFilename) || !unref(inputFilenameValidation).isValid
)

watch(
Expand All @@ -204,15 +213,11 @@ watch(
)

const inputFileNameErrorMessage = computed(() => {
if (unref(fileAlreadyExists)) {
return $gettext('»%{name}« already exists', { name: `${unref(inputFilename)}.url` })
}

if (/[/]/.test(unref(inputFilename))) {
return $gettext('Shortcut name cannot contain "/"')
if (unref(inputFilenameValidation).isValid) {
return ''
}

return ''
return unref(inputFilenameValidation).error || ''
})

const searchTask = useTask(function* (signal, searchTerm: string) {
Expand Down
18 changes: 18 additions & 0 deletions packages/web-pkg/tests/unit/components/CreateShortcutModal.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,24 @@ import { SearchResource } from '@opencloud-eu/web-client'
import { useMessages, useResourcesStore } from '../../../src/composables/piniaStores'

describe('CreateShortcutModal', () => {
describe('filename validation', () => {
it('disables confirm and shows an error for invalid filename characters', async () => {
const { wrapper } = getWrapper()

;(wrapper.vm as any).inputUrl = 'https://example.org'
;(wrapper.vm as any).inputFilename = 'valid-shortcut'
await wrapper.vm.$nextTick()

expect((wrapper.vm as any).confirmButtonDisabled).toBe(false)

;(wrapper.vm as any).inputFilename = 'invalid\\name'
await wrapper.vm.$nextTick()

expect((wrapper.vm as any).confirmButtonDisabled).toBe(true)
expect((wrapper.vm as any).inputFileNameErrorMessage).toBe('The name cannot contain "\\"')
})
})

describe('method "onConfirm"', () => {
it('should show message on success', async () => {
const { wrapper } = getWrapper()
Expand Down