use TextDecoder on Mime parts?

This commit is contained in:
the-djmaze 2022-08-30 22:27:07 +02:00
parent 5340ca84e5
commit 03543d5cfc

View file

@ -9,11 +9,11 @@ export function ParseMime(text)
class MimePart class MimePart
{ {
header(name) { header(name) {
return this.headers && this.headers[name]; return this.headers[name];
} }
headerValue(name) { headerValue(name) {
return (this.header(name) || {value:null}).value; return this.header(name)?.value;
} }
get raw() { get raw() {
@ -26,12 +26,17 @@ export function ParseMime(text)
get body() { get body() {
let body = this.bodyRaw, let body = this.bodyRaw,
// charset = this.header('content-type')?.params.charset,
encoding = this.headerValue('content-transfer-encoding'); encoding = this.headerValue('content-transfer-encoding');
if ('quoted-printable' == encoding) { if ('quoted-printable' == encoding) {
body = body.replace(/=\r?\n/g, '').replace(QPDecodeIn, QPDecodeOut); body = body.replace(/=\r?\n/g, '').replace(QPDecodeIn, QPDecodeOut);
} else if ('base64' == encoding) { } else if ('base64' == encoding) {
body = atob(body.replace(/\r?\n/g, '')); body = atob(body.replace(/\r?\n/g, ''));
} }
/*
// https://developer.mozilla.org/en-US/docs/Web/API/Encoding_API/Encodings
return (new TextDecoder(charset)).decode((new TextEncoder()).encode(body));
*/
return body; return body;
} }
@ -73,7 +78,8 @@ export function ParseMime(text)
const ParsePart = (mimePart, start_pos = 0, id = '') => const ParsePart = (mimePart, start_pos = 0, id = '') =>
{ {
let part = new MimePart; let part = new MimePart,
headers = {};
if (id) { if (id) {
part.id = id; part.id = id;
part.start = start_pos; part.start = start_pos;
@ -84,7 +90,6 @@ export function ParseMime(text)
let head = mimePart.match(/^[\s\S]+?\r?\n\r?\n/); let head = mimePart.match(/^[\s\S]+?\r?\n\r?\n/);
if (head) { if (head) {
head = head[0]; head = head[0];
let headers = {};
head.replace(/\r?\n\s+/g, ' ').split(/\r?\n/).forEach(header => { head.replace(/\r?\n\s+/g, ' ').split(/\r?\n/).forEach(header => {
let match = header.match(/^([^:]+):\s*([^;]+)/), let match = header.match(/^([^:]+):\s*([^;]+)/),
params = {}; params = {};
@ -96,7 +101,6 @@ export function ParseMime(text)
params: params params: params
}; };
}); });
part.headers = headers;
// get body // get body
part.bodyStart = start_pos + head.length; part.bodyStart = start_pos + head.length;
@ -125,6 +129,8 @@ export function ParseMime(text)
} }
} }
part.headers = headers;
return part; return part;
}; };