Skip to content

Commit

Permalink
fix(gmail-api): Fixed attachment forwarding when using Gmail API
Browse files Browse the repository at this point in the history
  • Loading branch information
andris9 committed Dec 7, 2024
1 parent 2b4d5bb commit 6aef655
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 25 deletions.
12 changes: 8 additions & 4 deletions lib/email-client/base-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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');
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
24 changes: 14 additions & 10 deletions lib/email-client/gmail-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -941,7 +940,7 @@ class GmailClient extends BaseClient {
}
}

const content = {
const contentResponse = {
headers: {
'content-type': attachmentData.mimeType || 'application/octet-stream',
'content-disposition': 'attachment' + filenameParam
Expand All @@ -952,7 +951,7 @@ class GmailClient extends BaseClient {
data: attachmentData.content
};

return content;
return contentResponse;
}

async getMessage(emailId, options) {
Expand Down Expand Up @@ -1773,20 +1772,25 @@ 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();

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 = '"') {
Expand Down
26 changes: 15 additions & 11 deletions lib/email-client/outlook-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -1038,7 +1038,7 @@ class OutlookClient extends BaseClient {
}
}

const content = {
const contentResponse = {
headers: {
'content-type': attachmentData.mimeType || 'application/octet-stream',
'content-disposition': 'attachment' + filenameParam
Expand All @@ -1048,7 +1048,7 @@ class OutlookClient extends BaseClient {
data: attachmentData.content
};

return content;
return contentResponse;
}

async getMessage(emailId, options) {
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 6aef655

Please sign in to comment.