[local-sync] Correctly close imap connections when downloading files

Summary: Addresses T7708

Test Plan: manual

Reviewers: evan, khamidou, mark, halla

Reviewed By: khamidou, mark, halla

Subscribers: halla

Differential Revision: https://phab.nylas.com/D3795
This commit is contained in:
Juan Tejada 2017-01-26 15:06:49 -08:00
parent ded002924a
commit 668be7463d
2 changed files with 11 additions and 5 deletions

View file

@ -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 || (() => {}));
})
}

View file

@ -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}`)