Skip to content

Commit

Permalink
Correctly handle messages that send base64-encoded parts in a slightl…
Browse files Browse the repository at this point in the history
…y different format
  • Loading branch information
dotnich-io committed May 1, 2024
1 parent 62c85dd commit c6e4501
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
14 changes: 11 additions & 3 deletions src/GmailAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,18 @@ async function saveMail(settings: ObsGMailSettings, id: string) {
// Fetch the last mail in the threads
const payload = res.data.messages.pop().payload
const dst:mail_obj = {assets: Array<any>(), mhtml:"", mtxt:""}
flatten_parts(dst, payload.parts)
const parts = payload.parts ? payload.parts : [payload]
flatten_parts(dst, parts)
if(dst.mhtml=="" && dst.mtxt==""){
dst.mhtml = dst.assets.pop().body.data;
dst.mtxt = dst.mhtml;
let bodyText = ""
const htmlAsset = dst.assets.find((asset)=> asset.mimeType == "text/html" || asset.mimeType == "text/plain");
if(htmlAsset) {
bodyText = htmlAsset.body ? htmlAsset.body.data : htmlAsset.data
} else {
console.warn("no body found")
}
dst.mhtml = bodyText
dst.mtxt = bodyText
}
console.log("DST:")
console.log(payload)
Expand Down
23 changes: 18 additions & 5 deletions src/mailProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,27 @@ async function getMailTitle(title_candidates) {

async function getBody(bodys:any, format:any){
let body = ''
const normalizedBodys = bodys.map((b:any) => {
if(typeof b == "object"){
if(b.data)
return b.data
else {
return ""
}
} else if (typeof b == "string") {
return b
} else {
throw new Error("Unknown body type")
}
})
if (format == "htmlmd")
body = await processHTMLBody(bodys[1].data || "")
body = await processHTMLBody(normalizedBodys[1])
else if (format == "text")
body = await processPTBody(bodys[0].data || "")
body = await processPTBody(normalizedBodys[0])
else
body = await processRawBody(bodys[1].data || "")
body = await processRawBody(normalizedBodys[1])
if (body=="")
body = await processRawBody(bodys[1].data || bodys[0].data || "")
body = await processRawBody(normalizedBodys[1] || normalizedBodys[0] || "")
return body
}

Expand Down Expand Up @@ -151,4 +164,4 @@ export async function incr_filename(title: string, folder: string) {
idx++
}
return `${folder}/${tmp}.${ext}`
}
}

0 comments on commit c6e4501

Please sign in to comment.