From f6d9991c71f4623b64b2a7180ec47e9f486e0e13 Mon Sep 17 00:00:00 2001 From: Raven Scott Date: Sun, 9 Jun 2024 06:06:08 -0400 Subject: [PATCH] attempts for file attachments again --- app.js | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/app.js b/app.js index 9626cc0..7d348fe 100644 --- a/app.js +++ b/app.js @@ -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); // Send filePath instead of fileUrl }; 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 = URL.createObjectURL(new Blob([fileBuffer], { type: messageObj.fileType })); + 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(); +async 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,8 @@ function sendFileMessage(name, fileUrl, fileType, avatar) { peer.write(messageObj); } + const fileBuffer = await drive.get(filePath); + const fileUrl = URL.createObjectURL(new Blob([fileBuffer], { type: fileType })); addFileMessage(name, fileName, fileUrl, fileType, avatar); } @@ -352,13 +356,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); @@ -388,7 +392,6 @@ function onMessageAdded(from, message, avatar) { 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);