mirror of
https://github.com/the-djmaze/snappymail.git
synced 2026-07-13 03:27:39 +03:00
Improved mime parser
This commit is contained in:
parent
03543d5cfc
commit
d7965d1193
1 changed files with 26 additions and 12 deletions
|
|
@ -8,6 +8,20 @@ export function ParseMime(text)
|
||||||
{
|
{
|
||||||
class MimePart
|
class MimePart
|
||||||
{
|
{
|
||||||
|
/*
|
||||||
|
constructor() {
|
||||||
|
this.id = 0;
|
||||||
|
this.start = 0;
|
||||||
|
this.end = 0;
|
||||||
|
this.parts = [];
|
||||||
|
this.bodyStart = 0;
|
||||||
|
this.bodyEnd = 0;
|
||||||
|
this.boundary = '';
|
||||||
|
this.bodyText = '';
|
||||||
|
this.headers = {};
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
header(name) {
|
header(name) {
|
||||||
return this.headers[name];
|
return this.headers[name];
|
||||||
}
|
}
|
||||||
|
|
@ -34,8 +48,12 @@ export function ParseMime(text)
|
||||||
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
|
try {
|
||||||
return (new TextDecoder(charset)).decode((new TextEncoder()).encode(body));
|
// https://developer.mozilla.org/en-US/docs/Web/API/Encoding_API/Encodings
|
||||||
|
return new TextDecoder(charset).decode(Uint8Array.from(body, c => c.charCodeAt(0)));
|
||||||
|
// return new TextDecoder(charset).decode(new TextEncoder().encode(body));
|
||||||
|
} catch (e) {
|
||||||
|
}
|
||||||
*/
|
*/
|
||||||
return body;
|
return body;
|
||||||
}
|
}
|
||||||
|
|
@ -56,21 +74,17 @@ export function ParseMime(text)
|
||||||
|
|
||||||
forEach(fn) {
|
forEach(fn) {
|
||||||
fn(this);
|
fn(this);
|
||||||
if (this.parts) {
|
this.parts.forEach(part => part.forEach(fn));
|
||||||
this.parts.forEach(part => part.forEach(fn));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getByContentType(type) {
|
getByContentType(type) {
|
||||||
if (type == this.headerValue('content-type')) {
|
if (type == this.headerValue('content-type')) {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
if (this.parts) {
|
let i = 0, p = this.parts, part;
|
||||||
let i = 0, p = this.parts, part;
|
for (i; i < p.length; ++i) {
|
||||||
for (i; i < p.length; ++i) {
|
if ((part = p[i].getByContentType(type))) {
|
||||||
if ((part = p[i].getByContentType(type))) {
|
return part;
|
||||||
return part;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -85,6 +99,7 @@ export function ParseMime(text)
|
||||||
part.start = start_pos;
|
part.start = start_pos;
|
||||||
part.end = start_pos + mimePart.length;
|
part.end = start_pos + mimePart.length;
|
||||||
}
|
}
|
||||||
|
part.parts = [];
|
||||||
|
|
||||||
// get headers
|
// get headers
|
||||||
let head = mimePart.match(/^[\s\S]+?\r?\n\r?\n/);
|
let head = mimePart.match(/^[\s\S]+?\r?\n\r?\n/);
|
||||||
|
|
@ -110,7 +125,6 @@ export function ParseMime(text)
|
||||||
let boundary = headers['content-type'].params.boundary;
|
let boundary = headers['content-type'].params.boundary;
|
||||||
if (boundary) {
|
if (boundary) {
|
||||||
part.boundary = boundary;
|
part.boundary = boundary;
|
||||||
part.parts = [];
|
|
||||||
let regex = new RegExp('(?:^|\r?\n)--' + boundary + '(?:--)?(?:\r?\n|$)', 'g'),
|
let regex = new RegExp('(?:^|\r?\n)--' + boundary + '(?:--)?(?:\r?\n|$)', 'g'),
|
||||||
body = mimePart.slice(head.length),
|
body = mimePart.slice(head.length),
|
||||||
bodies = body.split(regex),
|
bodies = body.split(regex),
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue