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, }; }, },