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

14
app.js
View File

@ -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());
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);
};