-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
162 lines (143 loc) · 4.51 KB
/
Copy pathindex.js
File metadata and controls
162 lines (143 loc) · 4.51 KB
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
const express = require('express');
const app = express();
const fs = require('fs');
const crypto = require('crypto');
const { generateSalt, hash, compare } = require('./utils/passwords');
app.use(express.static('views'));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.get('/', (req, res) => {
res.send(__dirname + 'views/index.html');
});
app.post('/create', (req, res) => {
let countername = req.body.name;
let password = req.body.password;
let stroke = req.body.stroke == undefined ? '000' : String(req.body.stroke);
let fill = req.body.fill == undefined ? 'FFF' : String(req.body.fill);
let message = req.body.message == undefined ? 'Total Views' : String(req.body.message);
let database = JSON.parse(
fs.readFileSync('database.json', { encoding: 'utf8' })
);
let conditions = [countername, password, stroke, fill, message];
if (!database[countername]) {
if (!conditions.includes("")) {
database[countername] = { counts: 0, stroke: stroke, fill: fill, message: message};
fs.writeFileSync('database.json', JSON.stringify(database), {
encoding: 'utf8'
});
let salt = generateSalt(12);
let hashedpass = hash(password, salt);
let passwordDB = JSON.parse(
fs.readFileSync('passwords.json', { encoding: 'utf8' })
);
passwordDB[countername] = hashedpass;
fs.writeFileSync('passwords.json', JSON.stringify(passwordDB), {
encoding: 'utf8'
});
if (!req.body.stroke) console.log(`New Basic Counter ${countername}`);
if (req.body.stroke) console.log(`New Advanced Counter ${countername}`);
res.end(`Successful.\nVisit your counter at:\nhttps://myviewcounts.rayhanadev.repl.co/viewcount/${countername}.svg`);
} else {
res.end('Missing a field. Unsuccessful.');
}
} else {
res.end('Counter already exists. Unsuccessful.');
}
});
app.post('/delete', (req, res) => {
let countername = req.body.name;
let password = req.body.password;
let passwordDB = JSON.parse(
fs.readFileSync('passwords.json', { encoding: 'utf8' })
);
let database = JSON.parse(
fs.readFileSync('database.json', { encoding: 'utf8' })
);
let hashData = passwordDB[countername];
let result = compare(password, hashData);
if (result === true) {
delete passwordDB[countername];
delete database[countername];
fs.writeFileSync('passwords.json', JSON.stringify(passwordDB), {
encoding: 'utf8'
});
fs.writeFileSync('database.json', JSON.stringify(database), {
encoding: 'utf8'
});
console.log(`Deleted Counter ${countername}`);
res.end('Successful');
} else {
res.end('Incorrect password. Unsuccessful.');
}
});
app.get('/viewcount/:countername', (req, res) => {
let countername = String(req.params.countername).split('.')[0];
let database = JSON.parse(
fs.readFileSync('database.json', { encoding: 'utf8' })
);
if (!database[countername]) {
res.setHeader('Content-Type', 'image/svg+xml');
res.send(`
<svg xmlns="http://www.w3.org/2000/svg" height="50" width="510">
<style>
text {
font-family: Trebuchet MS, Helvetica;
font-size: 50px;
stroke-width: 1.5px;
fill: #FFFFFF;
stroke: #000000;
background-color: transparent;
}
</style>
<text x="5" y="48">No Counters Here o-O!</text>
</svg>`);
return;
}
let counts = database[countername].counts;
let newCounts = (counts += 1);
database[countername].counts = newCounts;
fs.writeFileSync('database.json', JSON.stringify(database), {
encoding: 'utf8'
});
res.setHeader('Content-Type', 'image/svg+xml');
res.status(200);
if (database[countername].fill && database[countername].stroke) {
res.send(`
<svg xmlns="http://www.w3.org/2000/svg" height="50" width="500">
<style>
text {
font-family: Trebuchet MS, Helvetica;
font-size: 50px;
stroke-width: 1.5px;
fill: #${database[countername].fill};
stroke: #${database[countername].stroke};
background-color: transparent;
}
</style>
<text x="5" y="48">
${database[countername].message}: ${counts}
</text>
</svg>`);
} else {
res.send(`
<svg xmlns="http://www.w3.org/2000/svg" height="50" width="500">
<style>
text {
font-family: Trebuchet MS, Helvetica;
font-size: 50px;
stroke-width: 1.5px;
fill: #FFFFFF;
stroke: #000000;
background-color: transparent;
}
</style>
<text x="5" y="48">
Total Views: ${counts}
</text>
</svg>`);
}
return;
});
app.listen(3000, () => {
console.log('Listening on Port: 3000');
});