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