-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebserver.js
169 lines (142 loc) · 4.53 KB
/
webserver.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
const fs = require('fs');
const http = require('http');
const path = require('path');
const Url = require('url');
const AnsiConverter = require('ansi-to-html');
const isInvalidPath = require('is-invalid-path');
const readLastLines = require('read-last-lines');
const conf = require('./conf');
const helper = require('./helper');
const mediaIdentifier = require('./media-identifier');
const Template = require('./template');
const reply = (res, rep, type = null) => {
if (type === null) {
type = typeof rep;
}
switch (type) {
case "html":
case "htm":
res.setHeader('Content-Type', 'text/html; charset=utf-8');
res.end(rep);
break;
case "object":
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(rep));
break;
case "string":
default:
res.setHeader('Content-Type', 'text/plain');
res.end(rep);
break;
}
}
const renderHome = async (res) => {
reply(res, Template.render("home"), "html");
}
const renderLog = async (res) => {
const logFile = helper.getLogPath();
let text = "";
if (fs.existsSync(logFile)) {
text = await readLastLines.read(logFile, 50);
}
reply(res, (new AnsiConverter()).toHtml(text), "html");
}
const renderFailedMatches = async (res) => {
let files = helper.getAllFiles(conf.failedMatchDirectory);
let html = "";
for(let k in files) {
html += Template.render("failedMatch", path.parse(files[k]));
}
reply(res, html, "html");
}
const deleteFile = (name) => {
helper.log("blue", "Order to delete " + name + " received");
const fullPath = path.join(conf.failedMatchDirectory, name);
try {
fs.unlinkSync(fullPath);
helper.log("green", name + " deleted");
} catch (e) {
helper.log("red", "Failed to delete " + fullPath + " received (" + e.message + ")");
}
}
const renameFile = (newName, oldName) => {
helper.log("blue", "Order to rename " + oldName + " with " + newName + " received");
newName = helper.removeInvalidPathCharacters(newName);
let files = helper.getAllFiles(conf.failedMatchDirectory);
for(let k in files) {
if (files[k].indexOf(oldName) !== -1) {
let oldPath = files[k];
let newPath = files[k].replace(oldName, newName);
if (isInvalidPath(newPath)) {
helper.log("red", "Error, new path is not valid (" + newPath + ")");
return false;
}
fs.renameSync(oldPath, newPath);
helper.log("green", "Renaming done");
return newPath;
}
}
helper.log("red", "Renaming failed, file not found");
return false;
}
const start = () => {
const server = http.createServer(async (req, res) => {
res.statusCode = 200;
let parsedUrl = Url.parse(req.url, true);
switch (parsedUrl.pathname.substr(1)) {
case "":
await renderHome(res);
break;
case "delete":
deleteFile(parsedUrl.query.name);
reply(res, "deleted");
break;
case "rename":
let newPath = renameFile(parsedUrl.query.newName, parsedUrl.query.oldName);
if (newPath != false) {
await mediaIdentifier.parseFiles(conf.failedMatchDirectory, [newPath]);
}
await mediaIdentifier.cleanFolder(conf.failedMatchDirectory);
reply(res, "working on that");
break;
case "log":
renderLog(res);
break;
case "failed-matches":
renderFailedMatches(res);
break;
case "retry-invalids":
case "retry":
reply(res, "working on that");
await mediaIdentifier.parseDirectory(conf.failedMatchDirectory);
break;
case "parse":
reply(res, "working on that");
await mediaIdentifier.parseDirectory(conf.directoryToParse);
break;
case "replace":
if (typeof parsedUrl.query.word === "undefined" || parsedUrl.query.word.length < 1) {
return reply(res, "invalid word param");
}
if (typeof parsedUrl.query.replacement === "undefined") {
return reply(res, "invalid word param");
}
if (typeof parsedUrl.query.replacement === "undefined" || parsedUrl.query.replacement.length < 1) {
parsedUrl.query.replacement = "";
}
reply(res, "working on that");
await mediaIdentifier.replaceWord(conf.failedMatchDirectory, parsedUrl.query.word, parsedUrl.query.replacement);
break;
}
});
server.on('clientError', function(err, socket) {
socket.destroy();
});
server.listen(conf.port, '127.0.0.1', () => {
console.log(`Media identifier started on http://localhost:${conf.port}`);
});
return server;
}
module.exports = {
start
}