Browse Source

fix(nc-gui): allow duplicate file upload

pull/7605/head
Ramesh Mane 9 months ago
parent
commit
043da9e5bb
  1. 34
      packages/nc-gui/components/cell/attachment/utils.ts

34
packages/nc-gui/components/cell/attachment/utils.ts

@ -157,21 +157,11 @@ export const [useProvideAttachmentCell, useAttachmentCell] = useInjectionState(
} }
if (selectedFiles.length) { if (selectedFiles.length) {
// this prevent file with same names
const isFileNameAlreadyExist = attachments.value.some((el) => el.title === (file as File).name)
if (isFileNameAlreadyExist) {
message.error(
t('labels.duplicateAttachment', {
filename: (file as File).name,
}),
)
return
}
files.push(file as File) files.push(file as File)
} else { } else {
let fileName = `image.${(file as AttachmentReqType).mimetype?.split('/')[1]}` let fileName = (file as AttachmentReqType).fileName ?? ''
let count = 1 let count = 1
while (attachments.value.some((el) => el.title === fileName)) { while ([...attachments.value, ...imageUrls].some((el) => el.title === fileName)) {
fileName = fileName.replace(/(.+?)(\.[^.]+)$/, `$1(${count})$2`) fileName = fileName.replace(/(.+?)(\.[^.]+)$/, `$1(${count})$2`)
count++ count++
} }
@ -179,7 +169,7 @@ export const [useProvideAttachmentCell, useAttachmentCell] = useInjectionState(
} }
} }
if (isPublic.value && isForm.value) { if (files.length && isPublic.value && isForm.value) {
const newFiles = await Promise.all<AttachmentType>( const newFiles = await Promise.all<AttachmentType>(
Array.from(files).map( Array.from(files).map(
(file) => (file) =>
@ -196,7 +186,6 @@ export const [useProvideAttachmentCell, useAttachmentCell] = useInjectionState(
reader.onload = (e) => { reader.onload = (e) => {
res.data = e.target?.result res.data = e.target?.result
resolve(res) resolve(res)
} }
@ -213,6 +202,10 @@ export const [useProvideAttachmentCell, useAttachmentCell] = useInjectionState(
) )
attachments.value = [...attachments.value, ...newFiles] attachments.value = [...attachments.value, ...newFiles]
return updateModelValue(attachments.value)
} else if (isPublic.value && isForm.value) {
attachments.value = [...attachments.value, ...imageUrls.map((item) => ({ ...item, title: item.fileName }))]
return updateModelValue(attachments.value) return updateModelValue(attachments.value)
} }
@ -226,7 +219,16 @@ export const [useProvideAttachmentCell, useAttachmentCell] = useInjectionState(
files, files,
}, },
) )
newAttachments.push(...data) // add suffix in duplicate file title
for (const uploadedFile of data) {
let fileName = uploadedFile?.title
let count = 1
while ([...attachments.value, ...newAttachments].some((el) => el.title === fileName)) {
fileName = fileName.replace(/(.+?)(\.[^.]+)$/, `$1(${count})$2`)
count++
}
newAttachments.push({ ...uploadedFile, title: fileName })
}
} catch (e: any) { } catch (e: any) {
message.error(e.message || t('msg.error.internalError')) message.error(e.message || t('msg.error.internalError'))
} }
@ -275,7 +277,7 @@ export const [useProvideAttachmentCell, useAttachmentCell] = useInjectionState(
event.preventDefault() event.preventDefault()
// Sanitize the dataTransfer HTML string // Sanitize the dataTransfer HTML string
const sanitizedHtml = DOMPurify.sanitize(event.dataTransfer?.getData('text/html')) ?? '' const sanitizedHtml = DOMPurify.sanitize(event.dataTransfer?.getData('text/html') ?? '') ?? ''
const imageUrl = extractImageSrcFromRawHtml(sanitizedHtml) ?? '' const imageUrl = extractImageSrcFromRawHtml(sanitizedHtml) ?? ''

Loading…
Cancel
Save