-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconverters.js
97 lines (83 loc) · 3.07 KB
/
converters.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
function hd4Groups (from, fromName, to, toName, listID) {
const hd = [["From", [fromName, from]],["To", [toName, to]],["List-ID", [listID]]];
return JSON.stringify(hd);
}
function hd4Direct (from, fromName, to) {
const hd = [["From", [fromName, from]],["To", ["",to]]];
return JSON.stringify(hd);
}
function SenderOrRecipientJSON (rcpID, rcpName) {
const retval = [[rcpName, rcpID]];
return JSON.stringify(retval);
}
function bodyBlobJSON (blobID, size, csize, sha512) {
const retval = [[2, [[blobID, String(size), String(csize), String(sha512)]]]];
return JSON.stringify(retval);
}
function bodyBlobB64JSON (bodyBase64) {
const retval = [[2, [String(bodyBase64)]],];
return JSON.stringify(retval);
}
function filesJSON (fileName, mimeType, blobID, size, csize, sha512, contentID) {
let retval;
const ext = '.' + fileType(mimeType);
if (fileName === null || fileName === undefined || fileName === "") {
retval = [["Unnamed Attachment"+ext, mimeType, [blobID, String(size), String(csize), String(sha512)], String(contentID),""]];
} else {
retval = [[String(fileName), mimeType, [blobID, String(size), String(csize), String(sha512)], String(contentID),""]];
}
return JSON.stringify(retval);
}
function filesB64JSON (fileName, mimeType, data) {
let retval;
const ext = '.' + fileType(mimeType);
if (fileName === null || fileName === undefined || fileName === "") {
retval = [["Unnamed Attachment" + ext, String(mimeType), [String(data)], "", ""]];
}
else {
retval = [[String(fileName), String(mimeType), [String(data)], "", ""]];
}
return JSON.stringify(retval);
}
function convertToBase64(text) {
const retval = Buffer.from(text, 'utf8');
return retval.toString('base64');
}
function insertCharacterAtIndex(text) {
text = text.substring(0, 9) + '/' + text.substring(9);
text = text.substring(0, 6) + '/' + text.substring(6);
text = text.substring(0, 3) + '/' + text.substring(3);
return text;
}
function createRegex () {
const characters = 'ABCDEFGHIJKLMNOPQRSTUV0123456789';
let randomString = '';
for (let i = 0; i < 16; i++) {
const randomIndex = Math.floor(Math.random() * characters.length);
randomString += characters.charAt(randomIndex);
}
return randomString;
}
function fileType (str) {
let retval;
if (str === 'text/plain') {
retval = 'txt'
return retval;
}
if (str.includes(';')) {
retval = str.slice(str.indexOf('/')+1, str.indexOf(';'));
} else {
retval = str.slice(str.indexOf('/')+1);
}
return retval;
}
module.exports.hd4Groups = hd4Groups;
module.exports.hd4Direct = hd4Direct;
module.exports.SenderOrRecipientJSON = SenderOrRecipientJSON;
module.exports.bodyBlobJSON = bodyBlobJSON;
module.exports.convertToBase64 = convertToBase64;
module.exports.insertCharacterAtIndex = insertCharacterAtIndex;
module.exports.createRegex = createRegex;
module.exports.filesJSON = filesJSON;
module.exports.filesB64JSON = filesB64JSON;
module.exports.bodyBlobB64JSON = bodyBlobB64JSON;