diff --git a/src/components/encrypted-attachment-uploader.tsx b/src/components/encrypted-attachment-uploader.tsx index 50325d9..1b4be8b 100644 --- a/src/components/encrypted-attachment-uploader.tsx +++ b/src/components/encrypted-attachment-uploader.tsx @@ -285,6 +285,22 @@ export const EncryptedAttachmentUploader = forwardRef< }, [value, uploadQueueSize, dbContext, onUploadError, onUploadSuccess, updateFile]); + const ensureUniqueFileName = (fileName: string, idx: number): string => { + // Find the last dot in the file name to separate the name and extension + const dotIndex = fileName.lastIndexOf('.'); + + // If no dot is found, the file has no extension + if (dotIndex === -1) { + return `${fileName}-${idx}`; + } + + // Separate the base name and extension + const baseName = fileName.substring(0, dotIndex); + const extension = fileName.substring(dotIndex); + + // Return the new file name with the index appended before the extension + return `${baseName}-${idx}${extension}`; + } const onDrop = useCallback( (acceptedFiles: File[], rejectedFiles: FileRejection[]) => { @@ -305,7 +321,10 @@ export const EncryptedAttachmentUploader = forwardRef< let idx = maxIdx + 1; const filesToBeUploaded:UploadedFile[] = [] files.forEach((file) => { - if (newValues.length < maxFiles && newValues.find((f) => f.file.name === file.name) === undefined) { + if (newValues.find((f) => f.file.name === file.name) !== undefined) { // change the file name + file = new File([file], ensureUniqueFileName(file.name, idx) , { type: file.type }); + } + if (newValues.length < maxFiles /*&& newValues.find((f) => f.file.name === file.name) === undefined*/) { let uploadedFile:UploadedFile = { id: '', file: file, diff --git a/src/components/record-form.tsx b/src/components/record-form.tsx index 40195f7..fa38809 100644 --- a/src/components/record-form.tsx +++ b/src/components/record-form.tsx @@ -81,7 +81,7 @@ export default function RecordForm({ folder, mode }: { folder?: Folder, mode?: R const dropZoneConfig = { - maxFiles: 10, + maxFiles: 20, maxSize: 1024 * 1024 * 50, multiple: true, }; @@ -167,7 +167,7 @@ export default function RecordForm({ folder, mode }: { folder?: Folder, mode?: R }); const assignAttachments = async (savedRecord: Record) => { - uploadedAttachments?.forEach(async (attachmentToUpdate) => { + savedRecord.attachments?.forEach(async (attachmentToUpdate) => { attachmentToUpdate.assignedTo = [{ id: savedRecord.id as number, type: "record" }, { id: folderContext?.currentFolder?.id as number, type: "folder" }]; await eaac.put(attachmentToUpdate.toDTO()); }); @@ -204,22 +204,52 @@ export default function RecordForm({ folder, mode }: { folder?: Folder, mode?: R const savedRecord = await recordContext?.updateRecord(pr) as Record; savedRecords.push(savedRecord); } else { - for(const uploadedAttachment of uploadedAttachments) { - pr = new Record({ - folderId: folderContext?.currentFolder?.id as number, - type: 'note', - tags: tags ? tags.map((tag) => tag.text) : [], - description: data.note, - transcription: transcription, - updatedAt: getCurrentTS(), - createdAt: getCurrentTS(), - eventDate: getCurrentTS(), - attachments: [uploadedAttachment] - } as Record) - await pr.updateChecksum(); - const savedRecord = await recordContext?.updateRecord(pr) as Record; - savedRecords.push(savedRecord); - await assignAttachments(savedRecord); + let groupedNonPdfAttachments: EncryptedAttachment[] = []; + + for (const uploadedAttachment of uploadedAttachments) { + if (uploadedAttachment.mimeType === 'application/pdf') { + // Handle each .pdf file as a separate record + let pr = new Record({ + folderId: folderContext?.currentFolder?.id as number, + type: 'note', + tags: tags ? tags.map((tag) => tag.text) : [], + description: data.note, + transcription: transcription, + updatedAt: getCurrentTS(), + createdAt: getCurrentTS(), + eventDate: getCurrentTS(), + attachments: [uploadedAttachment] // Add the PDF attachment as a single attachment + } as Record); + + await pr.updateChecksum(); + const savedRecord = await recordContext?.updateRecord(pr) as Record; + savedRecords.push(savedRecord); + await assignAttachments(savedRecord); + + } else { + // Collect non-PDF attachments to group them into a single record later + groupedNonPdfAttachments.push(uploadedAttachment); + } + } + + // If there are any non-PDF attachments, create a single record for them + if (groupedNonPdfAttachments.length > 0) { + let pr = new Record({ + folderId: folderContext?.currentFolder?.id as number, + type: 'note', + tags: tags ? tags.map((tag) => tag.text) : [], + description: data.note, + transcription: transcription, + updatedAt: getCurrentTS(), + createdAt: getCurrentTS(), + eventDate: getCurrentTS(), + attachments: groupedNonPdfAttachments // Group all non-PDF attachments in one record + } as Record); + + await pr.updateChecksum(); + const savedRecord = await recordContext?.updateRecord(pr) as Record; + savedRecords.push(savedRecord); + await assignAttachments(savedRecord); } } }