This repository has been archived by the owner on Jul 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
206 lines (163 loc) · 4.5 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
"use strict";
const express = require("express"),
app = express();
const client = require("./replitdb-client");
const helmet = require("helmet");
const PORT = process.env.PORT || 5050;
// client.empty().then(() => console.log("emptied"));
app.set('views', __dirname + '/public/views');
app.set('view engine', 'ejs');
app.use(helmet());
app.use(express.urlencoded({ extended: true }));
let urls;
console.log("Getting urls from db");
client.getAll().then((keyList) => {
urls = new Map(Object.entries(keyList));
console.log(urls);
}).catch((err) => {
console.log("Error getting keys from db\n" + err);
process.exit(1);
});
app.get("/", (req, res) => {
res.sendFile(__dirname + "/public/static/index.html");
});
app.get("/:id", (req, res) => {
if(urls.get(req.params.id) === undefined) {
res.sendFile(__dirname + "/public/static/404.html");
return;
} else {
res.redirect(urls.get(req.params.id));
return;
}
});
app.post("/addUrl", (req, res) => {
const urlSub = req.body.urlToAdd;
const urlId = req.body.urlId;
console.log(`urlId: ${urlId}`);
const alreadythere = contains(urlSub);
if(urls.has(urlId)) {
res.render("error", {error: "Id already taken"});
return;
} else if(alreadythere) {
res.render("error", {error: "Url already taken. ID for " + urlSub + " is " + alreadythere});
return;
} else if (urlSub.indexOf('<') > -1 || urlSub.indexOf('>') > -1) {
res.render("error", {error: "Error: Illegal character(s) < or > is found in url."});
return;
} else if(urlId.match(/^[a-zA-Z0-9]*$/) === null) {
res.render("error", {error: "Error: Illegal character(s) found in requested id."});
return;
}
const valid = isValidURL(urlSub);
if(valid) {
console.log("is valid")
let shortUrl = "";
let val = contains(urlSub);
if(val) {
console.log("Already available");
shortUrl = val;
} else {
console.log("Need to create");
if (!urlId.replace(/\s/g, '').length) {
console.log(urlSub + " empty " + urlId);
shortUrl = addAndUpdate(urlSub);
console.log("Add and update is run")
} else {
console.log(urlSub + " id is given " + urlId);
shortUrl = addAndUpdateWithId(urlSub, urlId);
console.log("Add and update is run")
}
}
res.render("url", {url: urlSub, shortened: shortUrl})
} else {
res.render("error", {error: "Error shortening url"});
}
});
app.get('/styles/:stylesheet', (req, res) => {
const file = req.params.stylesheet;
const options = {};
res.sendFile(__dirname + "/public/styles/" + file, options, (err) => {
if(err) {
res.sendFile(__dirname + "/public/static/404.html");
}
});
});
app.get('/fonts/:font', (req, res) => {
const file = req.params.font;
const options = {};
res.sendFile(__dirname + "/public/fonts/" + file, options, (err) => {
if(err) {
res.sendFile(__dirname + "/public/static/404.html");
}
});
});
app.get("*", (req, res) => {
res.sendFile(__dirname + "/public/static/404.html");
});
app.listen(PORT, () => {
console.log("App listening on port " + PORT);
});
/* function isValidURL(string) {
const res = string.match(/(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/g);
if (string.slice(0, 6) != "https:") {
return false;
}
console.log(string);
return (res !== null)
}; */
const isValidURL = potentially_valid_url => {
try {
new URL(potentially_valid_url);
return true;
} catch {
return false;
}
}
function addAndUpdate(theUrl) {
const rand = makeid();
console.log(rand + " addAndUpdate " + theUrl);
while(!loop(rand)) {
rand = makeid();
console.log(rand + " " + theUrl);
}
console.log(rand + " " + theUrl);
client.set(rand, theUrl).then(() => {
urls.set(rand, theUrl);
//console.clear();
console.log(urls);
});
return rand;
}
function addAndUpdateWithId(theUrl, theId) {
client.set(theId, theUrl).then(() => {
urls.set(theId, theUrl);
//console.clear();
console.log(urls);
});
return theId;
}
function loop(id) {
for(const i in urls) {
if(i == id) {
return false;
}
}
return true;
}
function makeid() {
let result = '';
let characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let charactersLength = characters.length;
for (let i = 0; i < 10; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
function contains(site) {
for (let [key, value] of urls) {
if(value == site) {
return key;
}
}
return "";
}