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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// ============================================
|
| 2 |
+
// Bot Hosting Panel - Frontend JavaScript
|
| 3 |
+
// ============================================
|
| 4 |
+
|
| 5 |
+
let socket = null;
|
| 6 |
+
let currentBot = null;
|
| 7 |
+
let consoleInterval = null;
|
| 8 |
+
|
| 9 |
+
// Initialize when DOM is ready
|
| 10 |
+
document.addEventListener('DOMContentLoaded', () => {
|
| 11 |
+
initializeApp();
|
| 12 |
+
});
|
| 13 |
+
|
| 14 |
+
function initializeApp() {
|
| 15 |
+
// Check if we're on login page
|
| 16 |
+
if (window.location.pathname === '/login') {
|
| 17 |
+
initializeLogin();
|
| 18 |
+
return;
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
// Initialize socket connection
|
| 22 |
+
initializeSocket();
|
| 23 |
+
|
| 24 |
+
// Load bots if on dashboard
|
| 25 |
+
if (window.location.pathname === '/' || window.location.pathname === '') {
|
| 26 |
+
loadBots();
|
| 27 |
+
initializeUploadModal();
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
// Initialize file manager if on filemanager page
|
| 31 |
+
if (window.location.pathname.startsWith('/filemanager')) {
|
| 32 |
+
initializeFileManager();
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
// Update bots status periodically
|
| 36 |
+
setInterval(() => {
|
| 37 |
+
if (window.location.pathname === '/' || window.location.pathname === '') {
|
| 38 |
+
loadBots();
|
| 39 |
+
}
|
| 40 |
+
}, 5000);
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
// ============================================
|
| 44 |
+
// Login Functions
|
| 45 |
+
// ============================================
|
| 46 |
+
function initializeLogin() {
|
| 47 |
+
const form = document.getElementById('loginForm');
|
| 48 |
+
if (!form) return;
|
| 49 |
+
|
| 50 |
+
form.addEventListener('submit', async (e) => {
|
| 51 |
+
e.preventDefault();
|
| 52 |
+
|
| 53 |
+
const username = document.getElementById('username').value;
|
| 54 |
+
const password = document.getElementById('password').value;
|
| 55 |
+
const errorDiv = document.getElementById('errorMessage');
|
| 56 |
+
const btnText = form.querySelector('.btn-text');
|
| 57 |
+
const btnLoading = form.querySelector('.btn-loading');
|
| 58 |
+
|
| 59 |
+
// Show loading state
|
| 60 |
+
btnText.style.display = 'none';
|
| 61 |
+
btnLoading.style.display = 'inline';
|
| 62 |
+
errorDiv.textContent = '';
|
| 63 |
+
|
| 64 |
+
try {
|
| 65 |
+
const response = await fetch('/api/login', {
|
| 66 |
+
method: 'POST',
|
| 67 |
+
headers: { 'Content-Type': 'application/json' },
|
| 68 |
+
body: JSON.stringify({ username, password })
|
| 69 |
+
});
|
| 70 |
+
|
| 71 |
+
const data = await response.json();
|
| 72 |
+
|
| 73 |
+
if (response.ok && data.success) {
|
| 74 |
+
window.location.href = data.redirect || '/';
|
| 75 |
+
} else {
|
| 76 |
+
errorDiv.textContent = data.error || 'Login failed';
|
| 77 |
+
btnText.style.display = 'inline';
|
| 78 |
+
btnLoading.style.display = 'none';
|
| 79 |
+
}
|
| 80 |
+
} catch (error) {
|
| 81 |
+
errorDiv.textContent = 'Network error. Please try again.';
|
| 82 |
+
btnText.style.display = 'inline';
|
| 83 |
+
btnLoading.style.display = 'none';
|
| 84 |
+
}
|
| 85 |
+
});
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
function logout() {
|
| 89 |
+
fetch('/api/logout')
|
| 90 |
+
.then(() => {
|
| 91 |
+
window.location.href = '/login';
|
| 92 |
+
})
|
| 93 |
+
.catch(() => {
|
| 94 |
+
window.location.href = '/login';
|
| 95 |
+
});
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
// ============================================
|
| 99 |
+
// Socket.io
|
| 100 |
+
// ============================================
|
| 101 |
+
function initializeSocket() {
|
| 102 |
+
socket = io();
|
| 103 |
+
|
| 104 |
+
socket.on('connect', () => {
|
| 105 |
+
console.log('Socket connected');
|
| 106 |
+
});
|
| 107 |
+
|
| 108 |
+
socket.on('botStatusChanged', (data) => {
|
| 109 |
+
console.log('Bot status changed:', data);
|
| 110 |
+
loadBots(); // Refresh bot list
|
| 111 |
+
});
|
| 112 |
+
|
| 113 |
+
socket.on('botDeleted', (data) => {
|
| 114 |
+
console.log('Bot deleted:', data);
|
| 115 |
+
loadBots(); // Refresh bot list
|
| 116 |
+
});
|
| 117 |
+
|
| 118 |
+
socket.on('logs-update', (data) => {
|
| 119 |
+
if (data.botName === currentBot) {
|
| 120 |
+
const consoleOutput = document.getElementById('consoleOutput');
|
| 121 |
+
if (consoleOutput) {
|
| 122 |
+
consoleOutput.textContent = data.logs || 'No logs available';
|
| 123 |
+
consoleOutput.scrollTop = consoleOutput.scrollHeight;
|
| 124 |
+
}
|
| 125 |
+
}
|
| 126 |
+
});
|
| 127 |
+
|
| 128 |
+
socket.on('disconnect', () => {
|
| 129 |
+
console.log('Socket disconnected');
|
| 130 |
+
});
|
| 131 |
+
}
|
| 132 |
+
|
| 133 |
+
// ============================================
|
| 134 |
+
// Bot Management
|
| 135 |
+
// ============================================
|
| 136 |
+
async function loadBots() {
|
| 137 |
+
const container = document.getElementById('botsContainer');
|
| 138 |
+
if (!container) return;
|
| 139 |
+
|
| 140 |
+
try {
|
| 141 |
+
const response = await fetch('/api/bots');
|
| 142 |
+
const data = await response.json();
|
| 143 |
+
|
| 144 |
+
if (!data.bots || data.bots.length === 0) {
|
| 145 |
+
container.innerHTML = `
|
| 146 |
+
<div class="loading" style="grid-column: 1/-1; text-align: center; padding: 60px;">
|
| 147 |
+
<h3>No bots found</h3>
|
| 148 |
+
<p style="margin-top: 8px; color: var(--text-muted);">Click "New Bot" to upload your first bot</p>
|
| 149 |
+
</div>
|
| 150 |
+
`;
|
| 151 |
+
return;
|
| 152 |
+
}
|
| 153 |
+
|
| 154 |
+
container.innerHTML = data.bots.map(bot => createBotCard(bot)).join('');
|
| 155 |
+
} catch (error) {
|
| 156 |
+
container.innerHTML = '<div class="loading">Error loading bots</div>';
|
| 157 |
+
console.error('Error loading bots:', error);
|
| 158 |
+
}
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
function createBotCard(bot) {
|
| 162 |
+
const isOnline = bot.status === 'online';
|
| 163 |
+
const uptime = bot.uptime ? formatUptime(Date.now() - bot.uptime) : 'N/A';
|
| 164 |
+
|
| 165 |
+
return `
|
| 166 |
+
<div class="bot-card">
|
| 167 |
+
<div class="bot-card-header">
|
| 168 |
+
<div class="bot-name">${escapeHtml(bot.name)}</div>
|
| 169 |
+
<span class="bot-type ${bot.type}">${bot.type}</span>
|
| 170 |
+
</div>
|
| 171 |
+
<div class="status-indicator">
|
| 172 |
+
<span class="status-dot ${isOnline ? 'online' : 'offline'}"></span>
|
| 173 |
+
<span>${isOnline ? 'Running' : 'Offline'}</span>
|
| 174 |
+
</div>
|
| 175 |
+
<div class="bot-stats">
|
| 176 |
+
<div class="stat-item">
|
| 177 |
+
<div class="stat-label">CPU</div>
|
| 178 |
+
<div class="stat-value">${bot.cpu || '0'}%</div>
|
| 179 |
+
</div>
|
| 180 |
+
<div class="stat-item">
|
| 181 |
+
<div class="stat-label">Memory</div>
|
| 182 |
+
<div class="stat-value">${bot.memory || '0'} MB</div>
|
| 183 |
+
</div>
|
| 184 |
+
<div class="stat-item">
|
| 185 |
+
<div class="stat-label">Uptime</div>
|
| 186 |
+
<div class="stat-value">${uptime}</div>
|
| 187 |
+
</div>
|
| 188 |
+
<div class="stat-item">
|
| 189 |
+
<div class="stat-label">Restarts</div>
|
| 190 |
+
<div class="stat-value">${bot.restartCount || 0}</div>
|
| 191 |
+
</div>
|
| 192 |
+
</div>
|
| 193 |
+
<div class="bot-actions">
|
| 194 |
+
${isOnline ? `
|
| 195 |
+
<button class="btn btn-small btn-danger" onclick="stopBot('${escapeHtml(bot.name)}')">Stop</button>
|
| 196 |
+
<button class="btn btn-small" onclick="restartBot('${escapeHtml(bot.name)}')">Restart</button>
|
| 197 |
+
` : `
|
| 198 |
+
<button class="btn btn-small btn-success" onclick="startBot('${escapeHtml(bot.name)}')">Start</button>
|
| 199 |
+
`}
|
| 200 |
+
<button class="btn btn-small" onclick="showConsole('${escapeHtml(bot.name)}')">Console</button>
|
| 201 |
+
<button class="btn btn-small" onclick="viewFiles('${escapeHtml(bot.name)}')">Files</button>
|
| 202 |
+
<button class="btn btn-small btn-danger" onclick="deleteBot('${escapeHtml(bot.name)}')">Delete</button>
|
| 203 |
+
</div>
|
| 204 |
+
</div>
|
| 205 |
+
`;
|
| 206 |
+
}
|
| 207 |
+
|
| 208 |
+
async function startBot(name) {
|
| 209 |
+
try {
|
| 210 |
+
const response = await fetch(`/api/bots/${name}/start`, { method: 'POST' });
|
| 211 |
+
const data = await response.json();
|
| 212 |
+
|
| 213 |
+
if (response.ok) {
|
| 214 |
+
loadBots();
|
| 215 |
+
} else {
|
| 216 |
+
alert('Error: ' + data.error);
|
| 217 |
+
}
|
| 218 |
+
} catch (error) {
|
| 219 |
+
alert('Error starting bot');
|
| 220 |
+
}
|
| 221 |
+
}
|
| 222 |
+
|
| 223 |
+
async function stopBot(name) {
|
| 224 |
+
if (!confirm(`Are you sure you want to stop "${name}"?`)) return;
|
| 225 |
+
|
| 226 |
+
try {
|
| 227 |
+
const response = await fetch(`/api/bots/${name}/stop`, { method: 'POST' });
|
| 228 |
+
const data = await response.json();
|
| 229 |
+
|
| 230 |
+
if (response.ok) {
|
| 231 |
+
loadBots();
|
| 232 |
+
} else {
|
| 233 |
+
alert('Error: ' + data.error);
|
| 234 |
+
}
|
| 235 |
+
} catch (error) {
|
| 236 |
+
alert('Error stopping bot');
|
| 237 |
+
}
|
| 238 |
+
}
|
| 239 |
+
|
| 240 |
+
async function restartBot(name) {
|
| 241 |
+
try {
|
| 242 |
+
const response = await fetch(`/api/bots/${name}/restart`, { method: 'POST' });
|
| 243 |
+
const data = await response.json();
|
| 244 |
+
|
| 245 |
+
if (response.ok) {
|
| 246 |
+
loadBots();
|
| 247 |
+
} else {
|
| 248 |
+
alert('Error: ' + data.error);
|
| 249 |
+
}
|
| 250 |
+
} catch (error) {
|
| 251 |
+
alert('Error restarting bot');
|
| 252 |
+
}
|
| 253 |
+
}
|
| 254 |
+
|
| 255 |
+
async function deleteBot(name) {
|
| 256 |
+
if (!confirm(`Are you sure you want to delete "${name}"? This cannot be undone!`)) return;
|
| 257 |
+
|
| 258 |
+
try {
|
| 259 |
+
const response = await fetch(`/api/bots/${name}`, { method: 'DELETE' });
|
| 260 |
+
const data = await response.json();
|
| 261 |
+
|
| 262 |
+
if (response.ok) {
|
| 263 |
+
loadBots();
|
| 264 |
+
} else {
|
| 265 |
+
alert('Error: ' + data.error);
|
| 266 |
+
}
|
| 267 |
+
} catch (error) {
|
| 268 |
+
alert('Error deleting bot');
|
| 269 |
+
}
|
| 270 |
+
}
|
| 271 |
+
|
| 272 |
+
// ============================================
|
| 273 |
+
// Console Functions
|
| 274 |
+
// ============================================
|
| 275 |
+
function showConsole(botName) {
|
| 276 |
+
currentBot = botName;
|
| 277 |
+
|
| 278 |
+
const modal = document.getElementById('consoleModal');
|
| 279 |
+
const title = document.getElementById('consoleTitle');
|
| 280 |
+
|
| 281 |
+
if (!modal || !title) return;
|
| 282 |
+
|
| 283 |
+
title.textContent = `Console - ${botName}`;
|
| 284 |
+
modal.classList.add('active');
|
| 285 |
+
|
| 286 |
+
// Subscribe to logs via socket
|
| 287 |
+
if (socket) {
|
| 288 |
+
socket.emit('subscribe-logs', botName);
|
| 289 |
+
}
|
| 290 |
+
}
|
| 291 |
+
|
| 292 |
+
function closeConsoleModal() {
|
| 293 |
+
const modal = document.getElementById('consoleModal');
|
| 294 |
+
if (modal) {
|
| 295 |
+
modal.classList.remove('active');
|
| 296 |
+
}
|
| 297 |
+
|
| 298 |
+
if (socket) {
|
| 299 |
+
socket.emit('unsubscribe-logs');
|
| 300 |
+
}
|
| 301 |
+
|
| 302 |
+
currentBot = null;
|
| 303 |
+
}
|
| 304 |
+
|
| 305 |
+
function clearConsole() {
|
| 306 |
+
const output = document.getElementById('consoleOutput');
|
| 307 |
+
if (output) {
|
| 308 |
+
output.textContent = '';
|
| 309 |
+
}
|
| 310 |
+
}
|
| 311 |
+
|
| 312 |
+
// ============================================
|
| 313 |
+
// Upload Modal
|
| 314 |
+
// ============================================
|
| 315 |
+
function initializeUploadModal() {
|
| 316 |
+
const form = document.getElementById('uploadForm');
|
| 317 |
+
if (!form) return;
|
| 318 |
+
|
| 319 |
+
form.addEventListener('submit', async (e) => {
|
| 320 |
+
e.preventDefault();
|
| 321 |
+
|
| 322 |
+
const botName = document.getElementById('botName').value;
|
| 323 |
+
const fileInput = document.getElementById('botFile');
|
| 324 |
+
const file = fileInput.files[0];
|
| 325 |
+
const progressDiv = document.getElementById('uploadProgress');
|
| 326 |
+
|
| 327 |
+
if (!file) {
|
| 328 |
+
alert('Please select a ZIP file');
|
| 329 |
+
return;
|
| 330 |
+
}
|
| 331 |
+
|
| 332 |
+
if (!botName) {
|
| 333 |
+
alert('Please enter a bot name');
|
| 334 |
+
return;
|
| 335 |
+
}
|
| 336 |
+
|
| 337 |
+
// Show progress
|
| 338 |
+
progressDiv.style.display = 'block';
|
| 339 |
+
|
| 340 |
+
const formData = new FormData();
|
| 341 |
+
formData.append('name', botName);
|
| 342 |
+
formData.append('bot', file);
|
| 343 |
+
|
| 344 |
+
try {
|
| 345 |
+
const response = await fetch('/api/bots/upload', {
|
| 346 |
+
method: 'POST',
|
| 347 |
+
body: formData
|
| 348 |
+
});
|
| 349 |
+
|
| 350 |
+
const data = await response.json();
|
| 351 |
+
|
| 352 |
+
if (response.ok) {
|
| 353 |
+
closeUploadModal();
|
| 354 |
+
loadBots();
|
| 355 |
+
alert('Bot uploaded successfully!');
|
| 356 |
+
} else {
|
| 357 |
+
alert('Error: ' + data.error);
|
| 358 |
+
}
|
| 359 |
+
} catch (error) {
|
| 360 |
+
alert('Error uploading bot');
|
| 361 |
+
} finally {
|
| 362 |
+
progressDiv.style.display = 'none';
|
| 363 |
+
}
|
| 364 |
+
});
|
| 365 |
+
|
| 366 |
+
// Drag and drop
|
| 367 |
+
const dropZone = document.getElementById('dropZone');
|
| 368 |
+
if (dropZone) {
|
| 369 |
+
dropZone.addEventListener('dragover', (e) => {
|
| 370 |
+
e.preventDefault();
|
| 371 |
+
dropZone.style.borderColor = 'var(--accent-primary)';
|
| 372 |
+
});
|
| 373 |
+
|
| 374 |
+
dropZone.addEventListener('dragleave', () => {
|
| 375 |
+
dropZone.style.borderColor = 'var(--border-color)';
|
| 376 |
+
});
|
| 377 |
+
|
| 378 |
+
dropZone.addEventListener('drop', (e) => {
|
| 379 |
+
e.preventDefault();
|
| 380 |
+
dropZone.style.borderColor = 'var(--border-color)';
|
| 381 |
+
|
| 382 |
+
const files = e.dataTransfer.files;
|
| 383 |
+
if (files.length > 0) {
|
| 384 |
+
document.getElementById('botFile').files = files;
|
| 385 |
+
}
|
| 386 |
+
});
|
| 387 |
+
}
|
| 388 |
+
}
|
| 389 |
+
|
| 390 |
+
function showUploadModal() {
|
| 391 |
+
const modal = document.getElementById('uploadModal');
|
| 392 |
+
if (modal) {
|
| 393 |
+
modal.classList.add('active');
|
| 394 |
+
document.getElementById('uploadForm').reset();
|
| 395 |
+
document.getElementById('uploadProgress').style.display = 'none';
|
| 396 |
+
}
|
| 397 |
+
}
|
| 398 |
+
|
| 399 |
+
function closeUploadModal() {
|
| 400 |
+
const modal = document.getElementById('uploadModal');
|
| 401 |
+
if (modal) {
|
| 402 |
+
modal.classList.remove('active');
|
| 403 |
+
}
|
| 404 |
+
}
|
| 405 |
+
|
| 406 |
+
// ============================================
|
| 407 |
+
// File Manager
|
| 408 |
+
// ============================================
|
| 409 |
+
function initializeFileManager() {
|
| 410 |
+
loadBotList();
|
| 411 |
+
}
|
| 412 |
+
|
| 413 |
+
async function loadBotList() {
|
| 414 |
+
const botList = document.getElementById('botList');
|
| 415 |
+
if (!botList) return;
|
| 416 |
+
|
| 417 |
+
try {
|
| 418 |
+
const response = await fetch('/api/bots');
|
| 419 |
+
const data = await response.json();
|
| 420 |
+
|
| 421 |
+
if (data.bots && data.bots.length > 0) {
|
| 422 |
+
botList.innerHTML = data.bots.map(bot =>
|
| 423 |
+
`<li onclick="selectBot('${escapeHtml(bot.name)}')" data-bot="${escapeHtml(bot.name)}">${escapeHtml(bot.name)}</li>`
|
| 424 |
+
).join('');
|
| 425 |
+
} else {
|
| 426 |
+
botList.innerHTML = '<li>No bots found</li>';
|
| 427 |
+
}
|
| 428 |
+
} catch (error) {
|
| 429 |
+
botList.innerHTML = '<li>Error loading bots</li>';
|
| 430 |
+
}
|
| 431 |
+
}
|
| 432 |
+
|
| 433 |
+
function viewFiles(botName) {
|
| 434 |
+
window.location.href = `/filemanager?bot=${botName}`;
|
| 435 |
+
}
|
| 436 |
+
|
| 437 |
+
function selectBot(botName) {
|
| 438 |
+
// Highlight selected bot
|
| 439 |
+
document.querySelectorAll('.bot-file-list li').forEach(li => li.classList.remove('active'));
|
| 440 |
+
const selectedLi = document.querySelector(`.bot-file-list li[data-bot="${botName}"]`);
|
| 441 |
+
if (selectedLi) selectedLi.classList.add('active');
|
| 442 |
+
|
| 443 |
+
loadFiles(botName, '');
|
| 444 |
+
}
|
| 445 |
+
|
| 446 |
+
async function loadFiles(botName, dirPath = '') {
|
| 447 |
+
const fileList = document.getElementById('fileList');
|
| 448 |
+
const breadcrumb = document.getElementById('breadcrumb');
|
| 449 |
+
|
| 450 |
+
if (!fileList) return;
|
| 451 |
+
|
| 452 |
+
try {
|
| 453 |
+
const url = dirPath
|
| 454 |
+
? `/api/bots/${botName}/files/read?path=${encodeURIComponent(dirPath)}`
|
| 455 |
+
: `/api/bots/${botName}/files`;
|
| 456 |
+
|
| 457 |
+
const response = await fetch(url);
|
| 458 |
+
const data = await response.json();
|
| 459 |
+
|
| 460 |
+
if (data.files) {
|
| 461 |
+
// Update breadcrumb
|
| 462 |
+
if (breadcrumb) {
|
| 463 |
+
const parts = dirPath ? dirPath.split('/') : [];
|
| 464 |
+
breadcrumb.innerHTML = `<span onclick="loadFiles('${botName}', '')" style="cursor:pointer; color: var(--accent-secondary);">/</span>` +
|
| 465 |
+
parts.map((part, i) => {
|
| 466 |
+
const path = parts.slice(0, i + 1).join('/');
|
| 467 |
+
return `<span onclick="loadFiles('${botName}', '${path}')" style="cursor:pointer; color: var(--accent-secondary);">${part}/</span>`;
|
| 468 |
+
}).join('');
|
| 469 |
+
}
|
| 470 |
+
|
| 471 |
+
fileList.innerHTML = data.files.map(file => createFileItem(botName, file, dirPath)).join('');
|
| 472 |
+
|
| 473 |
+
if (data.files.length === 0) {
|
| 474 |
+
fileList.innerHTML = '<p style="padding: 20px; color: var(--text-muted);">Empty directory</p>';
|
| 475 |
+
}
|
| 476 |
+
}
|
| 477 |
+
} catch (error) {
|
| 478 |
+
fileList.innerHTML = '<p style="padding: 20px; color: var(--danger);">Error loading files</p>';
|
| 479 |
+
}
|
| 480 |
+
}
|
| 481 |
+
|
| 482 |
+
function createFileItem(botName, file, currentPath) {
|
| 483 |
+
const icon = file.type === 'directory' ? '๐' : '๐';
|
| 484 |
+
const size = file.type === 'directory' ? '' : formatFileSize(file.size);
|
| 485 |
+
const fullPath = currentPath ? `${currentPath}/${file.name}` : file.name;
|
| 486 |
+
|
| 487 |
+
return `
|
| 488 |
+
<div class="file-item" onclick="${file.type === 'directory' ? `loadFiles('${botName}', '${fullPath}')` : `openFile('${botName}', '${fullPath}')`}">
|
| 489 |
+
<div class="file-item-icon">${icon}</div>
|
| 490 |
+
<div class="file-item-info">
|
| 491 |
+
<div class="file-item-name">${escapeHtml(file.name)}</div>
|
| 492 |
+
<div class="file-item-meta">${size} โข ${new Date(file.modifiedAt).toLocaleString()}</div>
|
| 493 |
+
</div>
|
| 494 |
+
</div>
|
| 495 |
+
`;
|
| 496 |
+
}
|
| 497 |
+
|
| 498 |
+
async function openFile(botName, filePath) {
|
| 499 |
+
try {
|
| 500 |
+
const response = await fetch(`/api/bots/${botName}/files/read?path=${encodeURIComponent(filePath)}`);
|
| 501 |
+
const data = await response.json();
|
| 502 |
+
|
| 503 |
+
if (data.content) {
|
| 504 |
+
// For simplicity, show in an alert - in production you'd use a proper editor
|
| 505 |
+
const editorContent = data.content.substring(0, 5000);
|
| 506 |
+
const newContent = prompt(`Editing: ${filePath}`, editorContent);
|
| 507 |
+
|
| 508 |
+
if (newContent !== null && newContent !== editorContent) {
|
| 509 |
+
await saveFile(botName, filePath, newContent);
|
| 510 |
+
}
|
| 511 |
+
}
|
| 512 |
+
} catch (error) {
|
| 513 |
+
alert('Error opening file');
|
| 514 |
+
}
|
| 515 |
+
}
|
| 516 |
+
|
| 517 |
+
async function saveFile(botName, filePath, content) {
|
| 518 |
+
try {
|
| 519 |
+
const response = await fetch(`/api/bots/${botName}/files/save`, {
|
| 520 |
+
method: 'POST',
|
| 521 |
+
headers: { 'Content-Type': 'application/json' },
|
| 522 |
+
body: JSON.stringify({ path: filePath, content })
|
| 523 |
+
});
|
| 524 |
+
|
| 525 |
+
const data = await response.json();
|
| 526 |
+
if (data.success) {
|
| 527 |
+
alert('File saved successfully');
|
| 528 |
+
} else {
|
| 529 |
+
alert('Error saving file');
|
| 530 |
+
}
|
| 531 |
+
} catch (error) {
|
| 532 |
+
alert('Error saving file');
|
| 533 |
+
}
|
| 534 |
+
}
|
| 535 |
+
|
| 536 |
+
function uploadFile() {
|
| 537 |
+
document.getElementById('fileUploadInput').click();
|
| 538 |
+
}
|
| 539 |
+
|
| 540 |
+
function createDirectory() {
|
| 541 |
+
const dirName = prompt('Enter directory name:');
|
| 542 |
+
if (dirName && currentBot) {
|
| 543 |
+
// Implementation for creating directory
|
| 544 |
+
fetch(`/api/bots/${currentBot}/files/mkdir`, {
|
| 545 |
+
method: 'POST',
|
| 546 |
+
headers: { 'Content-Type': 'application/json' },
|
| 547 |
+
body: JSON.stringify({ name: dirName, path: '' })
|
| 548 |
+
}).then(() => loadFiles(currentBot));
|
| 549 |
+
}
|
| 550 |
+
}
|
| 551 |
+
|
| 552 |
+
// ============================================
|
| 553 |
+
// Utility Functions
|
| 554 |
+
// ============================================
|
| 555 |
+
function escapeHtml(text) {
|
| 556 |
+
const div = document.createElement('div');
|
| 557 |
+
div.textContent = text;
|
| 558 |
+
return div.innerHTML;
|
| 559 |
+
}
|
| 560 |
+
|
| 561 |
+
function formatUptime(ms) {
|
| 562 |
+
const seconds = Math.floor(ms / 1000);
|
| 563 |
+
const minutes = Math.floor(seconds / 60);
|
| 564 |
+
const hours = Math.floor(minutes / 60);
|
| 565 |
+
const days = Math.floor(hours / 24);
|
| 566 |
+
|
| 567 |
+
if (days > 0) return `${days}d ${hours % 24}h`;
|
| 568 |
+
if (hours > 0) return `${hours}h ${minutes % 60}m`;
|
| 569 |
+
if (minutes > 0) return `${minutes}m ${seconds % 60}s`;
|
| 570 |
+
return `${seconds}s`;
|
| 571 |
+
}
|
| 572 |
+
|
| 573 |
+
function formatFileSize(bytes) {
|
| 574 |
+
if (bytes === 0) return '0 B';
|
| 575 |
+
const k = 1024;
|
| 576 |
+
const sizes = ['B', 'KB', 'MB', 'GB'];
|
| 577 |
+
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
| 578 |
+
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
|
| 579 |
+
}
|
| 580 |
+
|
| 581 |
+
// Close modals on escape key
|
| 582 |
+
document.addEventListener('keydown', (e) => {
|
| 583 |
+
if (e.key === 'Escape') {
|
| 584 |
+
closeUploadModal();
|
| 585 |
+
closeConsoleModal();
|
| 586 |
+
}
|
| 587 |
+
});
|
| 588 |
+
|
| 589 |
+
// Close modals on outside click
|
| 590 |
+
document.addEventListener('click', (e) => {
|
| 591 |
+
if (e.target.classList.contains('modal')) {
|
| 592 |
+
closeUploadModal();
|
| 593 |
+
closeConsoleModal();
|
| 594 |
+
}
|
| 595 |
+
});
|