Initial commit: enterprise digital platform with portal SSO, DigiServer, IT Assets, NetworkView, Server Monitor
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
"""Utils package for digiserver-v2."""
|
||||
from app.utils.logger import log_action, log_info, log_warning, log_error, get_recent_logs
|
||||
from app.utils.uploads import (
|
||||
save_uploaded_file,
|
||||
process_video_file,
|
||||
process_pdf_file,
|
||||
get_upload_progress,
|
||||
set_upload_progress,
|
||||
clear_upload_progress,
|
||||
get_file_size,
|
||||
delete_file
|
||||
)
|
||||
from app.utils.group_player_management import (
|
||||
get_player_status_info,
|
||||
get_group_statistics,
|
||||
assign_player_to_group,
|
||||
bulk_assign_players_to_group,
|
||||
get_online_players_count,
|
||||
get_players_by_status
|
||||
)
|
||||
from app.utils.pptx_converter import pptx_to_pdf_libreoffice, validate_pptx_file
|
||||
|
||||
__all__ = [
|
||||
# Logger
|
||||
'log_action',
|
||||
'log_info',
|
||||
'log_warning',
|
||||
'log_error',
|
||||
'get_recent_logs',
|
||||
# Uploads
|
||||
'save_uploaded_file',
|
||||
'process_video_file',
|
||||
'process_pdf_file',
|
||||
'get_upload_progress',
|
||||
'set_upload_progress',
|
||||
'clear_upload_progress',
|
||||
'get_file_size',
|
||||
'delete_file',
|
||||
# Group/Player Management
|
||||
'get_player_status_info',
|
||||
'get_group_statistics',
|
||||
'assign_player_to_group',
|
||||
'bulk_assign_players_to_group',
|
||||
'get_online_players_count',
|
||||
'get_players_by_status',
|
||||
# PPTX Converter
|
||||
'pptx_to_pdf_libreoffice',
|
||||
'validate_pptx_file',
|
||||
]
|
||||
@@ -0,0 +1,154 @@
|
||||
"""Caddy configuration generator and manager."""
|
||||
import os
|
||||
from typing import Optional
|
||||
from app.models.https_config import HTTPSConfig
|
||||
|
||||
|
||||
class CaddyConfigGenerator:
|
||||
"""Generate Caddyfile configuration based on HTTPSConfig."""
|
||||
|
||||
@staticmethod
|
||||
def generate_caddyfile(config: Optional[HTTPSConfig] = None) -> str:
|
||||
"""Generate complete Caddyfile content.
|
||||
|
||||
Args:
|
||||
config: HTTPSConfig instance or None
|
||||
|
||||
Returns:
|
||||
Complete Caddyfile content as string
|
||||
"""
|
||||
# Get config from database if not provided
|
||||
if config is None:
|
||||
config = HTTPSConfig.get_config()
|
||||
|
||||
# Base configuration
|
||||
email = "admin@localhost"
|
||||
if config and config.email:
|
||||
email = config.email
|
||||
|
||||
base_config = f"""{{
|
||||
# Global options
|
||||
email {email}
|
||||
# Admin API for configuration management (listen on all interfaces)
|
||||
admin 0.0.0.0:2019
|
||||
# Uncomment for testing to avoid rate limits
|
||||
# acme_ca https://acme-staging-v02.api.letsencrypt.org/directory
|
||||
}}
|
||||
|
||||
# Shared reverse proxy configuration
|
||||
(reverse_proxy_config) {{
|
||||
reverse_proxy digiserver-app:5000 {{
|
||||
header_up Host {{host}}
|
||||
header_up X-Real-IP {{remote_host}}
|
||||
header_up X-Forwarded-Proto {{scheme}}
|
||||
|
||||
# Timeouts for large uploads
|
||||
transport http {{
|
||||
read_timeout 300s
|
||||
write_timeout 300s
|
||||
}}
|
||||
}}
|
||||
|
||||
# File upload size limit (2GB)
|
||||
request_body {{
|
||||
max_size 2GB
|
||||
}}
|
||||
|
||||
# Security headers
|
||||
header {{
|
||||
X-Frame-Options "SAMEORIGIN"
|
||||
X-Content-Type-Options "nosniff"
|
||||
X-XSS-Protection "1; mode=block"
|
||||
}}
|
||||
|
||||
# Logging
|
||||
log {{
|
||||
output file /var/log/caddy/access.log
|
||||
}}
|
||||
}}
|
||||
|
||||
# Localhost (development/local access)
|
||||
http://localhost {{
|
||||
import reverse_proxy_config
|
||||
}}
|
||||
"""
|
||||
|
||||
# Add main domain/IP configuration if HTTPS is enabled
|
||||
if config and config.https_enabled and config.domain and config.ip_address:
|
||||
# Internal domain configuration
|
||||
domain_config = f"""
|
||||
# Internal domain (HTTP only - internal use)
|
||||
http://{config.domain} {{
|
||||
import reverse_proxy_config
|
||||
}}
|
||||
|
||||
# Handle IP address access
|
||||
http://{config.ip_address} {{
|
||||
import reverse_proxy_config
|
||||
}}
|
||||
"""
|
||||
base_config += domain_config
|
||||
else:
|
||||
# Default fallback configuration
|
||||
base_config += """
|
||||
# Internal domain (HTTP only - internal use)
|
||||
http://digiserver.sibiusb.harting.intra {
|
||||
import reverse_proxy_config
|
||||
}
|
||||
|
||||
# Handle IP address access
|
||||
http://10.76.152.164 {
|
||||
import reverse_proxy_config
|
||||
}
|
||||
"""
|
||||
|
||||
# Add catch-all for any other HTTP requests
|
||||
base_config += """
|
||||
# Catch-all for any other HTTP requests
|
||||
http://* {
|
||||
import reverse_proxy_config
|
||||
}
|
||||
"""
|
||||
|
||||
return base_config
|
||||
|
||||
@staticmethod
|
||||
def write_caddyfile(caddyfile_content: str, path: str = '/app/Caddyfile') -> bool:
|
||||
"""Write Caddyfile to disk.
|
||||
|
||||
Args:
|
||||
caddyfile_content: Content to write
|
||||
path: Path to Caddyfile
|
||||
|
||||
Returns:
|
||||
True if successful, False otherwise
|
||||
"""
|
||||
try:
|
||||
with open(path, 'w') as f:
|
||||
f.write(caddyfile_content)
|
||||
return True
|
||||
except Exception as e:
|
||||
print(f"Error writing Caddyfile: {str(e)}")
|
||||
return False
|
||||
|
||||
@staticmethod
|
||||
def reload_caddy() -> bool:
|
||||
"""Reload Caddy configuration without restart.
|
||||
|
||||
Note: Caddy monitoring is handled via file watching. After writing the Caddyfile,
|
||||
Caddy should automatically reload. If it doesn't, you may need to restart the
|
||||
Caddy container manually.
|
||||
|
||||
Returns:
|
||||
True if configuration was written successfully (Caddy will auto-reload)
|
||||
"""
|
||||
try:
|
||||
# Just verify that Caddy is reachable
|
||||
import urllib.request
|
||||
response = urllib.request.urlopen('http://caddy:2019/config/', timeout=2)
|
||||
return response.status == 200
|
||||
except Exception as e:
|
||||
# Caddy might not be reachable, but Caddyfile was already written
|
||||
# Caddy should reload automatically when it detects file changes
|
||||
print(f"Note: Caddy reload check returned: {str(e)}")
|
||||
return True # Return True anyway since Caddyfile was written
|
||||
@@ -0,0 +1,209 @@
|
||||
"""Group and player management utilities."""
|
||||
from typing import Dict, List, Optional
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from app.extensions import db
|
||||
from app.models import Player, Group, PlayerFeedback
|
||||
from app.utils.logger import log_action
|
||||
|
||||
|
||||
def get_player_status_info(player_id: int) -> Dict:
|
||||
"""Get comprehensive status information for a player.
|
||||
|
||||
Args:
|
||||
player_id: Player ID to query
|
||||
|
||||
Returns:
|
||||
Dictionary with status information
|
||||
"""
|
||||
player = Player.query.get(player_id)
|
||||
|
||||
if not player:
|
||||
return {
|
||||
'online': False,
|
||||
'status': 'unknown',
|
||||
'last_seen': None,
|
||||
'latest_feedback': None
|
||||
}
|
||||
|
||||
# Check if player is online (seen in last 5 minutes)
|
||||
is_online = False
|
||||
if player.last_seen:
|
||||
delta = datetime.utcnow() - player.last_seen
|
||||
is_online = delta.total_seconds() < 300
|
||||
|
||||
# Get latest feedback
|
||||
latest_feedback = PlayerFeedback.query.filter_by(player_id=player_id)\
|
||||
.order_by(PlayerFeedback.timestamp.desc())\
|
||||
.first()
|
||||
|
||||
return {
|
||||
'online': is_online,
|
||||
'status': player.status,
|
||||
'last_seen': player.last_seen.isoformat() if player.last_seen else None,
|
||||
'last_seen_ago': _format_time_ago(player.last_seen) if player.last_seen else 'Never',
|
||||
'latest_feedback': {
|
||||
'status': latest_feedback.status,
|
||||
'message': latest_feedback.message,
|
||||
'error': latest_feedback.error,
|
||||
'timestamp': latest_feedback.timestamp.isoformat()
|
||||
} if latest_feedback else None
|
||||
}
|
||||
|
||||
|
||||
def get_group_statistics(group_id: int) -> Dict:
|
||||
"""Get statistics for a group.
|
||||
|
||||
Args:
|
||||
group_id: Group ID to query
|
||||
|
||||
Returns:
|
||||
Dictionary with group statistics
|
||||
"""
|
||||
group = Group.query.get(group_id)
|
||||
|
||||
if not group:
|
||||
return {
|
||||
'total_players': 0,
|
||||
'online_players': 0,
|
||||
'total_content': 0,
|
||||
'error_count': 0
|
||||
}
|
||||
|
||||
total_players = group.player_count
|
||||
total_content = group.content_count
|
||||
|
||||
# Count online players
|
||||
online_players = 0
|
||||
error_count = 0
|
||||
five_min_ago = datetime.utcnow() - timedelta(minutes=5)
|
||||
|
||||
for player in group.players:
|
||||
if player.last_seen and player.last_seen >= five_min_ago:
|
||||
online_players += 1
|
||||
if player.status == 'error':
|
||||
error_count += 1
|
||||
|
||||
return {
|
||||
'group_id': group_id,
|
||||
'group_name': group.name,
|
||||
'total_players': total_players,
|
||||
'online_players': online_players,
|
||||
'offline_players': total_players - online_players,
|
||||
'total_content': total_content,
|
||||
'error_count': error_count
|
||||
}
|
||||
|
||||
|
||||
def assign_player_to_group(player_id: int, group_id: Optional[int]) -> bool:
|
||||
"""Assign a player to a group or unassign if group_id is None.
|
||||
|
||||
Args:
|
||||
player_id: Player ID to assign
|
||||
group_id: Group ID to assign to, or None to unassign
|
||||
|
||||
Returns:
|
||||
True if successful, False otherwise
|
||||
"""
|
||||
try:
|
||||
player = Player.query.get(player_id)
|
||||
|
||||
if not player:
|
||||
log_action('error', f'Player {player_id} not found')
|
||||
return False
|
||||
|
||||
old_group_id = player.group_id
|
||||
player.group_id = group_id
|
||||
db.session.commit()
|
||||
|
||||
if group_id:
|
||||
group = Group.query.get(group_id)
|
||||
log_action('info', f'Player "{player.name}" assigned to group "{group.name}"')
|
||||
else:
|
||||
log_action('info', f'Player "{player.name}" unassigned from group')
|
||||
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
log_action('error', f'Error assigning player to group: {str(e)}')
|
||||
return False
|
||||
|
||||
|
||||
def bulk_assign_players_to_group(player_ids: List[int], group_id: Optional[int]) -> int:
|
||||
"""Assign multiple players to a group.
|
||||
|
||||
Args:
|
||||
player_ids: List of player IDs to assign
|
||||
group_id: Group ID to assign to, or None to unassign
|
||||
|
||||
Returns:
|
||||
Number of players successfully assigned
|
||||
"""
|
||||
count = 0
|
||||
|
||||
try:
|
||||
for player_id in player_ids:
|
||||
player = Player.query.get(player_id)
|
||||
if player:
|
||||
player.group_id = group_id
|
||||
count += 1
|
||||
|
||||
db.session.commit()
|
||||
|
||||
if group_id:
|
||||
group = Group.query.get(group_id)
|
||||
log_action('info', f'Bulk assigned {count} players to group "{group.name}"')
|
||||
else:
|
||||
log_action('info', f'Bulk unassigned {count} players from groups')
|
||||
|
||||
return count
|
||||
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
log_action('error', f'Error bulk assigning players: {str(e)}')
|
||||
return 0
|
||||
|
||||
|
||||
def get_online_players_count() -> int:
|
||||
"""Get count of online players (seen in last 5 minutes).
|
||||
|
||||
Returns:
|
||||
Number of online players
|
||||
"""
|
||||
five_min_ago = datetime.utcnow() - timedelta(minutes=5)
|
||||
return Player.query.filter(Player.last_seen >= five_min_ago).count()
|
||||
|
||||
|
||||
def get_players_by_status(status: str) -> List[Player]:
|
||||
"""Get all players with a specific status.
|
||||
|
||||
Args:
|
||||
status: Status to filter by
|
||||
|
||||
Returns:
|
||||
List of Player instances
|
||||
"""
|
||||
return Player.query.filter_by(status=status).all()
|
||||
|
||||
|
||||
def _format_time_ago(dt: datetime) -> str:
|
||||
"""Format datetime as 'time ago' string.
|
||||
|
||||
Args:
|
||||
dt: Datetime to format
|
||||
|
||||
Returns:
|
||||
Formatted string like '5 minutes ago'
|
||||
"""
|
||||
delta = datetime.utcnow() - dt
|
||||
seconds = delta.total_seconds()
|
||||
|
||||
if seconds < 60:
|
||||
return f'{int(seconds)} seconds ago'
|
||||
elif seconds < 3600:
|
||||
return f'{int(seconds / 60)} minutes ago'
|
||||
elif seconds < 86400:
|
||||
return f'{int(seconds / 3600)} hours ago'
|
||||
else:
|
||||
return f'{int(seconds / 86400)} days ago'
|
||||
@@ -0,0 +1,78 @@
|
||||
"""Logging utility for tracking system events."""
|
||||
from typing import Optional
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from app.extensions import db
|
||||
from app.models.server_log import ServerLog
|
||||
|
||||
|
||||
def log_action(level: str, message: str) -> None:
|
||||
"""Log an action to the database with specified level.
|
||||
|
||||
Args:
|
||||
level: Log level (info, warning, error)
|
||||
message: Log message content
|
||||
"""
|
||||
try:
|
||||
new_log = ServerLog(level=level, message=message)
|
||||
db.session.add(new_log)
|
||||
db.session.commit()
|
||||
print(f"[{level.upper()}] {message}")
|
||||
except Exception as e:
|
||||
print(f"Error logging action: {e}")
|
||||
db.session.rollback()
|
||||
|
||||
|
||||
def get_recent_logs(limit: int = 20, level: Optional[str] = None) -> list:
|
||||
"""Get the most recent log entries.
|
||||
|
||||
Args:
|
||||
limit: Maximum number of logs to return
|
||||
level: Optional filter by log level
|
||||
|
||||
Returns:
|
||||
List of ServerLog instances
|
||||
"""
|
||||
query = ServerLog.query
|
||||
|
||||
if level:
|
||||
query = query.filter_by(level=level)
|
||||
|
||||
return query.order_by(ServerLog.timestamp.desc()).limit(limit).all()
|
||||
|
||||
|
||||
def clear_old_logs(days: int = 30) -> int:
|
||||
"""Delete logs older than specified days.
|
||||
|
||||
Args:
|
||||
days: Number of days to keep
|
||||
|
||||
Returns:
|
||||
Number of logs deleted
|
||||
"""
|
||||
try:
|
||||
cutoff_date = datetime.utcnow() - timedelta(days=days)
|
||||
deleted = ServerLog.query.filter(ServerLog.timestamp < cutoff_date).delete()
|
||||
db.session.commit()
|
||||
log_action('info', f'Deleted {deleted} old log entries (older than {days} days)')
|
||||
return deleted
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
print(f"Error clearing old logs: {e}")
|
||||
return 0
|
||||
|
||||
|
||||
# Convenience functions for specific log levels
|
||||
def log_info(message: str) -> None:
|
||||
"""Log an info level message."""
|
||||
log_action('info', message)
|
||||
|
||||
|
||||
def log_warning(message: str) -> None:
|
||||
"""Log a warning level message."""
|
||||
log_action('warning', message)
|
||||
|
||||
|
||||
def log_error(message: str) -> None:
|
||||
"""Log an error level message."""
|
||||
log_action('error', message)
|
||||
@@ -0,0 +1,120 @@
|
||||
"""Nginx configuration reader utility."""
|
||||
import os
|
||||
import re
|
||||
from typing import Dict, List, Optional, Any
|
||||
|
||||
|
||||
class NginxConfigReader:
|
||||
"""Read and parse Nginx configuration files."""
|
||||
|
||||
def __init__(self, config_path: str = '/etc/nginx/nginx.conf'):
|
||||
"""Initialize Nginx config reader."""
|
||||
self.config_path = config_path
|
||||
self.config_content = None
|
||||
self.is_available = os.path.exists(config_path)
|
||||
|
||||
if self.is_available:
|
||||
try:
|
||||
with open(config_path, 'r') as f:
|
||||
self.config_content = f.read()
|
||||
except Exception as e:
|
||||
self.is_available = False
|
||||
self.error = str(e)
|
||||
|
||||
def get_status(self) -> Dict[str, Any]:
|
||||
"""Get Nginx configuration status."""
|
||||
if not self.is_available:
|
||||
return {
|
||||
'available': False,
|
||||
'error': 'Nginx configuration not found',
|
||||
'path': self.config_path
|
||||
}
|
||||
|
||||
return {
|
||||
'available': True,
|
||||
'path': self.config_path,
|
||||
'file_exists': os.path.exists(self.config_path),
|
||||
'ssl_enabled': self._check_ssl_enabled(),
|
||||
'http_ports': self._extract_http_ports(),
|
||||
'https_ports': self._extract_https_ports(),
|
||||
'upstream_servers': self._extract_upstream_servers(),
|
||||
'server_names': self._extract_server_names(),
|
||||
'ssl_protocols': self._extract_ssl_protocols(),
|
||||
'client_max_body_size': self._extract_client_max_body_size(),
|
||||
'gzip_enabled': self._check_gzip_enabled(),
|
||||
}
|
||||
|
||||
def _check_ssl_enabled(self) -> bool:
|
||||
"""Check if SSL is enabled."""
|
||||
if not self.config_content:
|
||||
return False
|
||||
return 'ssl_certificate' in self.config_content
|
||||
|
||||
def _extract_http_ports(self) -> List[int]:
|
||||
"""Extract HTTP listening ports."""
|
||||
if not self.config_content:
|
||||
return []
|
||||
pattern = r'listen\s+(\d+)'
|
||||
matches = re.findall(pattern, self.config_content)
|
||||
return sorted(list(set(int(p) for p in matches if int(p) < 1000)))
|
||||
|
||||
def _extract_https_ports(self) -> List[int]:
|
||||
"""Extract HTTPS listening ports."""
|
||||
if not self.config_content:
|
||||
return []
|
||||
pattern = r'listen\s+(\d+).*ssl'
|
||||
matches = re.findall(pattern, self.config_content)
|
||||
return sorted(list(set(int(p) for p in matches)))
|
||||
|
||||
def _extract_upstream_servers(self) -> List[str]:
|
||||
"""Extract upstream servers."""
|
||||
if not self.config_content:
|
||||
return []
|
||||
upstream_match = re.search(r'upstream\s+\w+\s*{([^}]+)}', self.config_content)
|
||||
if upstream_match:
|
||||
upstream_content = upstream_match.group(1)
|
||||
servers = re.findall(r'server\s+([^\s;]+)', upstream_content)
|
||||
return servers
|
||||
return []
|
||||
|
||||
def _extract_server_names(self) -> List[str]:
|
||||
"""Extract server names."""
|
||||
if not self.config_content:
|
||||
return []
|
||||
pattern = r'server_name\s+([^;]+);'
|
||||
matches = re.findall(pattern, self.config_content)
|
||||
result = []
|
||||
for match in matches:
|
||||
names = match.strip().split()
|
||||
result.extend(names)
|
||||
return result
|
||||
|
||||
def _extract_ssl_protocols(self) -> List[str]:
|
||||
"""Extract SSL protocols."""
|
||||
if not self.config_content:
|
||||
return []
|
||||
pattern = r'ssl_protocols\s+([^;]+);'
|
||||
match = re.search(pattern, self.config_content)
|
||||
if match:
|
||||
return match.group(1).strip().split()
|
||||
return []
|
||||
|
||||
def _extract_client_max_body_size(self) -> Optional[str]:
|
||||
"""Extract client max body size."""
|
||||
if not self.config_content:
|
||||
return None
|
||||
pattern = r'client_max_body_size\s+([^;]+);'
|
||||
match = re.search(pattern, self.config_content)
|
||||
return match.group(1).strip() if match else None
|
||||
|
||||
def _check_gzip_enabled(self) -> bool:
|
||||
"""Check if gzip is enabled."""
|
||||
if not self.config_content:
|
||||
return False
|
||||
return bool(re.search(r'gzip\s+on\s*;', self.config_content))
|
||||
|
||||
|
||||
def get_nginx_status() -> Dict[str, Any]:
|
||||
"""Get Nginx configuration status."""
|
||||
reader = NginxConfigReader()
|
||||
return reader.get_status()
|
||||
@@ -0,0 +1,57 @@
|
||||
"""
|
||||
Portal SSO middleware for DigiServer v2.
|
||||
|
||||
When the umbrella nginx verifies the portal JWT it sets two headers:
|
||||
X-Auth-Username — the portal username
|
||||
X-Auth-Role — 'admin' or 'user'
|
||||
|
||||
This before_request handler reads those headers and auto-logs in the
|
||||
corresponding local DigiServer user, creating them on first access if
|
||||
needed. The local session is then maintained normally by Flask-Login.
|
||||
"""
|
||||
import secrets
|
||||
from flask import request
|
||||
from flask_login import login_user, current_user
|
||||
|
||||
|
||||
def init_portal_sso(app):
|
||||
"""Register the SSO before_request handler on the given Flask app."""
|
||||
|
||||
@app.before_request
|
||||
def _portal_sso():
|
||||
if current_user.is_authenticated:
|
||||
return
|
||||
|
||||
username = request.headers.get('X-Auth-Username', '').strip()
|
||||
if not username:
|
||||
return
|
||||
|
||||
role = request.headers.get('X-Auth-Role', 'user').strip()
|
||||
user = _get_or_create_user(username, role)
|
||||
if user:
|
||||
login_user(user, remember=False)
|
||||
|
||||
|
||||
def _get_or_create_user(username, role):
|
||||
from app.models.user import User
|
||||
from app.extensions import db, bcrypt
|
||||
|
||||
try:
|
||||
target_role = 'admin' if role == 'admin' else 'user'
|
||||
user = User.query.filter_by(username=username).first()
|
||||
if not user:
|
||||
hashed_pw = bcrypt.generate_password_hash(secrets.token_hex(32)).decode('utf-8')
|
||||
user = User(
|
||||
username=username,
|
||||
password=hashed_pw,
|
||||
role=target_role,
|
||||
)
|
||||
db.session.add(user)
|
||||
db.session.commit()
|
||||
elif user.role != target_role:
|
||||
# Role changed in portal → sync it here immediately
|
||||
user.role = target_role
|
||||
db.session.commit()
|
||||
return user
|
||||
except Exception:
|
||||
return None
|
||||
@@ -0,0 +1,106 @@
|
||||
"""PowerPoint to PDF converter using LibreOffice."""
|
||||
import os
|
||||
import subprocess
|
||||
import time
|
||||
import logging
|
||||
from typing import Optional
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def cleanup_libreoffice_processes() -> None:
|
||||
"""Clean up any hanging LibreOffice processes."""
|
||||
try:
|
||||
subprocess.run(['pkill', '-f', 'soffice'], capture_output=True, timeout=10)
|
||||
time.sleep(1) # Give processes time to terminate
|
||||
except Exception as e:
|
||||
logger.warning(f"Failed to cleanup LibreOffice processes: {e}")
|
||||
|
||||
|
||||
def pptx_to_pdf_libreoffice(pptx_path: str, output_dir: str) -> Optional[str]:
|
||||
"""Convert PPTX to PDF using LibreOffice for highest quality.
|
||||
|
||||
This function is the core component of the PPTX processing workflow:
|
||||
PPTX → PDF (this function) → JPG images (handled in uploads.py)
|
||||
|
||||
Args:
|
||||
pptx_path: Path to the PPTX file
|
||||
output_dir: Directory to save the PDF
|
||||
|
||||
Returns:
|
||||
Path to the generated PDF file, or None if conversion failed
|
||||
"""
|
||||
try:
|
||||
# Clean up any existing LibreOffice processes
|
||||
cleanup_libreoffice_processes()
|
||||
|
||||
# Ensure output directory exists
|
||||
os.makedirs(output_dir, exist_ok=True)
|
||||
|
||||
# Use LibreOffice to convert PPTX to PDF
|
||||
cmd = [
|
||||
'libreoffice',
|
||||
'--headless',
|
||||
'--convert-to', 'pdf',
|
||||
'--outdir', output_dir,
|
||||
'--invisible',
|
||||
'--nodefault',
|
||||
pptx_path
|
||||
]
|
||||
|
||||
logger.info(f"Converting PPTX to PDF using LibreOffice: {pptx_path}")
|
||||
|
||||
# Increase timeout to 300 seconds (5 minutes) for large presentations
|
||||
result = subprocess.run(cmd, capture_output=True, text=True, timeout=300)
|
||||
|
||||
if result.returncode != 0:
|
||||
logger.error(f"LibreOffice conversion failed: {result.stderr}")
|
||||
logger.error(f"LibreOffice stdout: {result.stdout}")
|
||||
cleanup_libreoffice_processes()
|
||||
return None
|
||||
|
||||
# Find the generated PDF file
|
||||
base_name = os.path.splitext(os.path.basename(pptx_path))[0]
|
||||
pdf_path = os.path.join(output_dir, f"{base_name}.pdf")
|
||||
|
||||
if os.path.exists(pdf_path):
|
||||
logger.info(f"PDF conversion successful: {pdf_path}")
|
||||
cleanup_libreoffice_processes()
|
||||
return pdf_path
|
||||
else:
|
||||
logger.error(f"PDF file not found after conversion: {pdf_path}")
|
||||
cleanup_libreoffice_processes()
|
||||
return None
|
||||
|
||||
except subprocess.TimeoutExpired:
|
||||
logger.error("LibreOffice conversion timed out (300s)")
|
||||
cleanup_libreoffice_processes()
|
||||
return None
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error in PPTX to PDF conversion: {e}")
|
||||
cleanup_libreoffice_processes()
|
||||
return None
|
||||
|
||||
|
||||
def validate_pptx_file(filepath: str) -> bool:
|
||||
"""Validate if file is a valid PowerPoint file.
|
||||
|
||||
Args:
|
||||
filepath: Path to file to validate
|
||||
|
||||
Returns:
|
||||
True if valid PPTX file, False otherwise
|
||||
"""
|
||||
if not os.path.exists(filepath):
|
||||
return False
|
||||
|
||||
# Check file extension
|
||||
if not filepath.lower().endswith(('.ppt', '.pptx')):
|
||||
return False
|
||||
|
||||
# Check file size (must be > 0)
|
||||
if os.path.getsize(filepath) == 0:
|
||||
return False
|
||||
|
||||
return True
|
||||
@@ -0,0 +1,26 @@
|
||||
"""
|
||||
ScriptNameFix WSGI middleware.
|
||||
|
||||
When nginx strips the path prefix before forwarding to a Flask app it also
|
||||
sets the X-Script-Name header (e.g. /digiserver). This middleware reads
|
||||
that header and sets SCRIPT_NAME in the WSGI environ so that Flask's
|
||||
url_for() generates absolute URLs with the correct prefix.
|
||||
|
||||
Usage in the app factory:
|
||||
from app.utils.script_name_fix import ScriptNameFix
|
||||
app.wsgi_app = ScriptNameFix(app.wsgi_app)
|
||||
"""
|
||||
|
||||
|
||||
class ScriptNameFix:
|
||||
def __init__(self, app):
|
||||
self.app = app
|
||||
|
||||
def __call__(self, environ, start_response):
|
||||
script_name = environ.get('HTTP_X_SCRIPT_NAME', '').rstrip('/')
|
||||
if script_name:
|
||||
environ['SCRIPT_NAME'] = script_name
|
||||
path_info = environ.get('PATH_INFO', '/')
|
||||
if path_info.startswith(script_name):
|
||||
environ['PATH_INFO'] = path_info[len(script_name):] or '/'
|
||||
return self.app(environ, start_response)
|
||||
@@ -0,0 +1,243 @@
|
||||
"""Upload utilities for handling file uploads and processing."""
|
||||
import os
|
||||
import subprocess
|
||||
import shutil
|
||||
import tempfile
|
||||
from typing import Optional, Dict, Tuple
|
||||
from werkzeug.utils import secure_filename
|
||||
from werkzeug.datastructures import FileStorage
|
||||
|
||||
from app.utils.logger import log_action
|
||||
|
||||
|
||||
# In-memory storage for upload progress (use Redis in production)
|
||||
_upload_progress: Dict[str, Dict] = {}
|
||||
|
||||
|
||||
def get_upload_progress(upload_id: str) -> Dict:
|
||||
"""Get upload progress for a specific upload ID.
|
||||
|
||||
Args:
|
||||
upload_id: Unique upload identifier
|
||||
|
||||
Returns:
|
||||
Progress dictionary with status, progress, and message
|
||||
"""
|
||||
return _upload_progress.get(upload_id, {
|
||||
'status': 'unknown',
|
||||
'progress': 0,
|
||||
'message': 'Upload not found'
|
||||
})
|
||||
|
||||
|
||||
def set_upload_progress(upload_id: str, progress: int, message: str,
|
||||
status: str = 'processing') -> None:
|
||||
"""Set upload progress for a specific upload ID.
|
||||
|
||||
Args:
|
||||
upload_id: Unique upload identifier
|
||||
progress: Progress percentage (0-100)
|
||||
message: Status message
|
||||
status: Status string (uploading, processing, complete, error)
|
||||
"""
|
||||
_upload_progress[upload_id] = {
|
||||
'status': status,
|
||||
'progress': progress,
|
||||
'message': message
|
||||
}
|
||||
|
||||
|
||||
def clear_upload_progress(upload_id: str) -> None:
|
||||
"""Clear upload progress for a specific upload ID.
|
||||
|
||||
Args:
|
||||
upload_id: Unique upload identifier
|
||||
"""
|
||||
if upload_id in _upload_progress:
|
||||
del _upload_progress[upload_id]
|
||||
|
||||
|
||||
def save_uploaded_file(file: FileStorage, upload_folder: str,
|
||||
filename: Optional[str] = None) -> Tuple[bool, str, str]:
|
||||
"""Save an uploaded file to the upload folder.
|
||||
|
||||
Args:
|
||||
file: FileStorage object from request.files
|
||||
upload_folder: Path to upload directory
|
||||
filename: Optional custom filename (will be secured)
|
||||
|
||||
Returns:
|
||||
Tuple of (success, filepath, message)
|
||||
"""
|
||||
try:
|
||||
if not filename:
|
||||
filename = secure_filename(file.filename)
|
||||
else:
|
||||
filename = secure_filename(filename)
|
||||
|
||||
# Ensure upload folder exists
|
||||
os.makedirs(upload_folder, exist_ok=True)
|
||||
|
||||
filepath = os.path.join(upload_folder, filename)
|
||||
|
||||
# Save file
|
||||
file.save(filepath)
|
||||
|
||||
log_action('info', f'File saved: {filename}')
|
||||
return True, filepath, 'File saved successfully'
|
||||
|
||||
except Exception as e:
|
||||
log_action('error', f'Error saving file: {str(e)}')
|
||||
return False, '', f'Error saving file: {str(e)}'
|
||||
|
||||
|
||||
def process_video_file(filepath: str, upload_id: Optional[str] = None) -> Tuple[bool, str]:
|
||||
"""Process video file for optimization (H.264, 30fps, max 1080p).
|
||||
|
||||
Args:
|
||||
filepath: Path to video file
|
||||
upload_id: Optional upload ID for progress tracking
|
||||
|
||||
Returns:
|
||||
Tuple of (success, message)
|
||||
"""
|
||||
try:
|
||||
if upload_id:
|
||||
set_upload_progress(upload_id, 60, 'Converting video to optimized format...')
|
||||
|
||||
# Prepare temp output file
|
||||
temp_dir = tempfile.gettempdir()
|
||||
temp_output = os.path.join(temp_dir, f"optimized_{os.path.basename(filepath)}")
|
||||
|
||||
# ffmpeg command for Raspberry Pi optimization
|
||||
ffmpeg_cmd = [
|
||||
'ffmpeg', '-y', '-i', filepath,
|
||||
'-c:v', 'libx264',
|
||||
'-preset', 'medium',
|
||||
'-profile:v', 'main',
|
||||
'-crf', '23',
|
||||
'-maxrate', '8M',
|
||||
'-bufsize', '12M',
|
||||
'-vf', 'scale=\'min(1920,iw)\':\'min(1080,ih)\':force_original_aspect_ratio=decrease,fps=30',
|
||||
'-r', '30',
|
||||
'-c:a', 'aac',
|
||||
'-b:a', '128k',
|
||||
'-movflags', '+faststart',
|
||||
temp_output
|
||||
]
|
||||
|
||||
if upload_id:
|
||||
set_upload_progress(upload_id, 70, 'Processing video (this may take a few minutes)...')
|
||||
|
||||
# Run ffmpeg
|
||||
result = subprocess.run(ffmpeg_cmd, capture_output=True, text=True, timeout=1800)
|
||||
|
||||
if result.returncode != 0:
|
||||
error_msg = f'Video conversion failed: {result.stderr[:200]}'
|
||||
log_action('error', error_msg)
|
||||
|
||||
if upload_id:
|
||||
set_upload_progress(upload_id, 0, error_msg, 'error')
|
||||
|
||||
# Remove original file if conversion failed
|
||||
if os.path.exists(filepath):
|
||||
os.remove(filepath)
|
||||
|
||||
return False, error_msg
|
||||
|
||||
if upload_id:
|
||||
set_upload_progress(upload_id, 85, 'Replacing with optimized video...')
|
||||
|
||||
# Replace original with optimized version
|
||||
shutil.move(temp_output, filepath)
|
||||
|
||||
log_action('info', f'Video optimized successfully: {os.path.basename(filepath)}')
|
||||
return True, 'Video optimized successfully'
|
||||
|
||||
except subprocess.TimeoutExpired:
|
||||
error_msg = 'Video conversion timed out (30 minutes)'
|
||||
log_action('error', error_msg)
|
||||
|
||||
if upload_id:
|
||||
set_upload_progress(upload_id, 0, error_msg, 'error')
|
||||
|
||||
return False, error_msg
|
||||
|
||||
except Exception as e:
|
||||
error_msg = f'Error processing video: {str(e)}'
|
||||
log_action('error', error_msg)
|
||||
|
||||
if upload_id:
|
||||
set_upload_progress(upload_id, 0, error_msg, 'error')
|
||||
|
||||
return False, error_msg
|
||||
|
||||
|
||||
def process_pdf_file(filepath: str, upload_id: Optional[str] = None) -> Tuple[bool, str]:
|
||||
"""Process PDF file (convert to images for display).
|
||||
|
||||
Args:
|
||||
filepath: Path to PDF file
|
||||
upload_id: Optional upload ID for progress tracking
|
||||
|
||||
Returns:
|
||||
Tuple of (success, message)
|
||||
"""
|
||||
try:
|
||||
if upload_id:
|
||||
set_upload_progress(upload_id, 60, 'Converting PDF to images...')
|
||||
|
||||
# This would use pdf2image or similar
|
||||
# For now, just log the action
|
||||
log_action('info', f'PDF processing requested for: {os.path.basename(filepath)}')
|
||||
|
||||
if upload_id:
|
||||
set_upload_progress(upload_id, 85, 'PDF processed successfully')
|
||||
|
||||
return True, 'PDF processed successfully'
|
||||
|
||||
except Exception as e:
|
||||
error_msg = f'Error processing PDF: {str(e)}'
|
||||
log_action('error', error_msg)
|
||||
|
||||
if upload_id:
|
||||
set_upload_progress(upload_id, 0, error_msg, 'error')
|
||||
|
||||
return False, error_msg
|
||||
|
||||
|
||||
def get_file_size(filepath: str) -> int:
|
||||
"""Get file size in bytes.
|
||||
|
||||
Args:
|
||||
filepath: Path to file
|
||||
|
||||
Returns:
|
||||
File size in bytes, or 0 if file doesn't exist
|
||||
"""
|
||||
try:
|
||||
return os.path.getsize(filepath)
|
||||
except Exception:
|
||||
return 0
|
||||
|
||||
|
||||
def delete_file(filepath: str) -> Tuple[bool, str]:
|
||||
"""Delete a file from disk.
|
||||
|
||||
Args:
|
||||
filepath: Path to file
|
||||
|
||||
Returns:
|
||||
Tuple of (success, message)
|
||||
"""
|
||||
try:
|
||||
if os.path.exists(filepath):
|
||||
os.remove(filepath)
|
||||
log_action('info', f'File deleted: {os.path.basename(filepath)}')
|
||||
return True, 'File deleted successfully'
|
||||
else:
|
||||
return False, 'File not found'
|
||||
except Exception as e:
|
||||
error_msg = f'Error deleting file: {str(e)}'
|
||||
log_action('error', error_msg)
|
||||
return False, error_msg
|
||||
Reference in New Issue
Block a user