From d5ea2abb7aa6653e269d0c07d63be8d4e25ffc4b Mon Sep 17 00:00:00 2001 From: Raven Scott Date: Sun, 9 Jun 2024 06:39:15 -0400 Subject: [PATCH] add debug --- app.js | 40 +++++++++++++++------------------------- 1 file changed, 15 insertions(+), 25 deletions(-) diff --git a/app.js b/app.js index cdf9e41..9cded40 100644 --- a/app.js +++ b/app.js @@ -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(), });