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

View file

@ -20,7 +20,7 @@ class DecodeMimeEncodedWords extends Operation {
this.name = "Decode Mime Encoded Words"; this.name = "Decode Mime Encoded Words";
this.module = "Default"; this.module = "Default";
this.description = ["Parser an IMF formatted messages following RFC5322.", 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"); ].join("\n");
this.infoURL = "https://tools.ietf.org/html/rfc2047"; this.infoURL = "https://tools.ietf.org/html/rfc2047";
this.inputType = "string"; this.inputType = "string";

View file

@ -9,7 +9,7 @@ import Mime from "../lib/Mime";
import Utils from "../Utils"; import Utils from "../Utils";
/** /**
* * Operation for parsing IMF messages into file list.
*/ */
class ParseIMF extends Operation { class ParseIMF extends Operation {
@ -18,11 +18,12 @@ class ParseIMF extends Operation {
*/ */
constructor() { constructor() {
super(); super();
this.name = "Parse Internet Message Format"; this.name = "Parse Internet Message Format";
this.module = "Default"; this.module = "Default";
this.description = ["Parse an IMF formatted messages following RFC5322.", this.description = ["Parse an IMF formatted messages following RFC5322.",
"<br><br>", "<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"); ].join("\n");
this.infoURL = "https://tools.ietf.org/html/rfc5322"; this.infoURL = "https://tools.ietf.org/html/rfc5322";
this.inputType = "string"; this.inputType = "string";
@ -30,7 +31,7 @@ class ParseIMF extends Operation {
this.presentType = "html"; this.presentType = "html";
this.args = [ this.args = [
{ {
"name": "Decode Encoded-Words", "name": "Decode Mime Encoded Words",
"type": "boolean", "type": "boolean",
"value": false "value": false
} }
@ -38,14 +39,11 @@ class ParseIMF extends Operation {
} }
/** /**
* * @param {string}
* * @param {Object[]}
* * @returns {File[]}
*
*
*/ */
run(input, args) { run(input, args) {
//let mimeObj = new Mime(input);
return new Mime(input).decodeMime(args[0]); return new Mime(input).decodeMime(args[0]);
} }