diff --git a/server/queues/tasks/ExportDocumentTreeTask.ts b/server/queues/tasks/ExportDocumentTreeTask.ts index 383869fc3..083246e05 100644 --- a/server/queues/tasks/ExportDocumentTreeTask.ts +++ b/server/queues/tasks/ExportDocumentTreeTask.ts @@ -63,28 +63,34 @@ export default abstract class ExportDocumentTreeTask extends ExportTask { // reference in the document with the path to the attachment in the zip await Promise.all( attachments.map(async (attachment) => { - try { - Logger.debug("task", `Adding attachment to archive`, { - documentId, - key: attachment.key, - }); + Logger.debug("task", `Adding attachment to archive`, { + documentId, + key: attachment.key, + }); - const dir = path.dirname(pathInZip); - zip.file(path.join(dir, attachment.key), attachment.buffer, { + const dir = path.dirname(pathInZip); + zip.file( + path.join(dir, 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, - }); + } + ); - text = text.replace( - new RegExp(escapeRegExp(attachment.redirectUrl), "g"), - encodeURI(attachment.key) - ); - } catch (err) { - Logger.error( - `Failed to add attachment to archive: ${attachment.key}`, - err - ); - } + text = text.replace( + new RegExp(escapeRegExp(attachment.redirectUrl), "g"), + encodeURI(attachment.key) + ); }) ); diff --git a/server/queues/tasks/ExportJSONTask.ts b/server/queues/tasks/ExportJSONTask.ts index 1f8894be5..5210e8233 100644 --- a/server/queues/tasks/ExportJSONTask.ts +++ b/server/queues/tasks/ExportJSONTask.ts @@ -94,22 +94,28 @@ export default class ExportJSONTask extends ExportTask { await Promise.all( attachments.map(async (attachment) => { - try { - zip.file(attachment.key, attachment.buffer, { + zip.file( + attachment.key, + new Promise((resolve) => { + attachment.buffer.then(resolve).catch((err) => { + Logger.warn(`Failed to read attachment from S3`, { + 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, - }; - } catch (err) { - Logger.error( - `Failed to add attachment to archive: ${attachment.key}`, - err - ); - } + output.attachments[attachment.id] = { + ...omit(presentAttachment(attachment), "url"), + key: attachment.key, + }; }) ); diff --git a/server/routes/api/documents/documents.ts b/server/routes/api/documents/documents.ts index 32947cd80..051670c45 100644 --- a/server/routes/api/documents/documents.ts +++ b/server/routes/api/documents/documents.ts @@ -574,26 +574,32 @@ router.post( await Promise.all( attachments.map(async (attachment) => { - try { - const location = path.join( - "attachments", - `${attachment.id}.${mime.extension(attachment.contentType)}` - ); - zip.file(location, attachment.buffer, { + const location = path.join( + "attachments", + `${attachment.id}.${mime.extension(attachment.contentType)}` + ); + zip.file( + location, + 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, - }); + } + ); - content = content.replace( - new RegExp(escapeRegExp(attachment.redirectUrl), "g"), - location - ); - } catch (err) { - Logger.error( - `Failed to add attachment to archive: ${attachment.id}`, - err - ); - } + content = content.replace( + new RegExp(escapeRegExp(attachment.redirectUrl), "g"), + location + ); }) );