forked from snxraven/LinkUp-P2P-Chat
attempts for file attachments again
This commit is contained in:
parent
3042974e55
commit
f6d9991c71
19
app.js
19
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);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user