Skip to content

Commit

Permalink
server chat image fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ION606 committed Mar 26, 2024
1 parent f5e7972 commit 64e85ec
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 20 deletions.
14 changes: 12 additions & 2 deletions client/scripts/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ function messageRecieved(response) {
if (response.code == 6) return console.error("SERVER MESSAGES SHOULD NOT GO THROUGH HERE!");

if (response.op != 0 && response.data.channelId != localStorage.getItem('currentChatID')) return;

console.log("MSGRES", response);
switch (response.op) {
case 0: addMessage(response.data);
break;
Expand Down Expand Up @@ -385,6 +385,10 @@ function createNewMessage(msg) {
msgContentContainer.style.height = '200px';
}

const isServer = window.location.pathname.includes('/server/');
const serverId = (isServer) ? window.location.pathname.replace('/server/', '').replace('/', '') : undefined;
if (serverId) { req.setRequestHeader('serverid', serverId); }

req.setRequestHeader('sessionid', localStorage.getItem('sessionid'));
req.setRequestHeader('channelid', localStorage.getItem('currentChatID'));
req.setRequestHeader('username', JSON.parse(localStorage.getItem('user')).username);
Expand Down Expand Up @@ -608,16 +612,22 @@ async function handlePastedImage(file) {
req.responseType = 'text';

req.onloadend = () => {
if (req.response != "OK") alert("request failed!");
if (req.response != "OK") {
alert("request failed!");
console.error(req.response);
}
}

var fname = file.name.split(".");
if (fname.length === 1 || (fname[0] === "" && fname.length === 2)) {
return alert("please provide a valid file!");
}

const isServer = window.location.pathname.includes('/server/');
const serverId = (isServer) ? window.location.pathname.replace('/server/', '').replace('/', '') : undefined;
req.setRequestHeader('sessionid', localStorage.getItem('sessionid'));
req.setRequestHeader('channelid', localStorage.getItem('currentChatID'));
if (serverId) { req.setRequestHeader('serverid', serverId); }
req.setRequestHeader('fext', fname.pop());
req.setRequestHeader('username', JSON.parse(localStorage.getItem('user')).username);
req.setRequestHeader('Content-Type', 'application/octet-stream');
Expand Down
1 change: 0 additions & 1 deletion server/database/media/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ export class fileManager {
else resolve(data);
});
});

return (res) ? res.Body : null;
}

Expand Down
17 changes: 12 additions & 5 deletions server/database/newMessage.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { getUidFromSid, wasabiManager } from "../imports.js";
import { randomUUID } from 'crypto';
import { SERVERMACROS as MACROS } from "../macros.js";
import { handleChatServer } from '../guilds/chatServer.js';

export async function broadcastToSessions(client, connectionMap, others, toSend, encDoc, checkPerms = undefined) {
try {
Expand Down Expand Up @@ -160,8 +161,8 @@ export async function markDMAsRead(mongoconnection, connectionMap, data) {
async function uploadMsgImg(mongoconnection, CDNManager, connectionMap, data) {
try {
if (!data || !data.buf || data.buf.byteLength/1000000 > 10) return false;
const uploadPath = (data.serverId) ? `${data.serverId}/${data.channelid}` : data.channelid
const response = await CDNManager.uploadFile(data.channelid, data.filename, data.buf);
const uploadPath = (data.serverId) ? `${data.serverId}/${data.channelId}` : data.channelId;
const response = await CDNManager.uploadFile(uploadPath, data.filename, data.buf);

if (response && response.type && response.code) return response;
const uid = getUidFromSid(data.sid);
Expand All @@ -171,15 +172,21 @@ async function uploadMsgImg(mongoconnection, CDNManager, connectionMap, data) {
uid: uid,
username: data.username
},
channelId: data.channelid,
channelId: data.channelId,
id: randomUUID(),
timestamp: (new Date()).toISOString(),
content: {
filename: data.filename
}
},
sid: data.sid,
serverId: data.serverId
}

return await newMessage(mongoconnection, connectionMap, msg);
if (data.serverId) {
await handleChatServer(null, connectionMap, mongoconnection, {op: MACROS.SERVER.OPS.SEND_MESSAGE, data: msg});
return true;
}
else return await newMessage(mongoconnection, connectionMap, msg);
}
catch (err) {
console.error(err);
Expand Down
2 changes: 1 addition & 1 deletion server/guilds/chatServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ async function handleMessage(ws, connectionMap, mongoconnection, data, op) {
}
catch (err) {
console.error(err);
return ws.send(JSON.stringify({ msgId: data.id || null, serverId: data.serverId, channelId: data.channelId, code: 500, type: 1 }));
return ws?.send(JSON.stringify({ msgId: data.id || null, serverId: data.serverId, channelId: data.channelId, code: 500, type: 1 }));
}
}

Expand Down
26 changes: 15 additions & 11 deletions server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,18 +100,19 @@ turnServer.start();

app.put('/msgImg', async (req, res) => {
try {
const sid = req.headers.sessionid;
const channelid = req.headers.channelid;
const fext = req.headers.fext;
const username = req.headers.username;
if (!sid) return res.sendStatus(401);
if (!channelid || !username) return res.sendStatus(404);
const {sessionid: sid, channelid: channelId, fext, username, serverid: serverId} = req.headers;

if (!sid || !(await validateSession(mongoconnection, sid))) return res.sendStatus(401);
if (!channelId || !username) return res.sendStatus(404);
if (!fext) return res.sendStatus(409);

const buf = Buffer.from(req.body, 'base64');
const filename = Math.random().toString(36).slice(2);

const response = await handleMessage(mongoconnection, webSocketClients, {files: req.body, sid: sid, username: username, channelid: channelid, filename: `${filename}.${fext}`, buf: buf}, MACROS.MESSAGE.OPS.IMAGE, CDNManager);
const data = {files: req.body, sid, username, channelId, filename: `${filename}.${fext}`, buf};
if (serverId) data['serverId'] = serverId;

const response = await handleMessage(mongoconnection, webSocketClients, data, MACROS.MESSAGE.OPS.IMAGE, CDNManager);
if (!response) return res.sendStatus(500);
res.sendStatus(200);
}
Expand Down Expand Up @@ -202,14 +203,17 @@ app.get('/getpfp', async (req, res) => {

app.get('/msgImg', async (req, res) => {
try {
const { sessionid, channelid, username } = req.headers;
if (!sessionid) return res.sendStatus(401);
const { sessionid, channelid, username, serverid } = req.headers;
if (!sessionid || !(await validateSession(mongoconnection, sessionid))) return res.sendStatus(401);
if (!channelid || !username) return res.sendStatus(409);

const fname = req.query.fname;
if (!fname) return res.sendStatus(404);

const channelIDFull = (serverid) ? `${serverid}/${channelid}` : channelid;
console.log(channelIDFull);

const file = await CDNManager.getFile(channelid, fname);
const file = await CDNManager.getFile(channelIDFull, fname);
return res.send(file);
}
catch (err) {
Expand Down Expand Up @@ -480,7 +484,7 @@ app.ws('/websocket', async (ws, req) => {
app.get('*', async (req, res) => {
console.error(`UNKNOWN URL: ${req.url}`);
res.sendStatus(404);
})
});


app.listen(port, () => console.log(`App listening on port ${port}`));

0 comments on commit 64e85ec

Please sign in to comment.