This commit is contained in:
Raven Scott 2024-06-03 19:53:10 -04:00
parent 4c6cdb9170
commit 50026db476

18
app.js
View File

@ -3,6 +3,7 @@ import crypto from 'hypercore-crypto';
import b4a from 'b4a'; import b4a from 'b4a';
import ServeDrive from 'serve-drive'; import ServeDrive from 'serve-drive';
import Localdrive from 'localdrive'; import Localdrive from 'localdrive';
import fs from 'fs';
const drive = new Localdrive('./storage'); const drive = new Localdrive('./storage');
let swarm; let swarm;
@ -63,9 +64,16 @@ async function initialize() {
swarm.on('connection', (connection, info) => { swarm.on('connection', (connection, info) => {
peerCount++; peerCount++;
updatePeerCount(); updatePeerCount();
connection.on('data', (data) => { connection.on('data', async (data) => {
const messageObj = JSON.parse(data.toString()); 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', () => { connection.on('close', () => {
peerCount--; peerCount--;
@ -117,8 +125,8 @@ async function registerUser(e) {
const reader = new FileReader(); const reader = new FileReader();
reader.onload = async () => { reader.onload = async () => {
const fileBuffer = Buffer.from(reader.result); const fileBuffer = Buffer.from(reader.result);
await drive.put(`/icons/${regUsername}-${file.name}`, fileBuffer); await drive.put(`/icons/${regUsername}.png`, fileBuffer);
userAvatar = `http://localhost:1337/icons/${regUsername}-${file.name}`; userAvatar = `http://localhost:1337/icons/${regUsername}.png`;
// Save avatar URL to localStorage // Save avatar URL to localStorage
localStorage.setItem('avatarURL', userAvatar); localStorage.setItem('avatarURL', userAvatar);
}; };
@ -223,4 +231,4 @@ function onMessageAdded(from, message, avatar) {
document.querySelector('#messages').appendChild($div); document.querySelector('#messages').appendChild($div);
} }
initialize(); initialize();