attempts for file attachments again

This commit is contained in:
Raven Scott 2024-06-09 06:06:08 -04:00
parent 3042974e55
commit f6d9991c71

19
app.js
View File

@ -88,7 +88,7 @@ async function initialize() {
const filePath = `/files/${file.name}`; const filePath = `/files/${file.name}`;
await drive.put(filePath, buffer); await drive.put(filePath, buffer);
const fileUrl = `http://localhost:${servePort}${filePath}`; 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); reader.readAsArrayBuffer(file);
} }
@ -121,7 +121,9 @@ async function initialize() {
await drive.put(`/icons/${username}.png`, avatarBuffer); await drive.put(`/icons/${username}.png`, avatarBuffer);
updateIcon(username, avatarBuffer); updateIcon(username, avatarBuffer);
} else if (messageObj.type === 'file') { } 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 { } else {
onMessageAdded(messageObj.name, messageObj.message, messageObj.avatar); onMessageAdded(messageObj.name, messageObj.message, messageObj.avatar);
} }
@ -313,13 +315,13 @@ function sendMessage(e) {
} }
} }
function sendFileMessage(name, fileUrl, fileType, avatar) { async function sendFileMessage(name, filePath, fileType, avatar) {
const fileName = fileUrl.split('/').pop(); const fileName = filePath.split('/').pop();
const messageObj = JSON.stringify({ const messageObj = JSON.stringify({
type: 'file', type: 'file',
name, name,
fileName, fileName,
fileUrl, filePath,
fileType, fileType,
avatar, avatar,
timestamp: Date.now(), timestamp: Date.now(),
@ -330,6 +332,8 @@ function sendFileMessage(name, fileUrl, fileType, avatar) {
peer.write(messageObj); peer.write(messageObj);
} }
const fileBuffer = await drive.get(filePath);
const fileUrl = URL.createObjectURL(new Blob([fileBuffer], { type: fileType }));
addFileMessage(name, fileName, fileUrl, fileType, avatar); addFileMessage(name, fileName, fileUrl, fileType, avatar);
} }
@ -352,13 +356,13 @@ function addFileMessage(from, fileName, fileUrl, fileType, avatar) {
if (fileType.startsWith('image/')) { if (fileType.startsWith('image/')) {
const $image = document.createElement('img'); const $image = document.createElement('img');
$image.src = updatePortInUrl(fileUrl); $image.src = fileUrl;
$image.alt = fileName; $image.alt = fileName;
$image.classList.add('attached-image'); $image.classList.add('attached-image');
$content.appendChild($image); $content.appendChild($image);
} else { } else {
const $fileLink = document.createElement('a'); const $fileLink = document.createElement('a');
$fileLink.href = updatePortInUrl(fileUrl); $fileLink.href = fileUrl;
$fileLink.textContent = `File: ${fileName}`; $fileLink.textContent = `File: ${fileName}`;
$fileLink.download = fileName; $fileLink.download = fileName;
$content.appendChild($fileLink); $content.appendChild($fileLink);
@ -388,7 +392,6 @@ function onMessageAdded(from, message, avatar) {
const $img = document.createElement('img'); 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.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'); $img.classList.add('avatar');
$div.appendChild($img); $div.appendChild($img);