attempts for file attachments again

This commit is contained in:
Raven Scott 2024-06-09 06:25:30 -04:00
parent 0010feed24
commit 206d095bcf

22
app.js
View File

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