From 50026db476f16a8d5f0c0d440bd7ffb128f447b0 Mon Sep 17 00:00:00 2001 From: Raven Scott Date: Mon, 3 Jun 2024 19:53:10 -0400 Subject: [PATCH] test --- app.js | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/app.js b/app.js index ccb4289..1272852 100644 --- a/app.js +++ b/app.js @@ -3,6 +3,7 @@ import crypto from 'hypercore-crypto'; import b4a from 'b4a'; import ServeDrive from 'serve-drive'; import Localdrive from 'localdrive'; +import fs from 'fs'; const drive = new Localdrive('./storage'); let swarm; @@ -63,9 +64,16 @@ async function initialize() { swarm.on('connection', (connection, info) => { peerCount++; updatePeerCount(); - connection.on('data', (data) => { + connection.on('data', async (data) => { const messageObj = JSON.parse(data.toString()); - onMessageAdded(messageObj.name, messageObj.message, messageObj.avatar); + if (messageObj.type === 'icon') { + // Save icon to local directory + const username = messageObj.username; + const avatarBuffer = Buffer.from(messageObj.avatar, 'base64'); + await drive.put(`/icons/${username}.png`, avatarBuffer); + } else { + onMessageAdded(messageObj.name, messageObj.message, messageObj.avatar); + } }); connection.on('close', () => { peerCount--; @@ -117,8 +125,8 @@ async function registerUser(e) { const reader = new FileReader(); reader.onload = async () => { const fileBuffer = Buffer.from(reader.result); - await drive.put(`/icons/${regUsername}-${file.name}`, fileBuffer); - userAvatar = `http://localhost:1337/icons/${regUsername}-${file.name}`; + await drive.put(`/icons/${regUsername}.png`, fileBuffer); + userAvatar = `http://localhost:1337/icons/${regUsername}.png`; // Save avatar URL to localStorage localStorage.setItem('avatarURL', userAvatar); }; @@ -223,4 +231,4 @@ function onMessageAdded(from, message, avatar) { document.querySelector('#messages').appendChild($div); } -initialize(); +initialize(); \ No newline at end of file