Skip to content

Commit

Permalink
Merge pull request #193 from CatchTheTornado/develop
Browse files Browse the repository at this point in the history
[fix] quickfix to #191
  • Loading branch information
pkarw authored Oct 2, 2024
2 parents 6013763 + a1d7250 commit efcff3a
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 19 deletions.
21 changes: 20 additions & 1 deletion src/components/encrypted-attachment-uploader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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[]) => {
Expand All @@ -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,
Expand Down
66 changes: 48 additions & 18 deletions src/components/record-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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());
});
Expand Down Expand Up @@ -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);
}
}
}
Expand Down

0 comments on commit efcff3a

Please sign in to comment.