forked from snxraven/LinkUp-P2P-Chat
revert too tired
This commit is contained in:
parent
5ff2460d23
commit
f269781226
39
app.js
39
app.js
@ -5,7 +5,7 @@ import ServeDrive from 'serve-drive';
|
|||||||
import Hyperdrive from 'hyperdrive';
|
import Hyperdrive from 'hyperdrive';
|
||||||
import Corestore from 'corestore';
|
import Corestore from 'corestore';
|
||||||
import { EventEmitter } from 'events';
|
import { EventEmitter } from 'events';
|
||||||
import fs from 'fs';
|
import fs from "fs";
|
||||||
|
|
||||||
const storagePath = `./storage/`;
|
const storagePath = `./storage/`;
|
||||||
const store = new Corestore(storagePath);
|
const store = new Corestore(storagePath);
|
||||||
@ -29,7 +29,7 @@ let config = {
|
|||||||
rooms: []
|
rooms: []
|
||||||
};
|
};
|
||||||
|
|
||||||
// Function to get a random port between 49152 and 65535
|
// Function to get a random port between 1337 and 2223
|
||||||
function getRandomPort() {
|
function getRandomPort() {
|
||||||
return Math.floor(Math.random() * (65535 - 49152 + 1)) + 49152;
|
return Math.floor(Math.random() * (65535 - 49152 + 1)) + 49152;
|
||||||
}
|
}
|
||||||
@ -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, filePath, file.type, config.userAvatar, buffer);
|
sendFileMessage(config.userName, fileUrl, file.type, config.userAvatar);
|
||||||
};
|
};
|
||||||
reader.readAsArrayBuffer(file);
|
reader.readAsArrayBuffer(file);
|
||||||
}
|
}
|
||||||
@ -121,14 +121,7 @@ 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') {
|
||||||
try {
|
addFileMessage(messageObj.name, messageObj.fileName, messageObj.fileUrl, messageObj.fileType, messageObj.avatar);
|
||||||
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);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
onMessageAdded(messageObj.name, messageObj.message, messageObj.avatar);
|
onMessageAdded(messageObj.name, messageObj.message, messageObj.avatar);
|
||||||
}
|
}
|
||||||
@ -320,16 +313,15 @@ function sendMessage(e) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function sendFileMessage(name, filePath, fileType, avatar, fileBuffer) {
|
function sendFileMessage(name, fileUrl, fileType, avatar) {
|
||||||
const fileName = filePath.split('/').pop();
|
const fileName = fileUrl.split('/').pop();
|
||||||
const messageObj = JSON.stringify({
|
const messageObj = JSON.stringify({
|
||||||
type: 'file',
|
type: 'file',
|
||||||
name,
|
name,
|
||||||
fileName,
|
fileName,
|
||||||
filePath,
|
fileUrl,
|
||||||
fileType,
|
fileType,
|
||||||
avatar,
|
avatar,
|
||||||
fileData: fileBuffer.toString('base64'),
|
|
||||||
timestamp: Date.now(),
|
timestamp: Date.now(),
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -338,7 +330,6 @@ function sendFileMessage(name, filePath, fileType, avatar, fileBuffer) {
|
|||||||
peer.write(messageObj);
|
peer.write(messageObj);
|
||||||
}
|
}
|
||||||
|
|
||||||
const fileUrl = `http://localhost:${servePort}${filePath}`;
|
|
||||||
addFileMessage(name, fileName, fileUrl, fileType, avatar);
|
addFileMessage(name, fileName, fileUrl, fileType, avatar);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -347,7 +338,7 @@ function addFileMessage(from, fileName, fileUrl, fileType, avatar) {
|
|||||||
$div.classList.add('message');
|
$div.classList.add('message');
|
||||||
|
|
||||||
const $img = document.createElement('img');
|
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');
|
$img.classList.add('avatar');
|
||||||
$div.appendChild($img);
|
$div.appendChild($img);
|
||||||
|
|
||||||
@ -361,13 +352,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);
|
||||||
@ -386,7 +377,7 @@ function updatePeerCount() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function scrollToBottom() {
|
function scrollToBottom() {
|
||||||
var container = document.getElementById('messages-container');
|
var container = document.getElementById("messages-container");
|
||||||
container.scrollTop = container.scrollHeight;
|
container.scrollTop = container.scrollHeight;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -395,7 +386,9 @@ function onMessageAdded(from, message, avatar) {
|
|||||||
$div.classList.add('message');
|
$div.classList.add('message');
|
||||||
|
|
||||||
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);
|
||||||
|
|
||||||
@ -433,7 +426,7 @@ async function updateIcon(username, avatarBuffer) {
|
|||||||
userIcon.src = avatarUrl;
|
userIcon.src = avatarUrl;
|
||||||
|
|
||||||
config.userAvatar = avatarUrl;
|
config.userAvatar = avatarUrl;
|
||||||
writeConfigToFile('./config.json');
|
writeConfigToFile("./config.json");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -452,7 +445,7 @@ function toggleSetupView() {
|
|||||||
function writeConfigToFile(filePath) {
|
function writeConfigToFile(filePath) {
|
||||||
fs.writeFile(filePath, JSON.stringify(config), (err) => {
|
fs.writeFile(filePath, JSON.stringify(config), (err) => {
|
||||||
if (err) return console.error(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();
|
return urlObject.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
initialize();
|
initialize();
|
Loading…
Reference in New Issue
Block a user