mirror of
https://github.com/omnivore-app/omnivore.git
synced 2026-03-11 08:54:26 +00:00
Fixing issue with mail content from SNS that is base64 encoded (#4576)
This commit is contained in:
parent
b5008c4382
commit
0c378da0d2
2 changed files with 36 additions and 2 deletions
|
|
@ -7,6 +7,7 @@ import { SnsMessage } from './types/SNS'
|
|||
import { simpleParser } from 'mailparser'
|
||||
import axios from 'axios'
|
||||
import { convertToMailObject } from './lib/emailApi'
|
||||
import { decodeBase64 } from './lib/base64'
|
||||
|
||||
console.log('Starting worker...')
|
||||
|
||||
|
|
@ -91,13 +92,27 @@ app.post('/sns', async (req, res) => {
|
|||
const message = JSON.parse(snsMessage.Message) as {
|
||||
notificationType: string
|
||||
content: string
|
||||
receipt?: {
|
||||
action?: {
|
||||
encoding?: string
|
||||
}
|
||||
}
|
||||
}
|
||||
if (message.notificationType != 'Received') {
|
||||
console.log('Not an email, failing...')
|
||||
res.status(400).send()
|
||||
return
|
||||
}
|
||||
|
||||
const mailContent = await simpleParser(message.content)
|
||||
// Check if content is base64 encoded and decode if necessary
|
||||
let emailContent = message.content
|
||||
if (message.receipt?.action?.encoding === "BASE64") {
|
||||
console.log("Detected BASE64 encoded content, decoding...")
|
||||
emailContent = decodeBase64(message.content)
|
||||
console.log("Processed BASE64 content")
|
||||
}
|
||||
|
||||
const mailContent = await simpleParser(emailContent)
|
||||
const mail = convertToMailObject(mailContent)
|
||||
console.log(mail)
|
||||
await (
|
||||
|
|
@ -108,7 +123,7 @@ app.post('/sns', async (req, res) => {
|
|||
delay: 500,
|
||||
})
|
||||
res.sendStatus(200)
|
||||
|
||||
|
||||
res.status(200).send()
|
||||
return
|
||||
}
|
||||
|
|
|
|||
19
packages/local-mail-watcher/src/lib/base64.js
Normal file
19
packages/local-mail-watcher/src/lib/base64.js
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
/**
|
||||
* Utility functions for handling base64 encoding/decoding
|
||||
*/
|
||||
|
||||
/**
|
||||
* Decodes a base64 string to UTF-8 text
|
||||
* Uses Node.js Buffer but with type safety
|
||||
*/
|
||||
export function decodeBase64(base64String: string): string {
|
||||
try {
|
||||
// Use a type-safe approach without relying on global
|
||||
// @ts-ignore - Ignore TypeScript error for Buffer
|
||||
return Buffer.from(base64String, 'base64').toString('utf-8');
|
||||
} catch (error) {
|
||||
console.error('Error decoding base64 string:', error);
|
||||
return base64String; // Return original string if decoding fails
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in a new issue