From 4d9d22060b450ded92e39985e3ec347600ea8c73 Mon Sep 17 00:00:00 2001 From: Raven Scott Date: Fri, 14 Jun 2024 01:56:46 -0400 Subject: [PATCH] Adding hyperdrive files management via ~list-files --- app.js | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++--- commands.js | 9 +++++++-- 2 files changed, 60 insertions(+), 5 deletions(-) diff --git a/app.js b/app.js index 7374ac8..fe94413 100644 --- a/app.js +++ b/app.js @@ -74,6 +74,17 @@ async function createRoom(alias) { await joinSwarm(topicBuffer); } +async function listFiles() { + const files = []; + for await (const entry of drive.readdir('/files')) { + files.push(entry); + } + return files; +} +async function deleteFile(filename) { + await drive.del(`/files/${filename}`); +} + async function initialize() { try { servePort = getRandomPort(); @@ -525,11 +536,13 @@ async function sendMessage(e) { addMessage: (from, message, avatar, topic) => onMessageAdded(from, message, avatar, topic, timestamp), joinRoom, leaveRoom, - createRoom + createRoom, + listFiles, + deleteFile }); return; } - + console.log('Sending message:', message); // Debugging log onMessageAdded(config.userName, message, config.userAvatar, topic, timestamp); @@ -668,6 +681,34 @@ function onMessageAdded(from, message, avatar, topic, timestamp) { const $text = document.createElement('div'); $text.classList.add('message-text'); + if (message.includes('Available files:')) { + const files = message.split('\n').slice(1); // Skip the "Available files:" line + const fileList = document.createElement('ul'); + + files.forEach(file => { + file = file.replace("- ", "") + const listItem = document.createElement('li'); + const fileButton = document.createElement('button'); + fileButton.textContent = file.trim(); + fileButton.onclick = () => downloadFile(file.trim()); + + const deleteButton = document.createElement('button'); + deleteButton.textContent = 'Delete'; + deleteButton.onclick = () => { + console.log("file to delete: ", file); + deleteFile(file); + listItem.remove(); + }; + + + listItem.appendChild(fileButton); + listItem.appendChild(deleteButton); + fileList.appendChild(listItem); + }); + + $text.appendChild(fileList); + } else { + const md = window.markdownit({ highlight: function (str, lang) { if (lang && hljs.getLanguage(lang)) { @@ -678,9 +719,9 @@ function onMessageAdded(from, message, avatar, topic, timestamp) { return ''; // use external default escaping } }); - const markdownContent = md.render(message); $text.innerHTML = markdownContent; + } $content.appendChild($header); $content.appendChild($text); @@ -693,6 +734,15 @@ function onMessageAdded(from, message, avatar, topic, timestamp) { } } +function downloadFile(filename) { + const fileUrl = `http://localhost:${servePort}/files/${filename}`; + const a = document.createElement('a'); + a.href = fileUrl; + a.download = filename; + a.click(); +} + + function addAudioMessage(from, audioUrl, avatar, topic) { console.log('Adding audio message:', { from, audioUrl, avatar, topic }); // Debugging log const $div = document.createElement('div'); diff --git a/commands.js b/commands.js index 2d34f2e..2d6b26f 100644 --- a/commands.js +++ b/commands.js @@ -12,7 +12,7 @@ if (fs.existsSync(agentAvatarPath)) { } export default async function handleCommand(command, context) { - const { eventEmitter, currentTopic, clearMessages, addMessage, joinRoom, leaveRoom, createRoom } = context; + const { eventEmitter, currentTopic, clearMessages, addMessage, joinRoom, leaveRoom, createRoom, listFiles } = context; const args = command.trim().split(' '); const cmd = args[0].toLowerCase(); @@ -26,7 +26,7 @@ export default async function handleCommand(command, context) { addMessage('LinkUp', 'pong', agentAvatar, currentTopic()); break; case '~help': - addMessage('LinkUp', 'Available commands:\n- ~clear\n- ~ping\n- ~help\n- ~join [topic]\n- ~leave\n- ~create [alias]', agentAvatar, currentTopic()); + addMessage('LinkUp', 'Available commands:\n- ~clear\n- ~ping\n- ~help\n- ~join [topic]\n- ~leave\n- ~create [alias]\n- ~list-files', agentAvatar, currentTopic()); break; case '~join': if (restArgs) { @@ -45,6 +45,11 @@ export default async function handleCommand(command, context) { addMessage('LinkUp', 'Usage: ~create [alias]', agentAvatar, currentTopic()); } break; + case '~list-files': + const files = await listFiles(); + const fileList = files.length > 0 ? files.map(file => `- ${file}`).join('\n') : 'No files available'; + addMessage('LinkUp', `Available files:\n${fileList}`, agentAvatar, currentTopic()); + break; default: console.log('Unknown command:', command); }