-
Notifications
You must be signed in to change notification settings - Fork 0
/
metapost.js
100 lines (92 loc) · 2.79 KB
/
metapost.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
98
99
100
'use strict';
const fs = require('fs');
const path = require('path');
const vm = require('vm');
const http = require('http');
const walkitout = require('walkitout');
const DATA_PATH = path.join('.', 'data');
const KEY_PATH = path.join(DATA_PATH, 'keys.txt');
const VALUES_PATH = path.join(DATA_PATH, 'values');
vm.runInThisContext(fs.readFileSync(KEY_PATH, 'utf8'));
const POST_HOSTNAME = 'localhost';
const POST_PORT = 3000;
const POST_PATHNAME = '/';
main();
function main() {
walkitout(VALUES_PATH, (err, filePath, done) => {
if (err) return done();
if (!/\.meta$/.test(filePath)) return done();
postData(filePath, done);
}, () => console.log('Meta posted!'));
}
function getFilePart(filePath, name) {
return new Promise((res, rej) => {
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) rej(err);
let filename = path.basename(filePath);
let extname = path.extname(filename).substr(1);
res([
`Content-Disposition: form-data; name="${name}"; filename="${filename}"`,
`Content-Type: text/${extname}`,
'',
data
].join("\r\n"));
});
});
}
function getFieldPart(value, name) {
return new Promise((res, rej) => {
res([
`Content-Disposition: form-data; name="${name}"`,
'',
value
].join("\r\n"));
});
}
function getAllParts(metaPath, data) {
let lines = data.split("\n").map((value) => value.trim());
return Promise.all(KEYS
.slice(0)
.map((keyName, index) => {
switch (keyName) {
case 'FILE': {
let filename = lines[index];
let filePath = path.join(path.dirname(metaPath), filename);
return getFilePart(filePath, keyName);
}
default: {
let value = lines[index];
return getFieldPart(value, keyName);
}
}
}));
}
function getPostReqCfg(postHostname, postPort, postPathname, boundary) {
return {
hostname: postHostname,
port: postPort,
method: 'POST',
path: postPathname,
headers: {
'Content-Type': `multipart/form-data; boundary=${boundary}`
}
};
}
function postData(metaPath, done) {
let boundary = 'Metapost' + new String(Math.random() * 123456789).replace(/\./, '');
fs.readFile(metaPath, 'utf8', (err, data) =>
getAllParts(metaPath, data)
.then((parts) => {
let body = [
`--${boundary}\r\n`,
parts.join(`\r\n--${boundary}\r\n`),
`\r\n--${boundary}--`
].join('');
let reqCfg = getPostReqCfg(POST_HOSTNAME, POST_PORT, POST_PATHNAME, boundary);
http.request(reqCfg, (res) => {
console.log(`Post meta: ${metaPath} -- Status: ${res.statusCode}`);
res.on('error', (err) => console.log(`Error with ${metaPath}: ${err.message}`));
done();
}).end(body);
}));
}