modified: app.py

modified:   templates/itemeditor_command_storage.html
This commit is contained in:
SimolZimol
2026-01-07 04:15:55 +01:00
parent e90d28cc10
commit 26a7169515
2 changed files with 19 additions and 14 deletions

10
app.py
View File

@@ -2,7 +2,7 @@ import os
import json import json
import uuid import uuid
from pathlib import Path from pathlib import Path
from datetime import datetime, timedelta from datetime import datetime, timedelta, timezone
from threading import Lock from threading import Lock
from flask import Flask, render_template, jsonify, send_from_directory, request from flask import Flask, render_template, jsonify, send_from_directory, request
@@ -15,7 +15,7 @@ storage_lock = Lock()
def cleanup_expired_commands(): def cleanup_expired_commands():
"""Remove expired commands from storage""" """Remove expired commands from storage"""
with storage_lock: with storage_lock:
now = datetime.now() now = datetime.now(timezone.utc)
expired_keys = [key for key, value in command_storage.items() expired_keys = [key for key, value in command_storage.items()
if datetime.fromisoformat(value['expires_at']) < now] if datetime.fromisoformat(value['expires_at']) < now]
for key in expired_keys: for key in expired_keys:
@@ -268,8 +268,8 @@ def store_command():
# Generate UUID # Generate UUID
command_uuid = str(uuid.uuid4()) command_uuid = str(uuid.uuid4())
# Calculate expiry (30 minutes) # Calculate expiry (30 minutes) - use UTC
created_at = datetime.now() created_at = datetime.now(timezone.utc)
expires_at = created_at + timedelta(minutes=30) expires_at = created_at + timedelta(minutes=30)
# Store command # Store command
@@ -307,7 +307,7 @@ def retrieve_command(command_uuid):
return jsonify({'error': 'Command not found or expired'}), 404 return jsonify({'error': 'Command not found or expired'}), 404
# Check if expired # Check if expired
if datetime.fromisoformat(command_data['expires_at']) < datetime.now(): if datetime.fromisoformat(command_data['expires_at']) < datetime.now(timezone.utc):
with storage_lock: with storage_lock:
del command_storage[command_uuid] del command_storage[command_uuid]
return jsonify({'error': 'Command expired'}), 410 return jsonify({'error': 'Command expired'}), 410

View File

@@ -547,25 +547,30 @@ function startCountdown(expiresAt) {
const expiryTime = document.getElementById('expiryTime'); const expiryTime = document.getElementById('expiryTime');
const endTime = new Date(expiresAt).getTime(); const endTime = new Date(expiresAt).getTime();
// If expiry is in the past, show expired immediately // Update countdown immediately
if (endTime < new Date().getTime()) { function updateCountdown() {
expiryTime.innerHTML = '<span style="color: #ff5555;">Expired</span>';
return;
}
const interval = setInterval(() => {
const now = new Date().getTime(); const now = new Date().getTime();
const distance = endTime - now; const distance = endTime - now;
if (distance < 0) { if (distance < 0) {
clearInterval(interval);
expiryTime.innerHTML = '<span style="color: #ff5555;">Expired</span>'; expiryTime.innerHTML = '<span style="color: #ff5555;">Expired</span>';
return; return false;
} }
const minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60)); const minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
const seconds = Math.floor((distance % (1000 * 60)) / 1000); const seconds = Math.floor((distance % (1000 * 60)) / 1000);
expiryTime.textContent = `${minutes}m ${seconds}s`; expiryTime.textContent = `${minutes}m ${seconds}s`;
return true;
}
// Update immediately
if (!updateCountdown()) return;
// Then update every second
const interval = setInterval(() => {
if (!updateCountdown()) {
clearInterval(interval);
}
}, 1000); }, 1000);
} }
</script> </script>