From 3f23809c791d715b3195f42ed13698aeca06cabe Mon Sep 17 00:00:00 2001 From: Vitaliy Filippov Date: Wed, 5 Oct 2016 01:57:10 +0300 Subject: [PATCH] extract attachment info from bodystructure --- Syncer.js | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/Syncer.js b/Syncer.js index 3437aa2..a89f35c 100644 --- a/Syncer.js +++ b/Syncer.js @@ -222,7 +222,8 @@ Syncer.prototype.syncBox = function*(srv, accountId, boxName, boxKind, doFull) missing.push((maxUid ? maxUid+1 : 1)+':*'); yield* this.imap.runFetch(srv, missing, { size: true, - bodies: 'HEADER' + bodies: 'HEADER', + struct: true, }, (messages, state) => this.saveMessages(messages, boxRow.id, state)); yield this.pg.update('folders', { @@ -383,6 +384,25 @@ Syncer.prototype.parseMsg = function*(msg) return obj; } +Syncer.prototype.extractAttachments = function(struct, attachments) +{ + attachments = attachments || []; + for (var i = 0; i < struct.length; i++) + { + if (struct[i] instanceof Array) + this.extractAttachments(struct[i], attachments); + else if (struct[i].disposition && struct[i].disposition.type == 'attachment') + { + attachments.push([ + struct[i].disposition.params && struct[i].disposition.params.filename || struct[i].description || '', + struct[i].type+'/'+struct[i].subtype, + struct[i].size + ]); + } + } + return attachments; +} + Syncer.prototype.addMessage = function*(boxId, msgrow, attrs) { var self = this; @@ -403,11 +423,6 @@ Syncer.prototype.addMessage = function*(boxId, msgrow, attrs) if (!header.date) header.date = new Date(attrs.date); - if (!header.from) - { - console.log(msgrow.headers); - console.log(header); - } delete msgrow.headers; msgrow.folder_id = boxId; msgrow.subject = header.subject || ''; @@ -417,6 +432,7 @@ Syncer.prototype.addMessage = function*(boxId, msgrow, attrs) cc: (header.cc||[]).map((a) => [ a.name, a.address ]), bcc: (header.bcc||[]).map((a) => [ a.name, a.address ]), replyto: (header.replyTo||[]).map((a) => [ a.name, a.address ]), + attachments: this.extractAttachments(attrs.struct), }); msgrow.messageid = header.messageId || ''; msgrow.inreplyto = header.inReplyTo && header.inReplyTo[0] || '';