fixing encoding/parsing issues

This commit is contained in:
bwhitn 2018-12-11 01:22:23 -05:00
parent a980462d71
commit 94b2638365
3 changed files with 21 additions and 18 deletions

View file

@ -6,7 +6,6 @@
import OperationError from "../errors/OperationError";
import cptable from "../vendor/js-codepage/cptable.js";
import {fromBase64} from "../lib/Base64";
import {decodeQuotedPrintable} from "../lib/QuotedPrintable";
import {MIME_FORMAT} from "../lib/ChrEnc";
import Utils from "../Utils";
@ -58,7 +57,7 @@ class Mime {
}
name = name.concat(Mime.getFileExt(fileObj.type));
}
retval.push(new File([fileObj.data], name, {type: fileObj.type}));
retval.push(new File([Uint8Array.from(fileObj.data)], name, {type: fileObj.type}));
});
return retval;
}
@ -91,7 +90,7 @@ class Mime {
const newLineLength = this.rn ? 2 : 1;
let contType = "text/plain",
fileName = null,
charEnc = "us-ascii",
charEnc = null,
contDispoObj = null,
contTypeObj = null;
if (parentObj.header.hasOwnProperty("content-type")) {
@ -109,6 +108,10 @@ class Mime {
}
if (contTypeObj.hasOwnProperty("charset")) {
charEnc = contTypeObj.charset;
} else {
if (contType.startsWith("text/")) {
charEnc = "us-ascii";
}
}
if (fileName == null && contTypeObj.hasOwnProperty("name")) {
fileName = contTypeObj.name;
@ -196,10 +199,11 @@ class Mime {
static _decodeMimeData(input, charEnc, contEnc) {
switch (contEnc) {
case "base64":
input = fromBase64(input);
input = Utils.convertToByteArray(input, "base64");
//input = fromBase64(input);
break;
case "quoted-printable":
input = Utils.byteArrayToUtf8(decodeQuotedPrintable(input));
input = decodeQuotedPrintable(input);
}
if (charEnc && MIME_FORMAT.hasOwnProperty(charEnc.toLowerCase())) {
input = cptable.utils.decode(MIME_FORMAT[charEnc.toLowerCase()], input);
@ -253,15 +257,16 @@ class Mime {
*/
_splitMultipart(input, boundary, newLineLength) {
const output = [];
const boundaryStr = "--".concat(boundary, this.rn ? "\r\n" : "\n");
const last = input.indexOf("--".concat(boundary, "--")) - newLineLength;
const newline = this.rn ? "\r\n" : "\n";
const boundaryStr = newline.concat("--", boundary);
const last = input.indexOf(newline.concat("--", boundary, "--"));
for (;;) {
let start = input.indexOf(boundaryStr, start);
if (start < 0) {
break;
}
start += boundaryStr.length;
const end = input.indexOf(boundaryStr, start) - newLineLength;
const end = input.indexOf(boundaryStr, start);
if (end <= start) {
break;
}

View file

@ -20,7 +20,7 @@ class DecodeMimeEncodedWords extends Operation {
this.name = "Decode Mime Encoded Words";
this.module = "Default";
this.description = ["Parser an IMF formatted messages following RFC5322.",
"<br><br>", "Parses an IMF formated message. These often have the file extention &quot;.eml&quote; and contain the email headers and body. The output will be a file list of the headers and mime parts.",
"<br><br>", "Decodes Mime encoded words that are found in IMF messages.",
].join("\n");
this.infoURL = "https://tools.ietf.org/html/rfc2047";
this.inputType = "string";

View file

@ -9,7 +9,7 @@ import Mime from "../lib/Mime";
import Utils from "../Utils";
/**
*
* Operation for parsing IMF messages into file list.
*/
class ParseIMF extends Operation {
@ -18,11 +18,12 @@ class ParseIMF extends Operation {
*/
constructor() {
super();
this.name = "Parse Internet Message Format";
this.module = "Default";
this.description = ["Parse an IMF formatted messages following RFC5322.",
"<br><br>",
"Parses an IMF formated message. These often have the file extention &quot;.eml&quote; and contain the email headers and body. The output will be a file list of the root header and decoded mime parts.",
"Parses an IMF formated message like those sent in SMTP. These often have the file extention &quot;.eml&quote; and contain the email headers and body. The output will be a file list of the root header and decoded mime parts."
].join("\n");
this.infoURL = "https://tools.ietf.org/html/rfc5322";
this.inputType = "string";
@ -30,7 +31,7 @@ class ParseIMF extends Operation {
this.presentType = "html";
this.args = [
{
"name": "Decode Encoded-Words",
"name": "Decode Mime Encoded Words",
"type": "boolean",
"value": false
}
@ -38,14 +39,11 @@ class ParseIMF extends Operation {
}
/**
*
*
*
*
*
* @param {string}
* @param {Object[]}
* @returns {File[]}
*/
run(input, args) {
//let mimeObj = new Mime(input);
return new Mime(input).decodeMime(args[0]);
}