diff --git a/app.js b/app.js index 9db7de8..4a8545c 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); @@ -29,7 +29,7 @@ let config = { rooms: [] }; -// Function to get a random port between 49152 and 65535 +// Function to get a random port between 1337 and 2223 function getRandomPort() { return Math.floor(Math.random() * (65535 - 49152 + 1)) + 49152; } @@ -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, buffer); + sendFileMessage(config.userName, fileUrl, file.type, config.userAvatar); }; reader.readAsArrayBuffer(file); } @@ -121,14 +121,7 @@ async function initialize() { await drive.put(`/icons/${username}.png`, avatarBuffer); updateIcon(username, avatarBuffer); } else if (messageObj.type === 'file') { - try { - 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); - } catch (error) { - console.error('Error putting file:', error); - } + addFileMessage(messageObj.name, messageObj.fileName, messageObj.fileUrl, messageObj.fileType, messageObj.avatar); } else { onMessageAdded(messageObj.name, messageObj.message, messageObj.avatar); } @@ -320,16 +313,15 @@ function sendMessage(e) { } } -function sendFileMessage(name, filePath, fileType, avatar, fileBuffer) { - const fileName = filePath.split('/').pop(); +function sendFileMessage(name, fileUrl, fileType, avatar) { + const fileName = fileUrl.split('/').pop(); const messageObj = JSON.stringify({ type: 'file', name, fileName, - filePath, + fileUrl, fileType, avatar, - fileData: fileBuffer.toString('base64'), timestamp: Date.now(), }); @@ -338,7 +330,6 @@ function sendFileMessage(name, filePath, fileType, avatar, fileBuffer) { peer.write(messageObj); } - const fileUrl = `http://localhost:${servePort}${filePath}`; addFileMessage(name, fileName, fileUrl, fileType, avatar); } @@ -347,7 +338,7 @@ function addFileMessage(from, fileName, fileUrl, fileType, avatar) { $div.classList.add('message'); const $img = document.createElement('img'); - $img.src = updatePortInUrl(avatar) || 'https://via.placeholder.com/40'; + $img.src = avatar || 'https://via.placeholder.com/40'; $img.classList.add('avatar'); $div.appendChild($img); @@ -361,13 +352,13 @@ function addFileMessage(from, fileName, fileUrl, fileType, avatar) { if (fileType.startsWith('image/')) { const $image = document.createElement('img'); - $image.src = updatePortInUrl(fileUrl); + $image.src = fileUrl; $image.alt = fileName; $image.classList.add('attached-image'); $content.appendChild($image); } else { const $fileLink = document.createElement('a'); - $fileLink.href = updatePortInUrl(fileUrl); + $fileLink.href = fileUrl; $fileLink.textContent = `File: ${fileName}`; $fileLink.download = fileName; $content.appendChild($fileLink); @@ -386,7 +377,7 @@ function updatePeerCount() { } function scrollToBottom() { - var container = document.getElementById('messages-container'); + var container = document.getElementById("messages-container"); container.scrollTop = container.scrollHeight; } @@ -395,7 +386,9 @@ 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 + console.log(updatePortInUrl(avatar)) $img.classList.add('avatar'); $div.appendChild($img); @@ -433,7 +426,7 @@ async function updateIcon(username, avatarBuffer) { userIcon.src = avatarUrl; config.userAvatar = avatarUrl; - writeConfigToFile('./config.json'); + writeConfigToFile("./config.json"); } } @@ -452,7 +445,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"); }); } @@ -463,4 +456,4 @@ function updatePortInUrl(url) { return urlObject.toString(); } -initialize(); +initialize(); \ No newline at end of file