From 206d095bcfc6f3de7d3253aeecd590e849947def Mon Sep 17 00:00:00 2001 From: Raven Scott Date: Sun, 9 Jun 2024 06:25:30 -0400 Subject: [PATCH] attempts for file attachments again --- app.js | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/app.js b/app.js index 0300163..3319e8d 100644 --- a/app.js +++ b/app.js @@ -5,7 +5,7 @@ import ServeDrive from 'serve-drive'; import Hyperdrive from 'hyperdrive'; import Corestore from 'corestore'; import { EventEmitter } from 'events'; -import fs from "fs"; +import fs from 'fs'; const storagePath = `./storage/`; const store = new Corestore(storagePath); @@ -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, fileUrl, file.type, config.userAvatar); + sendFileMessage(config.userName, filePath, file.type, config.userAvatar); }; reader.readAsArrayBuffer(file); } @@ -121,7 +121,9 @@ async function initialize() { await drive.put(`/icons/${username}.png`, avatarBuffer); updateIcon(username, avatarBuffer); } else if (messageObj.type === 'file') { - addFileMessage(messageObj.name, messageObj.fileName, messageObj.fileUrl, messageObj.fileType, messageObj.avatar); + const fileBuffer = await drive.get(messageObj.filePath); + 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); } @@ -313,13 +315,13 @@ function sendMessage(e) { } } -function sendFileMessage(name, fileUrl, fileType, avatar) { - const fileName = fileUrl.split('/').pop(); +function sendFileMessage(name, filePath, fileType, avatar) { + const fileName = filePath.split('/').pop(); const messageObj = JSON.stringify({ type: 'file', name, fileName, - fileUrl, + filePath, fileType, avatar, timestamp: Date.now(), @@ -330,6 +332,7 @@ function sendFileMessage(name, fileUrl, fileType, avatar) { peer.write(messageObj); } + const fileUrl = `http://localhost:${servePort}${filePath}`; addFileMessage(name, fileName, fileUrl, fileType, avatar); } @@ -377,7 +380,7 @@ function updatePeerCount() { } function scrollToBottom() { - var container = document.getElementById("messages-container"); + var container = document.getElementById('messages-container'); container.scrollTop = container.scrollHeight; } @@ -386,7 +389,6 @@ function onMessageAdded(from, message, avatar) { $div.classList.add('message'); const $img = document.createElement('img'); - $img.src = updatePortInUrl(avatar) || 'https://via.placeholder.com/40'; // Default to a placeholder image if avatar URL is not provided $img.classList.add('avatar'); $div.appendChild($img); @@ -425,7 +427,7 @@ async function updateIcon(username, avatarBuffer) { userIcon.src = avatarUrl; config.userAvatar = avatarUrl; - writeConfigToFile("./config.json"); + writeConfigToFile('./config.json'); } } @@ -444,7 +446,7 @@ function toggleSetupView() { function writeConfigToFile(filePath) { fs.writeFile(filePath, JSON.stringify(config), (err) => { if (err) return console.error(err); - console.log("File has been created"); + console.log('File has been created'); }); }