diff --git a/lib/email-client/base-client.js b/lib/email-client/base-client.js index 4d2986d7..8e84c13c 100644 --- a/lib/email-client/base-client.js +++ b/lib/email-client/base-client.js @@ -955,7 +955,8 @@ class BaseClient { } else { // fetch from IMAP content = await this.getAttachmentContent(attachment.id, { - chunkSize: Math.max(DOWNLOAD_CHUNK_SIZE, 2 * 1024 * 1024) + chunkSize: Math.max(DOWNLOAD_CHUNK_SIZE, 2 * 1024 * 1024), + contentOnly: true }); } if (!content) { @@ -980,7 +981,8 @@ class BaseClient { this.checkIMAPConnection(); let content = await this.getAttachmentContent(attachment.reference, { - chunkSize: Math.max(DOWNLOAD_CHUNK_SIZE, 2 * 1024 * 1024) + chunkSize: Math.max(DOWNLOAD_CHUNK_SIZE, 2 * 1024 * 1024), + contentOnly: true }); if (!content) { let error = new Error('Referenced attachment was not found'); @@ -1338,7 +1340,8 @@ class BaseClient { } else { // fetch from IMAP content = await this.getAttachmentContent(attachment.id, { - chunkSize: Math.max(DOWNLOAD_CHUNK_SIZE, 2 * 1024 * 1024) + chunkSize: Math.max(DOWNLOAD_CHUNK_SIZE, 2 * 1024 * 1024), + contentOnly: true }); } if (!content) { @@ -1363,7 +1366,8 @@ class BaseClient { this.checkIMAPConnection(); let content = await this.getAttachmentContent(attachment.reference, { - chunkSize: Math.max(DOWNLOAD_CHUNK_SIZE, 2 * 1024 * 1024) + chunkSize: Math.max(DOWNLOAD_CHUNK_SIZE, 2 * 1024 * 1024), + contentOnly: true }); if (!content) { diff --git a/lib/email-client/gmail-client.js b/lib/email-client/gmail-client.js index c9cbf11c..8f20f876 100644 --- a/lib/email-client/gmail-client.js +++ b/lib/email-client/gmail-client.js @@ -21,7 +21,6 @@ const { AUTH_ERROR_NOTIFY, AUTH_SUCCESS_NOTIFY } = require('../consts'); -const { threadId } = require('worker_threads'); const GMAIL_API_BASE = 'https://gmail.googleapis.com'; const LIST_BATCH_SIZE = 10; // how many listing requests to run at the same time @@ -941,7 +940,7 @@ class GmailClient extends BaseClient { } } - const content = { + const contentResponse = { headers: { 'content-type': attachmentData.mimeType || 'application/octet-stream', 'content-disposition': 'attachment' + filenameParam @@ -952,7 +951,7 @@ class GmailClient extends BaseClient { data: attachmentData.content }; - return content; + return contentResponse; } async getMessage(emailId, options) { @@ -1773,7 +1772,8 @@ class GmailClient extends BaseClient { return result; } - async getAttachmentContent(attachmentId) { + async getAttachmentContent(attachmentId, options) { + options = options || {}; const [emailId, contentType, disposition, filename, id] = msgpack.decode(Buffer.from(attachmentId, 'base64url')); await this.prepare(); @@ -1781,12 +1781,16 @@ class GmailClient extends BaseClient { const requestQuery = {}; const result = await this.request(`${GMAIL_API_BASE}/gmail/v1/users/me/messages/${emailId}/attachments/${id}`, 'get', requestQuery); - return { - content: result?.data ? Buffer.from(result?.data, 'base64url') : null, - contentType, - disposition, - filename - }; + const content = result?.data ? Buffer.from(result?.data, 'base64url') : null; + + return options.contentOnly + ? content + : { + content, + contentType, + disposition, + filename + }; } formatSearchTerm(term, quot = '"') { diff --git a/lib/email-client/outlook-client.js b/lib/email-client/outlook-client.js index 33bbc4f7..9b0c27c9 100644 --- a/lib/email-client/outlook-client.js +++ b/lib/email-client/outlook-client.js @@ -1038,7 +1038,7 @@ class OutlookClient extends BaseClient { } } - const content = { + const contentResponse = { headers: { 'content-type': attachmentData.mimeType || 'application/octet-stream', 'content-disposition': 'attachment' + filenameParam @@ -1048,7 +1048,7 @@ class OutlookClient extends BaseClient { data: attachmentData.content }; - return content; + return contentResponse; } async getMessage(emailId, options) { @@ -2230,15 +2230,19 @@ class OutlookClient extends BaseClient { } } - return { - content: attachmentResponse?.contentBytes - ? options.returnBase64 - ? attachmentResponse.contentBytes - : Buffer.from(attachmentResponse.contentBytes, 'base64') - : null, - contentType: attachmentResponse?.contentType, - filename: attachmentResponse?.name - }; + const content = attachmentResponse?.contentBytes + ? options.returnBase64 + ? attachmentResponse.contentBytes + : Buffer.from(attachmentResponse.contentBytes, 'base64') + : null; + + return options.contentOnly + ? content + : { + content, + contentType: attachmentResponse?.contentType, + filename: attachmentResponse?.name + }; } triggerSync() {