-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.js
132 lines (117 loc) · 3.35 KB
/
app.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
var express = require("express"),
fs = require("fs"),
path = require("path"),
morgan = require("morgan"),
rle_parser = require("./assets/js/app/rle-parser");
var PATTERNS_DIR = path.join(__dirname, "patterns");
var patternsDatabase = [];
var app = express();
// logger
app.use(morgan("dev"));
// static files
app.use("/static", express.static(__dirname + "/assets"));
// routes
app.use("/favicon.ico", express.static(__dirname + "/assets/favicon.ico"));
app.get("/rle/list", function(req, res) {
var q = req.query.q;
var filtered_patterns = patternsDatabase.filter(function(pattern) {
var is_match = true;
if (typeof q !== "undefined" && q.length > 0) {
is_match = pattern.name.toLowerCase().search(q.toLowerCase()) >= 0;
}
return is_match;
});
res.json({
success: true,
patterns: filtered_patterns,
count: filtered_patterns.length
});
});
app.get("/rle/get/:filename", function(req, res) {
var fileName = req.params.filename;
var filePath = path.join(PATTERNS_DIR, fileName);
fs.readFile(filePath, function(err, data) {
if (err) {
res.json({
success: false,
error: err,
msg: "error reading file"
});
}
res.json({
success: true,
fileData: data.toString()
});
});
});
app.get("/", function(req, res) {
res.sendFile("views/index.html", { root: __dirname });
});
function loadRLEData(fileName, fileData) {
try {
var data = { fileName: fileName, comment: [], comments: "" };
var parsedData = RunLengthEncodedParser.parse(fileData);
parsedData.forEach(element => {
if (element.type == "name" || element.type == "author") {
data[element.type] = element.value.trim();
}
if (element.type == "comment") {
data[element.type].push(element.value.trim());
}
});
if (typeof data.name === "undefined") {
data.name = "No name: " + data.fileName.trim();
}
data["comment"].forEach((comment, idx) => {
if (comment.search("wiki") >= 0) {
data["wiki"] = comment.trim();
if (data["wiki"].search("http") == -1) {
data["wiki"] = "http://" + data["wiki"];
}
} else {
if (comment.search(".rle") >= 0) {
return;
} else {
data["comments"] += comment;
}
}
});
delete data["comment"];
return data;
} catch (err) {
console.warn("skipping pattern file", fileName, err.message);
return null;
}
}
// loading patterns database
function loadPatternsDatabase() {
fs.readdir(PATTERNS_DIR, function(err, files) {
if (err) {
res.json({
success: false,
error: err,
msg: "error reading patterns directory"
});
}
files.forEach(fileName => {
if (fileName.search(".rle") == -1) return;
var filePath = path.join(PATTERNS_DIR, fileName);
fs.readFile(filePath, function(err, data) {
if (err) {
console.log("Error reading file", filePath, err);
}
var pattern = loadRLEData(fileName, data.toString());
if (pattern) patternsDatabase.push(pattern);
});
});
});
}
loadPatternsDatabase();
const PORT = 3000;
const HOST = "0.0.0.0";
// server
var server = app.listen(PORT, HOST, function() {
var host = server.address().address;
var port = server.address().port;
console.log("Listening at %s:%s...", host, port);
});