From 1ba4a6eaf2ef1ed7a3dd305b662881dde2edd8ca Mon Sep 17 00:00:00 2001 From: Halla Moore Date: Mon, 9 Jan 2017 10:44:31 -0800 Subject: [PATCH] [local-sync] Add support for inline-images Summary: Extract files for inline attachments and store their content id Fixes T7414 Test Plan: tested locally Reviewers: evan, spang Reviewed By: spang Maniphest Tasks: T7414 Differential Revision: https://phab.nylas.com/D3609 --- .../src/message-processor/extract-files.js | 15 +++++++++++++-- packages/local-sync/src/models/file.js | 2 ++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/packages/local-sync/src/message-processor/extract-files.js b/packages/local-sync/src/message-processor/extract-files.js index de939cdaf..cb302c9c1 100644 --- a/packages/local-sync/src/message-processor/extract-files.js +++ b/packages/local-sync/src/message-processor/extract-files.js @@ -7,12 +7,22 @@ function collectFilesFromStruct({db, message, struct, fileIds = new Set()}) { collected = collected.concat(collectFilesFromStruct({db, message, struct: part, fileIds})); } else { const disposition = part.disposition || {} - const isAttachment = /attachment/gi.test(disposition.type); + const filename = (disposition.params || {}).filename; + + // Note that the contentId is stored in part.id, while the MIME part id + // is stored in part.partID + const match = /^<(.*)>$/.exec(part.id) // extract id from + const contentId = match ? match[1] : part.id; + + // Check if the part is an attachment. If it's inline, we also need + // to ensure that there is a filename and contentId because some clients + // use "inline" for text in the body. + const isAttachment = /(attachment)/gi.test(disposition.type) || + (/(inline)/gi.test(disposition.type) && filename && contentId); if (!isAttachment) continue const partId = part.partID - const filename = (disposition.params || {}).filename; const fileId = `${message.id}-${partId}-${part.size}` if (!fileIds.has(fileId)) { collected.push(File.build({ @@ -24,6 +34,7 @@ function collectFilesFromStruct({db, message, struct, fileIds = new Set()}) { messageId: message.id, accountId: message.accountId, contentType: `${part.type}/${part.subtype}`, + contentId, })); fileIds.add(fileId) } diff --git a/packages/local-sync/src/models/file.js b/packages/local-sync/src/models/file.js index 3eec65872..4cc63e38a 100644 --- a/packages/local-sync/src/models/file.js +++ b/packages/local-sync/src/models/file.js @@ -12,6 +12,7 @@ module.exports = (sequelize, Sequelize) => { messageId: { type: Sequelize.STRING, allowNull: false }, accountId: { type: Sequelize.STRING, allowNull: false }, contentType: Sequelize.STRING(500), + contentId: Sequelize.STRING(500), }, { indexes: [ {fields: ['messageId']}, @@ -54,6 +55,7 @@ module.exports = (sequelize, Sequelize) => { message_id: this.messageId, account_id: this.accountId, content_type: this.contentType, + content_id: this.contentId, }; }, },