From 18dcef8ce4b2ec140a2284d785cb3ed99fee3202 Mon Sep 17 00:00:00 2001 From: Hemachandar <132386067+hmacr@users.noreply.github.com> Date: Tue, 26 Aug 2025 20:20:55 +0530 Subject: [PATCH] Include collection attachments in json export (#10010) --- server/queues/tasks/ExportJSONTask.ts | 71 +++++++++++++++++---------- 1 file changed, 44 insertions(+), 27 deletions(-) diff --git a/server/queues/tasks/ExportJSONTask.ts b/server/queues/tasks/ExportJSONTask.ts index 08763e8bb7..5283a023ab 100644 --- a/server/queues/tasks/ExportJSONTask.ts +++ b/server/queues/tasks/ExportJSONTask.ts @@ -72,6 +72,35 @@ export default class ExportJSONTask extends ExportTask { attachments: {}, }; + async function addAttachments(attachments: Attachment[]) { + await Promise.all( + attachments.map(async (attachment) => { + zip.file( + attachment.key, + new Promise((resolve) => { + attachment.buffer.then(resolve).catch((err) => { + Logger.warn(`Failed to read attachment from storage`, { + attachmentId: attachment.id, + teamId: attachment.teamId, + error: err.message, + }); + resolve(Buffer.from("")); + }); + }), + { + date: attachment.updatedAt, + createFolders: true, + } + ); + + output.attachments[attachment.id] = { + ...omit(presentAttachment(attachment), "url"), + key: attachment.key, + }; + }) + ); + } + async function addDocumentTree(nodes: NavigationNode[]) { for (const node of nodes) { const document = await Document.findByPk(node.id, { @@ -82,7 +111,7 @@ export default class ExportJSONTask extends ExportTask { continue; } - const attachments = includeAttachments + const documentAttachments = includeAttachments ? await Attachment.findAll({ where: { teamId: document.teamId, @@ -93,32 +122,7 @@ export default class ExportJSONTask extends ExportTask { }) : []; - await Promise.all( - attachments.map(async (attachment) => { - zip.file( - attachment.key, - new Promise((resolve) => { - attachment.buffer.then(resolve).catch((err) => { - Logger.warn(`Failed to read attachment from storage`, { - attachmentId: attachment.id, - teamId: attachment.teamId, - error: err.message, - }); - resolve(Buffer.from("")); - }); - }), - { - date: attachment.updatedAt, - createFolders: true, - } - ); - - output.attachments[attachment.id] = { - ...omit(presentAttachment(attachment), "url"), - key: attachment.key, - }; - }) - ); + await addAttachments(documentAttachments); output.documents[document.id] = { id: document.id, @@ -146,6 +150,19 @@ export default class ExportJSONTask extends ExportTask { } } + const collectionAttachments = includeAttachments + ? await Attachment.findAll({ + where: { + teamId: collection.teamId, + id: ProsemirrorHelper.parseAttachmentIds( + DocumentHelper.toProsemirror(collection) + ), + }, + }) + : []; + + await addAttachments(collectionAttachments); + if (collection.documentStructure) { await addDocumentTree(collection.documentStructure); }