diff --git a/packages/isomorphic-core/src/imap-box.js b/packages/isomorphic-core/src/imap-box.js index a03069ba5..b839df933 100644 --- a/packages/isomorphic-core/src/imap-box.js +++ b/packages/isomorphic-core/src/imap-box.js @@ -104,21 +104,22 @@ class IMAPBox { }) } - fetchMessageStream(uid, options) { + fetchMessageStream(uid, {fetchOptions, onFetchComplete} = {}) { if (!uid) { throw new Error("IMAPConnection.fetchStream requires a message uid.") } - if (!options) { + if (!fetchOptions) { throw new Error("IMAPConnection.fetchStream requires an options object.") } return this._conn._createConnectionPromise((resolve, reject) => { - const f = this._conn._imap.fetch(uid, options); + const f = this._conn._imap.fetch(uid, fetchOptions); f.on('message', (imapMessage) => { imapMessage.on('body', (stream) => { resolve(stream) }) }) f.once('error', reject) + f.once('end', onFetchComplete || (() => {})); }) } diff --git a/packages/local-sync/src/models/file.js b/packages/local-sync/src/models/file.js index f146dc35c..57ab13d27 100644 --- a/packages/local-sync/src/models/file.js +++ b/packages/local-sync/src/models/file.js @@ -33,8 +33,13 @@ module.exports = (sequelize, Sequelize) => { const folder = await message.getFolder() const imapBox = await connection.openBox(folder.name) const stream = await imapBox.fetchMessageStream(message.folderImapUID, { - bodies: this.partId ? [this.partId] : [], - struct: true, + fetchOptions: { + bodies: this.partId ? [this.partId] : [], + struct: true, + }, + onFetchComplete() { + connection.end() + }, }) if (!stream) { throw new Error(`Unable to fetch binary data for File ${this.id}`)