-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
310 lines (269 loc) · 10 KB
/
Copy pathserver.js
File metadata and controls
310 lines (269 loc) · 10 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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
require('dotenv').config();
const express = require('express');
const session = require('express-session');
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
const bcrypt = require('bcrypt');
const http = require('http');
const { Server } = require('socket.io');
const path = require('path');
const { MongoClient, ObjectId } = require('mongodb');
const MongoStore = require('connect-mongo');
const { v2: cloudinary } = require('cloudinary');
const streamifier = require('streamifier');
const cors = require('cors');
const app = express();
const server = http.createServer(app);
const io = new Server(server);
app.use((req, res, next) => {
next();
});
app.use(cors());
const client = new MongoClient(process.env.URI);
let usersCollection, messagesCollection;
async function connectMongo() {
await client.connect();
const db = client.db('chatapp');
usersCollection = db.collection('users');
messagesCollection = db.collection('messages');
}
connectMongo().catch(console.error);
passport.use(new LocalStrategy(async (username, password, done) => {
try {
const user = await usersCollection.findOne({ username });
if (!user) return done(null, false);
const match = await bcrypt.compare(password, user.hash);
return match ? done(null, user) : done(null, false);
} catch (err) {
return done(err);
}
}));
passport.serializeUser((user, cb) => cb(null, user.username));
passport.deserializeUser(async (username, cb) => {
try {
const user = await usersCollection.findOne({ username });
cb(null, user);
} catch (err) {
cb(err);
}
});
app.use(express.urlencoded({ extended: true }));
app.use(express.json({ limit: '50mb' }));
const sessionMiddleware = session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
store: MongoStore.create({ mongoUrl: process.env.URI }),
});
app.use(sessionMiddleware);
app.use(passport.initialize());
app.use(passport.session());
cloudinary.config({
url: process.env.CLOUDINARY_URL,
});
app.post('/api/upload-image', async (req, res) => {
try {
console.log('Upload route hit');
const { image } = req.body;
if (!image || typeof image !== 'string') {
return res.status(400).json({ success: false, message: 'Image data missing or invalid.' });
}
const matches = image.match(/^data:image\/([a-zA-Z]+);base64,(.+)$/);
if (!matches) {
return res.status(400).json({ success: false, message: 'Invalid image format.' });
}
const base64Data = matches[2];
const buffer = Buffer.from(base64Data, 'base64');
if (buffer.length > 32 * 1024 * 1024) {
return res.status(413).json({ success: false, message: 'Image too large.' });
}
if (!process.env.CLOUDINARY_URL) {
console.error('Missing CLOUDINARY_URL key');
return res.status(500).json({ success: false, message: 'Server misconfigured: missing CLOUDINARY_URL.' });
}
const uploadFromBuffer = (buffer) =>
new Promise((resolve, reject) => {
const stream = cloudinary.uploader.upload_stream(
{ folder: 'chatapp' },
(error, result) => {
if (error) reject(error);
else resolve(result);
}
);
streamifier.createReadStream(buffer).pipe(stream);
});
const result = await uploadFromBuffer(buffer);
console.log('Cloudinary response:', result);
if (result?.secure_url) {
return res.json({ success: true, url: result.secure_url });
} else {
console.error('Unexpected Cloudinary response:', result);
return res.status(502).json({ success: false, message: 'Unexpected response from Cloudinary.' });
}
} catch (error) {
console.error('Upload error:', error);
return res.status(500).json({ success: false, message: `Upload failed: ${error.message || 'Unknown error'}` });
}
});
app.get('/register', (req, res) => res.sendFile(path.join(__dirname, 'public/register.html')));
app.post('/register', async (req, res) => {
const { username, password } = req.body;
try {
const existing = await usersCollection.findOne({ username });
if (existing) return res.send('Username already exists.');
const hash = await bcrypt.hash(password, 10);
await usersCollection.insertOne({ username, hash });
res.redirect('/login');
} catch (err) {
console.error(err);
res.status(500).send('Registration error');
}
});
app.get('/login', (req, res) => res.sendFile(path.join(__dirname, 'public/login.html')));
app.post('/login', (req, res, next) => {
passport.authenticate('local', (err, user) => {
if (err || !user) return res.status(401).send('Login failed');
req.logIn(user, (err) => {
if (err) return res.status(500).send('Login error');
res.json({ success: true, username: user.username });
});
})(req, res, next);
});
app.get('/messages', async (req, res) => {
const skip = parseInt(req.query.skip) || 0;
const channel = req.query.channel || 'general';
try {
const msgs = await messagesCollection.find({ channel }).sort({ timestamp: -1 }).skip(skip).limit(50).toArray();
res.json(msgs.reverse());
} catch (err) {
console.error(err);
res.status(500).send('Failed to load messages');
}
});
app.put('/messages/:id', async (req, res) => {
const { id } = req.params;
const { newMessage } = req.body;
try {
const msg = await messagesCollection.findOne({ _id: new ObjectId(id) });
if (!msg) return res.status(404).send('Message not found');
if (msg.username !== req.user.username) return res.status(403).send('Forbidden');
await messagesCollection.updateOne({ _id: new ObjectId(id) }, { $set: { message: newMessage } });
const updated = { ...msg, message: newMessage };
io.emit('message edited', updated);
res.send(updated);
} catch (err) {
console.error(err);
res.status(500).send('Edit error');
}
});
function getPublicIdFromUrl(url) {
if (!url) return null;
const cleanUrl = url.split('?')[0];
const parts = cleanUrl.split('/upload/');
if (parts.length < 2) return null;
const pathWithVersion = parts[1];
const path = pathWithVersion.replace(/^v\d+\//, '');
const lastDot = path.lastIndexOf('.');
return lastDot === -1 ? path : path.substring(0, lastDot);
}
app.delete('/messages/:id', async (req, res) => {
const { id } = req.params;
try {
const msg = await messagesCollection.findOne({ _id: new ObjectId(id) });
if (!msg) return res.status(404).send('Message not found');
if (msg.username !== req.user.username) return res.status(403).send('Forbidden');
if (msg.imageUrl) {
const publicId = getPublicIdFromUrl(msg.imageUrl);
if (publicId) {
cloudinary.uploader.destroy(publicId, (result) => {
console.log('Cloudinary delete result:', result);
});
}
}
await messagesCollection.deleteOne({ _id: new ObjectId(id) });
io.emit('message deleted', id);
res.send({ success: true });
} catch (err) {
console.error(err);
res.status(500).send('Delete error');
}
});
app.get('/api/klipy-search', async (req, res) => {
const { q } = req.query;
if (!q) {
return res.status(400).json({ error: 'Search query is required' });
}
const apiKey = process.env.KLIPY_API;
if (!apiKey) {
return res.status(500).json({ error: 'Klipy API key is not configured on the server' });
}
const url = `https://api.klipy.com/v2/search?q=${encodeURIComponent(q)}&key=${apiKey}&limit=12`;
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Klipy API request failed with status ${response.status}`);
}
const data = await response.json();
res.json(data);
} catch (error) {
console.error('Error fetching from Klipy API:', error);
res.status(500).json({ error: 'Failed to fetch from Klipy API' });
}
});
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', (req, res) => res.sendFile(path.join(__dirname, 'public/home.html')));
app.get('/app', (req, res) => {
if (!req.isAuthenticated()) return res.redirect('/login');
res.sendFile(path.join(__dirname, 'public/app.html'));
});
const onlineUsers = new Map();
const wrap = (mw) => (socket, next) => mw(socket.request, {}, next);
io.use(wrap(sessionMiddleware));
io.use(wrap(passport.initialize()));
io.use(wrap(passport.session()));
io.use((socket, next) => socket.request.user ? next() : next(new Error('unauthorized')));
io.on('connection', async (socket) => {
const username = socket.request.user?.username;
if (!username) return;
socket.channel = 'general';
socket.join('general');
const count = (onlineUsers.get(username) || 0) + 1;
onlineUsers.set(username, count);
io.emit('online users', Array.from(onlineUsers.keys()));
const history = await messagesCollection.find({ channel: 'general' }).sort({ timestamp: -1 }).limit(50).toArray();
socket.emit('chat history', history.reverse());
socket.on('join channel', (channel) => {
socket.leave(socket.channel);
socket.join(channel);
socket.channel = channel;
messagesCollection.find({ channel }).sort({ timestamp: -1 }).limit(50).toArray().then(history => {
socket.emit('chat history', history.reverse());
});
});
socket.on('chat message', async (data) => {
const msg = {
username,
message: data.message,
imageUrl: data.imageUrl,
postid: data.postid,
type: data.type || 'chat',
timestamp: new Date(),
replyTo: data.replyTo,
channel: socket.channel
};
await messagesCollection.insertOne(msg);
io.to(socket.channel).emit('chat message', msg);
});
socket.on('disconnect', () => {
const count = onlineUsers.get(username) - 1;
if (count > 0) {
onlineUsers.set(username, count);
} else {
onlineUsers.delete(username);
}
io.emit('online users', Array.from(onlineUsers.keys()));
});
});
process.on('uncaughtException', (err) => console.error('Uncaught Exception:', err));
process.on('unhandledRejection', (reason, promise) => console.error('Unhandled Rejection at:', promise, 'reason:', reason));
server.listen(3000, () => console.log('Server running on http://localhost:3000'));