added file audio video and files
This commit is contained in:
1 parent
8cb6143bac
commit
9016889a78
11 files changed
+988
-7
No files matched your search
@@ -1285,3 +1285,167 @@ body.modal-open {
|
||||
.modal-btn-delete-for-me:hover {
|
||||
background: var(--primary-hover);
|
||||
}
|
||||
|
||||
/* File message styles */
|
||||
.file-message {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
padding: 8px;
|
||||
background: var(--bg-tertiary);
|
||||
border-radius: 8px;
|
||||
max-width: 300px;
|
||||
}
|
||||
|
||||
.file-icon {
|
||||
font-size: 24px;
|
||||
}
|
||||
|
||||
.file-info {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.file-name {
|
||||
font-weight: 500;
|
||||
font-size: 13px;
|
||||
margin-bottom: 2px;
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
|
||||
.file-size,
|
||||
.file-type {
|
||||
font-size: 11px;
|
||||
color: var(--text-secondary);
|
||||
}
|
||||
|
||||
.download-btn {
|
||||
background: var(--primary-color);
|
||||
color: white;
|
||||
border: none;
|
||||
border-radius: 4px;
|
||||
padding: 4px 8px;
|
||||
font-size: 11px;
|
||||
cursor: pointer;
|
||||
text-decoration: none;
|
||||
display: inline-block;
|
||||
margin-top: 5px;
|
||||
}
|
||||
|
||||
.download-btn:hover {
|
||||
background: var(--primary-hover);
|
||||
}
|
||||
|
||||
/* Media message styles */
|
||||
.media-message {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 5px;
|
||||
max-width: 300px;
|
||||
}
|
||||
|
||||
.message-image {
|
||||
max-width: 100%;
|
||||
max-height: 200px;
|
||||
border-radius: 8px;
|
||||
cursor: pointer;
|
||||
object-fit: cover;
|
||||
}
|
||||
|
||||
.message-video {
|
||||
max-width: 100%;
|
||||
max-height: 200px;
|
||||
border-radius: 8px;
|
||||
}
|
||||
|
||||
.message-audio {
|
||||
max-width: 100%;
|
||||
border-radius: 8px;
|
||||
}
|
||||
|
||||
/* Image viewer modal */
|
||||
.image-viewer-modal {
|
||||
position: fixed;
|
||||
top: 0;
|
||||
left: 0;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
background: rgba(0, 0, 0, 0.9);
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
z-index: 2000;
|
||||
}
|
||||
|
||||
.image-viewer-content {
|
||||
position: relative;
|
||||
max-width: 90%;
|
||||
max-height: 90%;
|
||||
}
|
||||
|
||||
.image-viewer-large {
|
||||
max-width: 100%;
|
||||
max-height: 100%;
|
||||
object-fit: contain;
|
||||
}
|
||||
|
||||
.close-image-viewer {
|
||||
position: absolute;
|
||||
top: -40px;
|
||||
right: 0;
|
||||
color: white;
|
||||
font-size: 35px;
|
||||
font-weight: bold;
|
||||
cursor: pointer;
|
||||
z-index: 2001;
|
||||
}
|
||||
|
||||
.close-image-viewer:hover {
|
||||
color: #ccc;
|
||||
}
|
||||
|
||||
/* Upload progress styles */
|
||||
.upload-progress {
|
||||
padding: 10px;
|
||||
margin: 5px 0;
|
||||
background: var(--bg-tertiary);
|
||||
border-radius: 10px;
|
||||
align-self: flex-start;
|
||||
max-width: 85%;
|
||||
}
|
||||
|
||||
.progress-text {
|
||||
font-size: 12px;
|
||||
margin-bottom: 5px;
|
||||
color: var(--text-secondary);
|
||||
}
|
||||
|
||||
.progress-bar {
|
||||
width: 100%;
|
||||
height: 6px;
|
||||
background: var(--bg-secondary);
|
||||
border-radius: 3px;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.progress-fill {
|
||||
height: 100%;
|
||||
background: var(--primary-color);
|
||||
width: 0%;
|
||||
transition: width 0.3s ease;
|
||||
animation: progressAnimation 2s infinite;
|
||||
}
|
||||
|
||||
@keyframes progressAnimation {
|
||||
0% {
|
||||
width: 0%;
|
||||
}
|
||||
50% {
|
||||
width: 100%;
|
||||
}
|
||||
100% {
|
||||
width: 0%;
|
||||
}
|
||||
}
|
||||
+12
-1
@@ -194,7 +194,18 @@
|
||||
disabled
|
||||
rows="1"
|
||||
></textarea>
|
||||
<button class="add-button">+</button>
|
||||
<input
|
||||
type="file"
|
||||
id="fileInput"
|
||||
class="file-input"
|
||||
style="display: none"
|
||||
/>
|
||||
<button
|
||||
class="add-button"
|
||||
onclick="document.getElementById('fileInput').click()"
|
||||
>
|
||||
+
|
||||
</button>
|
||||
<button
|
||||
onclick="sendMessage()"
|
||||
class="send-button"
|
||||
|
||||
+276
-1
@@ -142,7 +142,67 @@ function displayMessage(msg, scrollToBottom) {
|
||||
|
||||
const contentDiv = document.createElement("div");
|
||||
contentDiv.className = "message-content";
|
||||
contentDiv.textContent = msg.content;
|
||||
|
||||
// Handle different message types
|
||||
if (msg.message_type === "text") {
|
||||
contentDiv.textContent = msg.content;
|
||||
} else if (msg.message_type === "file") {
|
||||
const fileInfo = JSON.parse(msg.content);
|
||||
contentDiv.innerHTML = `
|
||||
<div class="file-message">
|
||||
<div class="file-icon">📄</div>
|
||||
<div class="file-info">
|
||||
<div class="file-name">${fileInfo.filename}</div>
|
||||
<div class="file-size">${formatFileSize(fileInfo.size)}</div>
|
||||
<div class="file-type">${fileInfo.mime_type}</div>
|
||||
</div>
|
||||
<a href="${fileInfo.filepath}" download="${fileInfo.filename}" class="download-btn">⬇️ Download</a>
|
||||
</div>
|
||||
`;
|
||||
} else if (msg.message_type === "photo") {
|
||||
const photoInfo = JSON.parse(msg.content);
|
||||
contentDiv.innerHTML = `
|
||||
<div class="media-message">
|
||||
<img src="${photoInfo.filepath}" alt="Photo" class="message-image" onclick="viewImage('${photoInfo.filepath}')"/>
|
||||
<div class="file-info">
|
||||
<div class="file-name">${photoInfo.filename}</div>
|
||||
<div class="file-size">${formatFileSize(photoInfo.size)}</div>
|
||||
</div>
|
||||
<a href="${photoInfo.filepath}" download="${photoInfo.filename}" class="download-btn">⬇️ Download</a>
|
||||
</div>
|
||||
`;
|
||||
} else if (msg.message_type === "video") {
|
||||
const videoInfo = JSON.parse(msg.content);
|
||||
contentDiv.innerHTML = `
|
||||
<div class="media-message">
|
||||
<video controls class="message-video">
|
||||
<source src="${videoInfo.filepath}" type="${videoInfo.mime_type}">
|
||||
Your browser does not support the video tag.
|
||||
</video>
|
||||
<div class="file-info">
|
||||
<div class="file-name">${videoInfo.filename}</div>
|
||||
<div class="file-size">${formatFileSize(videoInfo.size)}</div>
|
||||
</div>
|
||||
<a href="${videoInfo.filepath}" download="${videoInfo.filename}" class="download-btn">⬇️ Download</a>
|
||||
</div>
|
||||
`;
|
||||
} else if (msg.message_type === "audio") {
|
||||
const audioInfo = JSON.parse(msg.content);
|
||||
contentDiv.innerHTML = `
|
||||
<div class="media-message">
|
||||
<audio controls class="message-audio">
|
||||
<source src="${audioInfo.filepath}" type="${audioInfo.mime_type}">
|
||||
Your browser does not support the audio tag.
|
||||
</audio>
|
||||
<div class="file-info">
|
||||
<div class="file-name">${audioInfo.filename}</div>
|
||||
<div class="file-size">${formatFileSize(audioInfo.size)}</div>
|
||||
</div>
|
||||
<a href="${audioInfo.filepath}" download="${audioInfo.filename}" class="download-btn">⬇️ Download</a>
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
|
||||
messageDiv.appendChild(contentDiv);
|
||||
|
||||
const timeDiv = document.createElement("div");
|
||||
@@ -182,6 +242,40 @@ function displayMessage(msg, scrollToBottom) {
|
||||
}
|
||||
}
|
||||
|
||||
function formatFileSize(bytes) {
|
||||
if (bytes === 0) return "0 Bytes";
|
||||
const k = 1024;
|
||||
const sizes = ["Bytes", "KB", "MB", "GB"];
|
||||
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
||||
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + " " + sizes[i];
|
||||
}
|
||||
|
||||
function viewImage(imageUrl) {
|
||||
// Create a modal to view the image
|
||||
const modal = document.createElement("div");
|
||||
modal.className = "image-viewer-modal";
|
||||
modal.innerHTML = `
|
||||
<div class="image-viewer-content">
|
||||
<span class="close-image-viewer">×</span>
|
||||
<img src="${imageUrl}" alt="Full size image" class="image-viewer-large">
|
||||
</div>
|
||||
`;
|
||||
|
||||
document.body.appendChild(modal);
|
||||
|
||||
// Close modal when clicking on close button
|
||||
modal.querySelector(".close-image-viewer").onclick = function () {
|
||||
document.body.removeChild(modal);
|
||||
};
|
||||
|
||||
// Close modal when clicking outside the image
|
||||
modal.onclick = function (event) {
|
||||
if (event.target === modal) {
|
||||
document.body.removeChild(modal);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function addSystemMessage(content) {
|
||||
const chatMessages = document.getElementById("chatMessages");
|
||||
|
||||
@@ -234,6 +328,163 @@ function sendMessage() {
|
||||
setTimeout(() => loadChats(), 100);
|
||||
}
|
||||
|
||||
// Queue to handle file uploads sequentially
|
||||
let uploadQueue = [];
|
||||
let isUploading = false;
|
||||
|
||||
// Function to process the upload queue
|
||||
async function processUploadQueue() {
|
||||
if (isUploading || uploadQueue.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
isUploading = true;
|
||||
const file = uploadQueue.shift(); // Get the first file in the queue
|
||||
|
||||
try {
|
||||
await sendFileMessageInternal(file);
|
||||
} catch (error) {
|
||||
console.error("Upload failed for file:", error);
|
||||
} finally {
|
||||
isUploading = false;
|
||||
// Process next file in queue if available
|
||||
setTimeout(processUploadQueue, 100); // Small delay to allow server to settle
|
||||
}
|
||||
}
|
||||
|
||||
// Internal function to handle the actual file upload
|
||||
async function sendFileMessageInternal(file) {
|
||||
if (!ws || ws.readyState !== WebSocket.OPEN) {
|
||||
showAlert("Please connect to the server first", "error");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!currentRecipient) {
|
||||
showAlert("Please select a user to chat with", "error");
|
||||
return;
|
||||
}
|
||||
|
||||
// Show upload progress
|
||||
const chatMessages = document.getElementById("chatMessages");
|
||||
const progressDiv = document.createElement("div");
|
||||
progressDiv.className = "upload-progress";
|
||||
progressDiv.innerHTML = `<div class="progress-text">Uploading ${file.name}</div><div class="progress-bar"><div class="progress-fill"></div></div>`;
|
||||
chatMessages.appendChild(progressDiv);
|
||||
|
||||
try {
|
||||
// Create FormData for file upload
|
||||
const formData = new FormData();
|
||||
formData.append("file", file);
|
||||
|
||||
// Upload file to server with better error handling
|
||||
const response = await fetch("/api/upload", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: `Bearer ${currentToken}`,
|
||||
},
|
||||
body: formData,
|
||||
}).catch((error) => {
|
||||
console.error("Network error during upload:", error);
|
||||
throw new Error("Network error during upload. Please try again.");
|
||||
});
|
||||
|
||||
// Remove progress indicator
|
||||
progressDiv.remove();
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Upload failed: ${response.status}`);
|
||||
}
|
||||
|
||||
const result = await response.json();
|
||||
|
||||
if (!result.success) {
|
||||
throw new Error(result.error || "Upload failed");
|
||||
}
|
||||
|
||||
// Create message object based on file type
|
||||
let message;
|
||||
let messageType;
|
||||
|
||||
if (result.file_type === "photo") {
|
||||
messageType = "photo";
|
||||
message = {
|
||||
id: generateId(),
|
||||
from_user: currentUser,
|
||||
to_user: currentRecipient,
|
||||
content: JSON.stringify(result.info),
|
||||
timestamp: Date.now(),
|
||||
message_type: messageType,
|
||||
};
|
||||
} else if (result.file_type === "video") {
|
||||
messageType = "video";
|
||||
message = {
|
||||
id: generateId(),
|
||||
from_user: currentUser,
|
||||
to_user: currentRecipient,
|
||||
content: JSON.stringify(result.info),
|
||||
timestamp: Date.now(),
|
||||
message_type: messageType,
|
||||
};
|
||||
} else if (result.file_type === "audio") {
|
||||
messageType = "audio";
|
||||
message = {
|
||||
id: generateId(),
|
||||
from_user: currentUser,
|
||||
to_user: currentRecipient,
|
||||
content: JSON.stringify(result.info),
|
||||
timestamp: Date.now(),
|
||||
message_type: messageType,
|
||||
};
|
||||
} else {
|
||||
messageType = "file";
|
||||
message = {
|
||||
id: generateId(),
|
||||
from_user: currentUser,
|
||||
to_user: currentRecipient,
|
||||
content: JSON.stringify(result.info),
|
||||
timestamp: Date.now(),
|
||||
message_type: messageType,
|
||||
};
|
||||
}
|
||||
|
||||
console.log("Sending file message:", message);
|
||||
ws.send(JSON.stringify(message));
|
||||
|
||||
setTimeout(() => loadChats(), 100);
|
||||
} catch (error) {
|
||||
// Remove progress indicator
|
||||
progressDiv.remove();
|
||||
console.error("Upload error:", error);
|
||||
showAlert(`Failed to upload file: ${error.message}`, "error");
|
||||
}
|
||||
}
|
||||
|
||||
// Function to send a file message (adds to queue)
|
||||
async function sendFileMessage(file) {
|
||||
// Check if file is already in queue to prevent duplicates
|
||||
const fileName = file.name;
|
||||
const fileSize = file.size;
|
||||
const isInQueue = uploadQueue.some(
|
||||
(item) => item.name === fileName && item.size === fileSize,
|
||||
);
|
||||
|
||||
if (isInQueue) {
|
||||
console.log(
|
||||
`File ${fileName} is already in upload queue, skipping duplicate.`,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
// Add file to upload queue
|
||||
uploadQueue.push(file);
|
||||
console.log(
|
||||
`File ${file.name} added to upload queue. Queue length: ${uploadQueue.length}`,
|
||||
);
|
||||
|
||||
// Process the queue
|
||||
setTimeout(processUploadQueue, 0);
|
||||
}
|
||||
|
||||
// Event listeners
|
||||
document
|
||||
.getElementById("messageInput")
|
||||
@@ -243,3 +494,27 @@ document
|
||||
sendMessage();
|
||||
}
|
||||
});
|
||||
|
||||
// Track if event listener is already attached to prevent duplicates
|
||||
if (!window.fileInputListenerAttached) {
|
||||
// File input event listener
|
||||
document.getElementById("fileInput").addEventListener("change", function (e) {
|
||||
const file = e.target.files[0];
|
||||
if (file) {
|
||||
// Add file to upload queue regardless of current upload status
|
||||
// The queue system will handle the sequencing
|
||||
uploadQueue.push(file);
|
||||
console.log(
|
||||
`File ${file.name} added to upload queue. Queue length: ${uploadQueue.length}`,
|
||||
);
|
||||
|
||||
// Process the queue
|
||||
setTimeout(processUploadQueue, 0);
|
||||
|
||||
// Reset the input so the same file can be selected again
|
||||
e.target.value = "";
|
||||
}
|
||||
});
|
||||
|
||||
window.fileInputListenerAttached = true;
|
||||
}
|
||||
Reference in new issue
Block a user