add debug

This commit is contained in:
Raven Scott 2024-06-09 06:39:15 -04:00
parent 3835149c92
commit d5ea2abb7a

40
app.js
View File

@ -88,7 +88,7 @@ async function initialize() {
const filePath = `/files/${file.name}`;
await drive.put(filePath, buffer);
const fileUrl = `http://localhost:${servePort}${filePath}`;
sendFileMessage(config.userName, filePath, file.type, config.userAvatar);
sendFileMessage(config.userName, filePath, file.type, config.userAvatar, buffer);
};
reader.readAsArrayBuffer(file);
}
@ -121,17 +121,10 @@ async function initialize() {
await drive.put(`/icons/${username}.png`, avatarBuffer);
updateIcon(username, avatarBuffer);
} else if (messageObj.type === 'file') {
try {
const fileBuffer = await drive.get(messageObj.filePath);
if (fileBuffer) {
const fileUrl = `http://localhost:${servePort}${messageObj.filePath}`;
addFileMessage(messageObj.name, messageObj.fileName, fileUrl, messageObj.fileType, messageObj.avatar);
} else {
console.error('File not found:', messageObj.filePath);
}
} catch (error) {
console.error('Error getting file:', error);
}
const fileBuffer = Buffer.from(messageObj.fileData, 'base64');
await drive.put(messageObj.filePath, fileBuffer);
const fileUrl = `http://localhost:${servePort}${messageObj.filePath}`;
addFileMessage(messageObj.name, messageObj.fileName, fileUrl, messageObj.fileType, messageObj.avatar);
} else {
onMessageAdded(messageObj.name, messageObj.message, messageObj.avatar);
}
@ -143,18 +136,14 @@ async function initialize() {
console.log('Peer connected, current peer count:', peerCount);
// Send the current user's icon to the new peer
try {
const iconBuffer = await drive.get(`/icons/${config.userName}.png`);
if (iconBuffer) {
const iconMessage = JSON.stringify({
type: 'icon',
username: config.userName,
avatar: iconBuffer.toString('base64'),
});
connection.write(iconMessage);
}
} catch (error) {
console.error('Error getting icon:', error);
const iconBuffer = await drive.get(`/icons/${config.userName}.png`);
if (iconBuffer) {
const iconMessage = JSON.stringify({
type: 'icon',
username: config.userName,
avatar: iconBuffer.toString('base64'),
});
connection.write(iconMessage);
}
connection.on('data', (data) => {
@ -327,7 +316,7 @@ function sendMessage(e) {
}
}
function sendFileMessage(name, filePath, fileType, avatar) {
function sendFileMessage(name, filePath, fileType, avatar, fileBuffer) {
const fileName = filePath.split('/').pop();
const messageObj = JSON.stringify({
type: 'file',
@ -336,6 +325,7 @@ function sendFileMessage(name, filePath, fileType, avatar) {
filePath,
fileType,
avatar,
fileData: fileBuffer.toString('base64'),
timestamp: Date.now(),
});