-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
242 lines (222 loc) · 8.8 KB
/
index.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
const express = require('express')
const app = express();
const multer = require('multer');
const session = require('express-session');
const fs = require('fs');
const path = require('path');
const shortid = require('shortid');
const { QuickDB } = require("quick.db");
const db = new QuickDB();
const users = require('./assets/json/users.json');
require('ejs');
app.set('view engine', 'ejs');
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(session({
secret: 'hello',
resave: true,
saveUninitialized: true,
}));
app.use('/static', express.static('assets'));
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, './uploads');
},
e: (req, file, cb) => {
cb(null, file.originalname);
}
});
const upload = multer({
storage,
storageFilter: (req, file, callback) => {
const supportedFileTypes = [
'image/gif',
'image/jpeg',
'image/jpg',
'image/png',
'video/mp4',
];
if (!supportedFiletypes.includes(file.mimetype)) callback(null, false);
callback(null, true);
}
});
const validateCredentials = (username, password) => {
return ((username.toLowerCase() === users[0].username && password === users[0].password) || (username.toLowerCase() === users[1].username && password === users[1].password));
}
app.get('/', async (req, res) => {
const { user } = req.session;
if (!user) return res.redirect('/login');
if (validateCredentials(user.username, user.password)) {
const files = await db.all();
console.log(files);
const favorites = await db.get(`${user.username}.favorites`) || [];
res.render('index', { user, files, favorites: favorites.map(f => f.id), pfp: users.find(u => u.username === user.username).pfp });
} else {
res.json({ error: 'Invalid credentials' });
}
});
app.get("/editprofile", (req, res) => {
const { user } = req.session;
if (!user) return res.redirect('/login?returnUrl=editprofile');
if (validateCredentials(user.username, user.password)) {
res.render('editprofile', { user, pfp: users.find(u => u.username === user.username).pfp });
} else {
res.json({ error: 'Invalid credentials' });
}
})
app.post("/editprofile", (req, res) => {
const { user } = req.session;
console.log(user);
if (!user) return res.redirect('/login?returnUrl=editprofile');
const { password, pfp } = req.body;
if (password) {
users.find(u => u.username === user.username).password = password;
fs.writeFileSync('./assets/json/users.json', JSON.stringify(users, null, 2));
res.json({ success: 'Profile updated' });
};
if (pfp) {
users.find(u => u.username === user.username).pfp = pfp;
fs.writeFileSync('./assets/json/users.json', JSON.stringify(users, null, 2));
res.json({ success: 'Profile updated' });
}
})
app.get('/upload', (req, res) => {
const { user } = req.session;
if (!user) return res.redirect('/login?returnUrl=upload');
if (validateCredentials(user.username, user.password)) {
res.render('upload', { user, pfp: users.find(u => u.username === user.username).pfp });
} else {
res.json({ error: 'Invalid credentials' });
}
});
app.get('/favorites', async (req, res) => {
const { user } = req.session;
if (!user) return res.redirect('/login?returnUrl=favorites');
if (validateCredentials(user.username, user.password)) {
const favorites = await db.get(`${user.username}.favorites`) || [];
const unique = [...new Set(favorites)]
res.render('favorites', { user, files: unique, pfp: users.find(u => u.username === user.username).pfp });
} else {
res.json({ error: 'Invalid credentials' });
}
});
app.post('/favorites/add', async (req, res) => {
const { user } = req.session;
const { id } = req.body;
if (!user) return res.json({ error: 'You must be logged in to access this' });
const file = await db.get(id);
if (!file) return res.json({ error: 'Invalid file' });
const userFavs = await db.get(`${user.username}.favorites`) || [];
if (userFavs.map(f => f.id).includes(file.id)) {
await db.delete(`${user.username}.favorites`, file);
res.json({ existing: 'Removed from favorites' });
} else {
await db.push(`${user.username}.favorites`, file);
res.json({ success: 'Added to favorites' });
}
});
app.get('/login', (req, res) => {
if (req.session.user) return res.redirect('/');
res.render('login', { returnUrl: req.query.returnUrl });
});
app.post("/login", (req, res) => {
const { username, password } = req.body;
console.log(username, password);
if (validateCredentials(username, password)) {
req.session.user = {
username,
password
};
res.redirect(`/${req.query.returnUrl || ''}`);
} else return res.json({ error: 'Invalid credentials' });
});
app.post('/upload', upload.array('img', 30), function (req, res, next) {
try {
if (req.body.password) {
const { password } = req.body;
const { path: f } = req.files[0];
if (!f) return res.json({ error: 'Provide a file!' });
const id = shortid.generate();
const supportedFiletypes = [
'image/gif',
'image/jpeg',
'image/jpg',
'image/png',
];
console.log(req.files[0].mimetype)
if (!supportedFiletypes.includes(req.files[0].mimetype)) return res.json({
error: "Couldn't upload file. Please try again with one of the correct filetypes: png, jpeg, jpg, .gif'"
})
const extension = path.extname(req.files[0].originalname);
fs.rename(`./uploads/${req.files[0].path.slice(8)}`, `./uploads/${id}${extension}`, function (err) {
console.log(err);
});
users.find(u => u.username === req.session.user.username).password = password;
users.find(u => u.username === req.session.user.username).pfp = `/pfp/${id}${extension}`;
fs.writeFileSync('./assets/json/users.json', JSON.stringify(users, null, 2));
req.session.user.password = password;
res.redirect('/');
} else {
req.files.forEach(file => {
const { path: f } = file;
const { caption } = req.body;
if (!f) return res.json({ error: 'Provide a file!' });
const id = shortid.generate();
const supportedFiletypes = [
'image/gif',
'image/jpeg',
'image/jpg',
'image/png',
'video/mp4'
];
console.log(file.mimetype)
if (!supportedFiletypes.includes(file.mimetype)) return res.json({
error: "Couldn't upload file. Please try again with one of the correct filetypes: png, jpeg, jpg, .mp4, .gif'"
})
const extension = path.extname(file.originalname);
fs.rename(`./uploads/${file.path.slice(8)}`, `./uploads/${id.replace(/-/g, '')}${extension}`, function (err) {
console.log(err);
});
db.set(id.replace(/-/g, ''), {
id: id.replace(/-/g, ''),
caption,
extension,
user: req.session.user.username
});
})
setTimeout(() => {
res.redirect(`/`);
}, 300)
}
} catch (e) {
console.error(e)
return res.json({
error: "Couldn't upload file. Please try again with one of the correct filetypes: png, jpeg, jpg'"
})
}
});
app.get('/:id', async (req, res) => {
const { id } = req.params;
const data = await db.get(id.split(".")[0]);
if (!data) return res.json({ error: 'Invalid File' });
console.log(data);
res.sendFile(process.cwd() + '/uploads/' + id)
});
app.get('/pfp/:id', async (req, res) => {
const { id } = req.params;
res.sendFile(process.cwd() + '/uploads/' + id)
})
app.post('/:id/delete', async (req, res) => {
if (!req.session.user) return res.redirect('/login');
const { id } = req.params;
const data = await db.get(id.split(".")[0]);
if (!data) return res.json({ error: 'Invalid File' });
if (data.user !== req.session.user.username) return res.json({ error: 'You do not have permission to delete this file' });
await db.delete(id.split(".")[0]);
fs.unlinkSync(`./uploads/${id}${data.extension}`);
await db.delete(`${req.session.user.username}.favorites`, data);
res.json({ success: 'File deleted' });
})
app.listen(80, () => {
console.log('http://localhost:80')
});