munir131/attachment-downloader

not all attachments are downloaded

Opened this issue ยท 3 comments

hello, firstly I'd like to say nice project!

I tried using it and it found the label I was looking for, it found all the emails under the label but it didn't find any attachments

I traced the problem to index.js, line 189 in the function pluckAllAttachments

      if (!p.body || !p.body.attachmentId) {
        return undefined;
      }

It seems not all messages that have attachments, have an attachmentId. Sometimes they are found in the same object but in the data key. See gmail api https://developers.google.com/gmail/api/reference/rest/v1/users.messages.attachments

it seems the messages that are causing the problem have nested parts which contain the relevant attachmentId

image

The messages in question were forwarded with an iPad FWIW

EDIT: removed something I was wrong about, added what the actual problem was ๐Ÿ˜…

changing it to the below snippet worked for my case ๐Ÿ˜

Sorry I don't have time to do it properly as part of a pull request at the moment. I'm not sure if my use of lodash flattenDeep would cause problems in other scenarios.

I suspect there's only nested parts if the mimeType is 'multipart/mixed'

function pluckAllAttachments(mails) {
  return _.compact(_.flattenDeep(_.map(mails, (m) => {
    if (!m.data || !m.data.payload || !m.data.payload.parts) {
      return undefined;
    }
    return pluckParts(m.data.payload.parts, m.data.id);

  })));
}

function pluckParts(parts, dataId ) {
  const pluckedParts = _.map(parts, (p) => {
      if (p.parts !== undefined) {
        return pluckParts(p.parts, dataId);
      }
      if (!p.body || !p.body.attachmentId) {
        return undefined;
      }
      const attachment = {
        mailId: dataId,
        name: p.filename,
        id: p.body.attachmentId
      };
      return attachment;
    })
    return pluckedParts;
}

@HanaanY Thanks for sharing the issue and solution. I haven't got time to look into that. But, I will look into that and fix within 2 weeks. Thanks again.

@lpirkwieser fixed that issue. @HanaanY Please confirm for your use case.