Browse Source

feat(nc-gui): bring rename attachment before upload

pull/4931/head
Wing-Kam Wong 2 years ago
parent
commit
4dc13640b4
  1. 24
      packages/nc-gui/components/cell/attachment/utils.ts

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

@ -88,7 +88,8 @@ export const [useProvideAttachmentCell, useAttachmentCell] = useInjectionState(
new Promise<AttachmentProps>((resolve) => { new Promise<AttachmentProps>((resolve) => {
const res: AttachmentProps = { ...file, file, title: file.name, mimetype: file.type } const res: AttachmentProps = { ...file, file, title: file.name, mimetype: file.type }
if (isImage(file.name, (<any>file).mimetype ?? file.type)) { renameFile(file).then((renamedFile) => {
if (isImage(renamedFile.name, (<any>renamedFile).mimetype ?? renamedFile.type)) {
const reader = new FileReader() const reader = new FileReader()
reader.onload = (e) => { reader.onload = (e) => {
@ -105,6 +106,7 @@ export const [useProvideAttachmentCell, useAttachmentCell] = useInjectionState(
} else { } else {
resolve(res) resolve(res)
} }
})
}), }),
), ),
) )
@ -116,7 +118,7 @@ export const [useProvideAttachmentCell, useAttachmentCell] = useInjectionState(
const newAttachments = [] const newAttachments = []
for (const file of selectedFiles) { for (let file of selectedFiles) {
// verify mime type // verify mime type
const attachmentMeta = typeof column.value?.meta === 'string' ? JSON.parse(column.value.meta) : column.value?.meta const attachmentMeta = typeof column.value?.meta === 'string' ? JSON.parse(column.value.meta) : column.value?.meta
if (attachmentMeta.unsupportedAttachmentMimeTypes.includes(file.type)) { if (attachmentMeta.unsupportedAttachmentMimeTypes.includes(file.type)) {
@ -124,6 +126,8 @@ export const [useProvideAttachmentCell, useAttachmentCell] = useInjectionState(
continue continue
} }
file = await renameFile(file)
try { try {
const data = await api.storage.upload( const data = await api.storage.upload(
{ {
@ -143,19 +147,23 @@ export const [useProvideAttachmentCell, useAttachmentCell] = useInjectionState(
updateModelValue(JSON.stringify([...attachments.value, ...newAttachments])) updateModelValue(JSON.stringify([...attachments.value, ...newAttachments]))
} }
async function renameFile(file: Record<string, any>) { async function renameFile(file: File) {
return new Promise((resolve) => { return new Promise<File>((resolve) => {
const { close } = useDialog(RenameFile, { const { close } = useDialog(RenameFile, {
fileName: file.title, fileName: file.name,
fileNames: [...attachments.value.map((file) => file.title), ...storedFiles.value.map((file) => file.title)], fileNames: [...attachments.value.map((file) => file.title), ...storedFiles.value.map((file) => file.title)],
onRename: (newName: string) => { onRename: (newName: string) => {
close() close()
// TODO: update name resolve(
resolve(true) new File([file], newName, {
type: file.type,
lastModified: file.lastModified,
}),
)
}, },
onCancel: () => { onCancel: () => {
close() close()
resolve(true) resolve(file)
}, },
}) })
}) })

Loading…
Cancel
Save