Merge pull request #148 from christianlouis/copilot/fix-flake8-linter-errors

fix: resolve all 1080 Flake8 linter errors across app/ directory
This commit is contained in:
Christian Krakau-Louis
2026-02-08 19:14:01 +01:00
committed by GitHub
73 changed files with 2200 additions and 2185 deletions
+14 -11
View File
@@ -1,21 +1,24 @@
""" """
API Router module that combines all API endpoints API Router module that combines all API endpoints
""" """
from fastapi import APIRouter
import logging import logging
from fastapi import APIRouter
from app.api.azure import router as azure_router
from app.api.diagnostic import router as diagnostic_router
from app.api.dropbox import router as dropbox_router
from app.api.files import router as files_router
from app.api.google_drive import router as google_drive_router
from app.api.logs import router as logs_router
from app.api.onedrive import router as onedrive_router
from app.api.openai import router as openai_router
from app.api.process import router as process_router
from app.api.settings import router as settings_router
# Import all the individual routers # Import all the individual routers
from app.api.user import router as user_router from app.api.user import router as user_router
from app.api.files import router as files_router
from app.api.process import router as process_router
from app.api.diagnostic import router as diagnostic_router
from app.api.onedrive import router as onedrive_router
from app.api.dropbox import router as dropbox_router
from app.api.openai import router as openai_router
from app.api.azure import router as azure_router
from app.api.google_drive import router as google_drive_router
from app.api.logs import router as logs_router
from app.api.settings import router as settings_router
# Set up logging # Set up logging
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
+26 -36
View File
@@ -1,24 +1,25 @@
""" """
Azure AI API endpoints Azure AI API endpoints
""" """
from fastapi import APIRouter, Request, HTTPException, status
import logging
import os
from app.auth import require_login import logging
from app.config import settings
import azure.core.exceptions
from azure.ai.documentintelligence import DocumentIntelligenceAdministrationClient
# Import the Azure modules including the administration client # Import the Azure modules including the administration client
from azure.core.credentials import AzureKeyCredential from azure.core.credentials import AzureKeyCredential
from azure.ai.documentintelligence import DocumentIntelligenceClient from fastapi import APIRouter, Request
from azure.ai.documentintelligence import DocumentIntelligenceAdministrationClient
import azure.core.exceptions from app.auth import require_login
from app.config import settings
# Set up logging # Set up logging
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
router = APIRouter() router = APIRouter()
@router.get("/azure/test") @router.get("/azure/test")
@require_login @require_login
async def test_azure_connection(request: Request): async def test_azure_connection(request: Request):
@@ -40,15 +41,14 @@ async def test_azure_connection(request: Request):
return { return {
"status": "error", "status": "error",
"message": f"Azure Document Intelligence configuration is incomplete. Missing: {', '.join(missing)}" "message": f"Azure Document Intelligence configuration is incomplete. Missing: {', '.join(missing)}",
} }
# Try to initialize the admin client and make a request to list operations # Try to initialize the admin client and make a request to list operations
try: try:
# Initialize the admin client with credentials # Initialize the admin client with credentials
admin_client = DocumentIntelligenceAdministrationClient( admin_client = DocumentIntelligenceAdministrationClient(
endpoint=settings.azure_endpoint, endpoint=settings.azure_endpoint, credential=AzureKeyCredential(settings.azure_ai_key)
credential=AzureKeyCredential(settings.azure_ai_key)
) )
# Test the connection by listing operations - this is a documented method in the admin client # Test the connection by listing operations - this is a documented method in the admin client
@@ -61,12 +61,12 @@ async def test_azure_connection(request: Request):
operations_info = [] operations_info = []
try: try:
for op in operations: for op in operations:
if hasattr(op, 'operation_id') and op.operation_id: if hasattr(op, "operation_id") and op.operation_id:
op_info = { op_info = {
"id": op.operation_id, "id": op.operation_id,
"status": op.status if hasattr(op, 'status') else "Unknown", "status": op.status if hasattr(op, "status") else "Unknown",
"created": str(op.created_on) if hasattr(op, 'created_on') else "Unknown", "created": str(op.created_on) if hasattr(op, "created_on") else "Unknown",
"kind": op.kind if hasattr(op, 'kind') else "Unknown" "kind": op.kind if hasattr(op, "kind") else "Unknown",
} }
operations_info.append(op_info) operations_info.append(op_info)
@@ -76,49 +76,39 @@ async def test_azure_connection(request: Request):
"message": f"Azure Document Intelligence connection is valid. Found {operation_count} operations.", "message": f"Azure Document Intelligence connection is valid. Found {operation_count} operations.",
"endpoint": settings.azure_endpoint, "endpoint": settings.azure_endpoint,
"operations_count": operation_count, "operations_count": operation_count,
"recent_operations": operations_info[:3] if operations_info else [] "recent_operations": operations_info[:3] if operations_info else [],
} }
except Exception as e: except Exception as e:
# If error occurs while processing operations info, still return success # If error occurs while processing operations info, still return success
logger.warning(f"Connected to Azure but couldn't parse operations: {e}") logger.warning(f"Connected to Azure but couldn't parse operations: {e}")
return { return {
"status": "success", "status": "success",
"message": "Azure Document Intelligence connection is valid, but couldn't retrieve operations details.", "message": "Azure Document Intelligence connection is valid, "
"endpoint": settings.azure_endpoint "but couldn't retrieve operations details.",
"endpoint": settings.azure_endpoint,
} }
except azure.core.exceptions.ClientAuthenticationError as e: except azure.core.exceptions.ClientAuthenticationError as e:
logger.error(f"Azure authentication error: {e}") logger.error(f"Azure authentication error: {e}")
return { return {
"status": "error", "status": "error",
"message": f"Authentication error: Invalid API key or credentials", "message": "Authentication error: Invalid API key or credentials",
"detail": str(e) "detail": str(e),
} }
except azure.core.exceptions.ServiceRequestError as e: except azure.core.exceptions.ServiceRequestError as e:
logger.error(f"Azure service request error: {e}") logger.error(f"Azure service request error: {e}")
return { return {
"status": "error", "status": "error",
"message": f"Service request error: Could not reach the Azure endpoint", "message": "Service request error: Could not reach the Azure endpoint",
"detail": str(e) "detail": str(e),
} }
except ValueError as e: except ValueError as e:
logger.error(f"Azure configuration value error: {e}") logger.error(f"Azure configuration value error: {e}")
return { return {"status": "error", "message": f"Configuration error: {str(e)}", "detail": str(e)}
"status": "error",
"message": f"Configuration error: {str(e)}",
"detail": str(e)
}
except Exception as e: except Exception as e:
logger.error(f"Azure connection test failed with unexpected error: {e}") logger.error(f"Azure connection test failed with unexpected error: {e}")
return { return {"status": "error", "message": "Connection test failed with unexpected error", "detail": str(e)}
"status": "error",
"message": f"Connection test failed with unexpected error",
"detail": str(e)
}
except Exception as e: except Exception as e:
logger.exception("Unexpected error testing Azure Document Intelligence connection") logger.exception("Unexpected error testing Azure Document Intelligence connection")
return { return {"status": "error", "message": f"Unexpected error: {str(e)}"}
"status": "error",
"message": f"Unexpected error: {str(e)}"
}
+2 -1
View File
@@ -5,10 +5,11 @@ Common utilities for API routes
import logging import logging
import os import os
from pathlib import Path from pathlib import Path
from fastapi import HTTPException, status from fastapi import HTTPException, status
from app.database import SessionLocal
from app.config import settings from app.config import settings
from app.database import SessionLocal
# Set up logging # Set up logging
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
+33 -27
View File
@@ -1,10 +1,12 @@
""" """
Diagnostic API endpoints Diagnostic API endpoints
""" """
from fastapi import APIRouter, Request, Depends
import logging import logging
from app.auth import require_login, get_current_user from fastapi import APIRouter, Depends, Request
from app.auth import get_current_user, require_login
from app.config import settings from app.config import settings
# Set up logging # Set up logging
@@ -12,6 +14,7 @@ logger = logging.getLogger(__name__)
router = APIRouter() router = APIRouter()
@router.get("/diagnostic/settings") @router.get("/diagnostic/settings")
@require_login @require_login
async def diagnostic_settings(request: Request, current_user: dict = Depends(get_current_user)): async def diagnostic_settings(request: Request, current_user: dict = Depends(get_current_user)):
@@ -19,7 +22,8 @@ async def diagnostic_settings(request: Request, current_user: dict = Depends(get
API endpoint to dump settings to the log and view basic config information API endpoint to dump settings to the log and view basic config information
This endpoint doesn't expose sensitive information like passwords or tokens This endpoint doesn't expose sensitive information like passwords or tokens
""" """
from app.utils.config_validator import dump_all_settings, get_settings_for_display from app.utils.config_validator import dump_all_settings
# Dump full settings to log for admin to see # Dump full settings to log for admin to see
dump_all_settings() dump_all_settings()
@@ -28,33 +32,35 @@ async def diagnostic_settings(request: Request, current_user: dict = Depends(get
"workdir": settings.workdir, "workdir": settings.workdir,
"external_hostname": settings.external_hostname, "external_hostname": settings.external_hostname,
"configured_services": { "configured_services": {
"email": bool(getattr(settings, 'email_host', None)), "email": bool(getattr(settings, "email_host", None)),
"s3": bool(getattr(settings, 's3_bucket_name', None)), "s3": bool(getattr(settings, "s3_bucket_name", None)),
"dropbox": bool(getattr(settings, 'dropbox_refresh_token', None)), "dropbox": bool(getattr(settings, "dropbox_refresh_token", None)),
"onedrive": bool(getattr(settings, 'onedrive_refresh_token', None)), "onedrive": bool(getattr(settings, "onedrive_refresh_token", None)),
"nextcloud": bool(getattr(settings, 'nextcloud_upload_url', None)), "nextcloud": bool(getattr(settings, "nextcloud_upload_url", None)),
"sftp": bool(getattr(settings, 'sftp_host', None)), "sftp": bool(getattr(settings, "sftp_host", None)),
"paperless": bool(getattr(settings, 'paperless_host', None)), "paperless": bool(getattr(settings, "paperless_host", None)),
"google_drive": bool(getattr(settings, 'google_drive_credentials_json', None)), "google_drive": bool(getattr(settings, "google_drive_credentials_json", None)),
"uptime_kuma": bool(getattr(settings, 'uptime_kuma_url', None)), "uptime_kuma": bool(getattr(settings, "uptime_kuma_url", None)),
"auth": bool(getattr(settings, 'authentik_config_url', None)), "auth": bool(getattr(settings, "authentik_config_url", None)),
"openai": bool(getattr(settings, 'openai_api_key', None)), "openai": bool(getattr(settings, "openai_api_key", None)),
"azure": bool(getattr(settings, 'azure_api_key', None) and getattr(settings, 'azure_endpoint', None)), "azure": bool(getattr(settings, "azure_api_key", None) and getattr(settings, "azure_endpoint", None)),
}, },
"imap_enabled": bool(getattr(settings, 'imap1_host', None) or getattr(settings, 'imap2_host', None)), "imap_enabled": bool(getattr(settings, "imap1_host", None) or getattr(settings, "imap2_host", None)),
} }
return { return {
"status": "success", "status": "success",
"settings": safe_settings, "settings": safe_settings,
"message": "Full settings have been dumped to application logs" "message": "Full settings have been dumped to application logs",
} }
@router.post("/diagnostic/test-notification") @router.post("/diagnostic/test-notification")
@require_login @require_login
async def test_notification(request: Request): async def test_notification(request: Request):
# Add request_time to request.state # Add request_time to request.state
import datetime import datetime
request.state.request_time = datetime.datetime.utcnow().isoformat() request.state.request_time = datetime.datetime.utcnow().isoformat()
""" """
Send a test notification through all configured notification channels Send a test notification through all configured notification channels
@@ -62,20 +68,23 @@ async def test_notification(request: Request):
from app.utils.notification import send_notification from app.utils.notification import send_notification
try: try:
notification_urls = getattr(settings, 'notification_urls', []) notification_urls = getattr(settings, "notification_urls", [])
if not notification_urls: if not notification_urls:
return { return {
"status": "warning", "status": "warning",
"message": "No notification services configured. Add notification URLs to your configuration." "message": "No notification services configured. Add notification URLs to your configuration.",
} }
# Send a test notification # Send a test notification
hostname = settings.external_hostname or "Document Processor" hostname = settings.external_hostname or "Document Processor"
result = send_notification( result = send_notification(
title=f"Test Notification from {hostname}", title=f"Test Notification from {hostname}",
message=f"This is a test notification sent at {request.state.request_time}. If you're receiving this, notifications are working!", message=(
f"This is a test notification sent at {request.state.request_time}. "
"If you're receiving this, notifications are working!"
),
notification_type="success", notification_type="success",
tags=["test", "notification", "diagnostic"] tags=["test", "notification", "diagnostic"],
) )
if result: if result:
@@ -83,18 +92,15 @@ async def test_notification(request: Request):
return { return {
"status": "success", "status": "success",
"message": f"Test notification sent successfully to {len(notification_urls)} service(s)", "message": f"Test notification sent successfully to {len(notification_urls)} service(s)",
"services_count": len(notification_urls) "services_count": len(notification_urls),
} }
else: else:
logger.warning("Test notification send attempt returned False") logger.warning("Test notification send attempt returned False")
return { return {
"status": "error", "status": "error",
"message": "Failed to send test notification. Check application logs for details." "message": "Failed to send test notification. Check application logs for details.",
} }
except Exception as e: except Exception as e:
logger.exception(f"Error sending test notification: {e}") logger.exception(f"Error sending test notification: {e}")
return { return {"status": "error", "message": f"Error sending notification: {str(e)}"}
"status": "error",
"message": f"Error sending notification: {str(e)}"
}
+2 -1
View File
@@ -2,10 +2,11 @@
Dropbox API endpoints Dropbox API endpoints
""" """
from fastapi import APIRouter, Request, HTTPException, status, Form
import logging import logging
import os import os
import requests import requests
from fastapi import APIRouter, Form, HTTPException, Request, status
from app.auth import require_login from app.auth import require_login
from app.config import settings from app.config import settings
+8 -8
View File
@@ -435,15 +435,15 @@ def retry_subtask(
# Map subtask names to their corresponding Celery tasks # Map subtask names to their corresponding Celery tasks
from app.tasks.upload_to_dropbox import upload_to_dropbox from app.tasks.upload_to_dropbox import upload_to_dropbox
from app.tasks.upload_to_nextcloud import upload_to_nextcloud
from app.tasks.upload_to_paperless import upload_to_paperless
from app.tasks.upload_to_google_drive import upload_to_google_drive
from app.tasks.upload_to_onedrive import upload_to_onedrive
from app.tasks.upload_to_s3 import upload_to_s3
from app.tasks.upload_to_webdav import upload_to_webdav
from app.tasks.upload_to_ftp import upload_to_ftp
from app.tasks.upload_to_sftp import upload_to_sftp
from app.tasks.upload_to_email import upload_to_email from app.tasks.upload_to_email import upload_to_email
from app.tasks.upload_to_ftp import upload_to_ftp
from app.tasks.upload_to_google_drive import upload_to_google_drive
from app.tasks.upload_to_nextcloud import upload_to_nextcloud
from app.tasks.upload_to_onedrive import upload_to_onedrive
from app.tasks.upload_to_paperless import upload_to_paperless
from app.tasks.upload_to_s3 import upload_to_s3
from app.tasks.upload_to_sftp import upload_to_sftp
from app.tasks.upload_to_webdav import upload_to_webdav
task_map = { task_map = {
"upload_to_dropbox": upload_to_dropbox, "upload_to_dropbox": upload_to_dropbox,
+3 -2
View File
@@ -2,11 +2,12 @@
Google Drive API endpoints Google Drive API endpoints
""" """
from fastapi import APIRouter, Request, HTTPException, status, Form
import logging import logging
import os import os
from typing import Optional
from datetime import datetime from datetime import datetime
from typing import Optional
from fastapi import APIRouter, Form, HTTPException, Request, status
from app.auth import require_login from app.auth import require_login
from app.config import settings from app.config import settings
+37 -48
View File
@@ -1,21 +1,24 @@
""" """
Processing logs API endpoints Processing logs API endpoints
""" """
from fastapi import APIRouter, Request, HTTPException, Depends, Query
from sqlalchemy.orm import Session
from sqlalchemy import desc
from typing import Optional
import logging
from app.auth import require_login import logging
from app.models import ProcessingLog, FileRecord from typing import Optional
from fastapi import APIRouter, Depends, HTTPException, Query, Request
from sqlalchemy import desc
from sqlalchemy.orm import Session
from app.api.common import get_db from app.api.common import get_db
from app.auth import require_login
from app.models import FileRecord, ProcessingLog
# Set up logging # Set up logging
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
router = APIRouter() router = APIRouter()
@router.get("/logs") @router.get("/logs")
@require_login @require_login
def list_processing_logs( def list_processing_logs(
@@ -23,7 +26,7 @@ def list_processing_logs(
db: Session = Depends(get_db), db: Session = Depends(get_db),
file_id: Optional[int] = Query(None, description="Filter by file ID"), file_id: Optional[int] = Query(None, description="Filter by file ID"),
task_id: Optional[str] = Query(None, description="Filter by task ID"), task_id: Optional[str] = Query(None, description="Filter by task ID"),
limit: int = Query(100, ge=1, le=1000, description="Number of logs to return") limit: int = Query(100, ge=1, le=1000, description="Number of logs to return"),
): ):
""" """
Returns a JSON list of ProcessingLog entries. Returns a JSON list of ProcessingLog entries.
@@ -62,24 +65,23 @@ def list_processing_logs(
# Return a simple list of dicts # Return a simple list of dicts
result = [] result = []
for log in logs: for log in logs:
result.append({ result.append(
{
"id": log.id, "id": log.id,
"file_id": log.file_id, "file_id": log.file_id,
"task_id": log.task_id, "task_id": log.task_id,
"step_name": log.step_name, "step_name": log.step_name,
"status": log.status, "status": log.status,
"message": log.message, "message": log.message,
"timestamp": log.timestamp.isoformat() if log.timestamp else None "timestamp": log.timestamp.isoformat() if log.timestamp else None,
}) }
)
return result return result
@router.get("/logs/file/{file_id}") @router.get("/logs/file/{file_id}")
@require_login @require_login
def get_file_processing_logs( def get_file_processing_logs(request: Request, file_id: int, db: Session = Depends(get_db)):
request: Request,
file_id: int,
db: Session = Depends(get_db)
):
""" """
Get all processing logs for a specific file. Get all processing logs for a specific file.
Returns logs ordered by timestamp (oldest first to show processing flow). Returns logs ordered by timestamp (oldest first to show processing flow).
@@ -89,27 +91,24 @@ def get_file_processing_logs(
# Check if file exists # Check if file exists
file_record = db.query(FileRecord).filter(FileRecord.id == file_id).first() file_record = db.query(FileRecord).filter(FileRecord.id == file_id).first()
if not file_record: if not file_record:
raise HTTPException( raise HTTPException(status_code=404, detail=f"File with ID {file_id} not found")
status_code=404,
detail=f"File with ID {file_id} not found"
)
# Get all logs for this file # Get all logs for this file
logs = db.query(ProcessingLog).filter( logs = db.query(ProcessingLog).filter(ProcessingLog.file_id == file_id).order_by(ProcessingLog.timestamp).all()
ProcessingLog.file_id == file_id
).order_by(ProcessingLog.timestamp).all()
# Build response # Build response
log_list = [] log_list = []
for log in logs: for log in logs:
log_list.append({ log_list.append(
{
"id": log.id, "id": log.id,
"task_id": log.task_id, "task_id": log.task_id,
"step_name": log.step_name, "step_name": log.step_name,
"status": log.status, "status": log.status,
"message": log.message, "message": log.message,
"timestamp": log.timestamp.isoformat() if log.timestamp else None "timestamp": log.timestamp.isoformat() if log.timestamp else None,
}) }
)
return { return {
"file": { "file": {
@@ -117,48 +116,38 @@ def get_file_processing_logs(
"original_filename": file_record.original_filename, "original_filename": file_record.original_filename,
"file_size": file_record.file_size, "file_size": file_record.file_size,
"mime_type": file_record.mime_type, "mime_type": file_record.mime_type,
"created_at": file_record.created_at.isoformat() if file_record.created_at else None "created_at": file_record.created_at.isoformat() if file_record.created_at else None,
}, },
"logs": log_list, "logs": log_list,
"total_logs": len(log_list) "total_logs": len(log_list),
} }
@router.get("/logs/task/{task_id}") @router.get("/logs/task/{task_id}")
@require_login @require_login
def get_task_processing_logs( def get_task_processing_logs(request: Request, task_id: str, db: Session = Depends(get_db)):
request: Request,
task_id: str,
db: Session = Depends(get_db)
):
""" """
Get all processing logs for a specific task. Get all processing logs for a specific task.
Returns logs ordered by timestamp (oldest first to show processing flow). Returns logs ordered by timestamp (oldest first to show processing flow).
""" """
# Get all logs for this task # Get all logs for this task
logs = db.query(ProcessingLog).filter( logs = db.query(ProcessingLog).filter(ProcessingLog.task_id == task_id).order_by(ProcessingLog.timestamp).all()
ProcessingLog.task_id == task_id
).order_by(ProcessingLog.timestamp).all()
if not logs: if not logs:
raise HTTPException( raise HTTPException(status_code=404, detail=f"No logs found for task {task_id}")
status_code=404,
detail=f"No logs found for task {task_id}"
)
# Build response # Build response
log_list = [] log_list = []
for log in logs: for log in logs:
log_list.append({ log_list.append(
{
"id": log.id, "id": log.id,
"file_id": log.file_id, "file_id": log.file_id,
"step_name": log.step_name, "step_name": log.step_name,
"status": log.status, "status": log.status,
"message": log.message, "message": log.message,
"timestamp": log.timestamp.isoformat() if log.timestamp else None "timestamp": log.timestamp.isoformat() if log.timestamp else None,
})
return {
"task_id": task_id,
"logs": log_list,
"total_logs": len(log_list)
} }
)
return {"task_id": task_id, "logs": log_list, "total_logs": len(log_list)}
+3 -2
View File
@@ -2,12 +2,13 @@
OneDrive API endpoints OneDrive API endpoints
""" """
from fastapi import APIRouter, Request, HTTPException, status, Form
import logging import logging
import os import os
import requests
from datetime import datetime, timedelta from datetime import datetime, timedelta
import requests
from fastapi import APIRouter, Form, HTTPException, Request, status
from app.auth import require_login from app.auth import require_login
from app.config import settings from app.config import settings
from app.utils.oauth_helper import exchange_oauth_token from app.utils.oauth_helper import exchange_oauth_token
+9 -17
View File
@@ -1,10 +1,10 @@
""" """
OpenAI API endpoints OpenAI API endpoints
""" """
from fastapi import APIRouter, Request, HTTPException, status
import logging import logging
import os
import requests from fastapi import APIRouter, Request
from app.auth import require_login from app.auth import require_login
from app.config import settings from app.config import settings
@@ -14,6 +14,7 @@ logger = logging.getLogger(__name__)
router = APIRouter() router = APIRouter()
@router.get("/openai/test") @router.get("/openai/test")
@require_login @require_login
async def test_openai_connection(request: Request): async def test_openai_connection(request: Request):
@@ -28,10 +29,7 @@ async def test_openai_connection(request: Request):
# Check if API key is configured # Check if API key is configured
if not settings.openai_api_key: if not settings.openai_api_key:
logger.warning("No OpenAI API key configured") logger.warning("No OpenAI API key configured")
return { return {"status": "error", "message": "No OpenAI API key is configured"}
"status": "error",
"message": "No OpenAI API key is configured"
}
# Configure the client # Configure the client
client = openai.OpenAI(api_key=settings.openai_api_key) client = openai.OpenAI(api_key=settings.openai_api_key)
@@ -46,7 +44,7 @@ async def test_openai_connection(request: Request):
return { return {
"status": "success", "status": "success",
"message": "OpenAI API key is valid", "message": "OpenAI API key is valid",
"models_available": len(models.data) if hasattr(models, "data") else "Unknown" "models_available": len(models.data) if hasattr(models, "data") else "Unknown",
} }
except Exception as e: except Exception as e:
error_msg = str(e) error_msg = str(e)
@@ -58,18 +56,12 @@ async def test_openai_connection(request: Request):
return { return {
"status": "error", "status": "error",
"message": f"API key validation failed: {error_msg}", "message": f"API key validation failed: {error_msg}",
"is_auth_error": is_auth_error "is_auth_error": is_auth_error,
} }
except ImportError: except ImportError:
logger.exception("OpenAI package not installed") logger.exception("OpenAI package not installed")
return { return {"status": "error", "message": "OpenAI package not installed"}
"status": "error",
"message": "OpenAI package not installed"
}
except Exception as e: except Exception as e:
logger.exception("Unexpected error testing OpenAI connection") logger.exception("Unexpected error testing OpenAI connection")
return { return {"status": "error", "message": f"Unexpected error: {str(e)}"}
"status": "error",
"message": f"Unexpected error: {str(e)}"
}
+36 -95
View File
@@ -3,21 +3,22 @@ API endpoints for managing application settings.
""" """
import logging import logging
from typing import Dict, Any, Optional from typing import Any, Dict, Optional
from fastapi import APIRouter, Depends, HTTPException, Request, status
from sqlalchemy.orm import Session from fastapi import APIRouter, Depends, HTTPException, Request, status
from pydantic import BaseModel, Field from pydantic import BaseModel, Field
from sqlalchemy.orm import Session
from app.database import get_db
from app.config import settings from app.config import settings
from app.database import get_db
from app.utils.settings_service import ( from app.utils.settings_service import (
get_all_settings_from_db, SETTING_METADATA,
save_setting_to_db,
delete_setting_from_db, delete_setting_from_db,
get_all_settings_from_db,
get_setting_metadata, get_setting_metadata,
get_settings_by_category, get_settings_by_category,
save_setting_to_db,
validate_setting_value, validate_setting_value,
SETTING_METADATA,
) )
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -34,21 +35,20 @@ def require_admin(request: Request) -> dict:
""" """
user = request.session.get("user") user = request.session.get("user")
if not user or not user.get("is_admin"): if not user or not user.get("is_admin"):
raise HTTPException( raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Admin access required")
status_code=status.HTTP_403_FORBIDDEN,
detail="Admin access required"
)
return user return user
class SettingUpdate(BaseModel): class SettingUpdate(BaseModel):
"""Model for updating a setting""" """Model for updating a setting"""
key: str = Field(..., description="Setting key") key: str = Field(..., description="Setting key")
value: Optional[str] = Field(None, description="Setting value (None to delete)") value: Optional[str] = Field(None, description="Setting value (None to delete)")
class SettingResponse(BaseModel): class SettingResponse(BaseModel):
"""Model for setting response""" """Model for setting response"""
key: str key: str
value: Optional[str] value: Optional[str]
metadata: Dict[str, Any] metadata: Dict[str, Any]
@@ -56,17 +56,14 @@ class SettingResponse(BaseModel):
class SettingsListResponse(BaseModel): class SettingsListResponse(BaseModel):
"""Model for list of settings""" """Model for list of settings"""
settings: Dict[str, Any] settings: Dict[str, Any]
categories: Dict[str, list] categories: Dict[str, list]
db_settings: Dict[str, str] db_settings: Dict[str, str]
@router.get("/", response_model=SettingsListResponse) @router.get("/", response_model=SettingsListResponse)
async def get_settings( async def get_settings(request: Request, db: Session = Depends(get_db), admin: dict = Depends(require_admin)):
request: Request,
db: Session = Depends(get_db),
admin: dict = Depends(require_admin)
):
""" """
Get all application settings with metadata. Get all application settings with metadata.
Admin only. Admin only.
@@ -77,10 +74,7 @@ async def get_settings(
for key in SETTING_METADATA.keys(): for key in SETTING_METADATA.keys():
if hasattr(settings, key): if hasattr(settings, key):
value = getattr(settings, key) value = getattr(settings, key)
current_settings[key] = { current_settings[key] = {"value": value, "metadata": get_setting_metadata(key)}
"value": value,
"metadata": get_setting_metadata(key)
}
# Get settings stored in database # Get settings stored in database
db_settings = get_all_settings_from_db(db) db_settings = get_all_settings_from_db(db)
@@ -88,26 +82,14 @@ async def get_settings(
# Get settings organized by category # Get settings organized by category
categories = get_settings_by_category() categories = get_settings_by_category()
return SettingsListResponse( return SettingsListResponse(settings=current_settings, categories=categories, db_settings=db_settings)
settings=current_settings,
categories=categories,
db_settings=db_settings
)
except Exception as e: except Exception as e:
logger.error(f"Error retrieving settings: {e}") logger.error(f"Error retrieving settings: {e}")
raise HTTPException( raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Failed to retrieve settings")
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to retrieve settings"
)
@router.get("/{key}", response_model=SettingResponse) @router.get("/{key}", response_model=SettingResponse)
async def get_setting( async def get_setting(key: str, request: Request, db: Session = Depends(get_db), admin: dict = Depends(require_admin)):
key: str,
request: Request,
db: Session = Depends(get_db),
admin: dict = Depends(require_admin)
):
""" """
Get a specific setting by key. Get a specific setting by key.
Admin only. Admin only.
@@ -119,16 +101,11 @@ async def get_setting(
# Get metadata # Get metadata
metadata = get_setting_metadata(key) metadata = get_setting_metadata(key)
return SettingResponse( return SettingResponse(key=key, value=str(value) if value is not None else None, metadata=metadata)
key=key,
value=str(value) if value is not None else None,
metadata=metadata
)
except Exception as e: except Exception as e:
logger.error(f"Error retrieving setting {key}: {e}") logger.error(f"Error retrieving setting {key}: {e}")
raise HTTPException( raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=f"Failed to retrieve setting: {key}"
detail=f"Failed to retrieve setting: {key}"
) )
@@ -138,7 +115,7 @@ async def update_setting(
setting: SettingUpdate, setting: SettingUpdate,
request: Request, request: Request,
db: Session = Depends(get_db), db: Session = Depends(get_db),
admin: dict = Depends(require_admin) admin: dict = Depends(require_admin),
): ):
""" """
Update a specific setting. Update a specific setting.
@@ -149,17 +126,13 @@ async def update_setting(
if setting.value is not None: if setting.value is not None:
is_valid, error_message = validate_setting_value(key, setting.value) is_valid, error_message = validate_setting_value(key, setting.value)
if not is_valid: if not is_valid:
raise HTTPException( raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=error_message)
status_code=status.HTTP_400_BAD_REQUEST,
detail=error_message
)
# Save to database # Save to database
success = save_setting_to_db(db, key, setting.value) success = save_setting_to_db(db, key, setting.value)
if not success: if not success:
raise HTTPException( raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Failed to save setting to database"
detail="Failed to save setting to database"
) )
# Get metadata # Get metadata
@@ -171,24 +144,20 @@ async def update_setting(
"message": f"Setting '{key}' updated successfully", "message": f"Setting '{key}' updated successfully",
"restart_required": restart_required, "restart_required": restart_required,
"key": key, "key": key,
"value": setting.value "value": setting.value,
} }
except HTTPException: except HTTPException:
raise raise
except Exception as e: except Exception as e:
logger.error(f"Error updating setting {key}: {e}") logger.error(f"Error updating setting {key}: {e}")
raise HTTPException( raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=f"Failed to update setting: {key}"
detail=f"Failed to update setting: {key}"
) )
@router.delete("/{key}") @router.delete("/{key}")
async def delete_setting( async def delete_setting(
key: str, key: str, request: Request, db: Session = Depends(get_db), admin: dict = Depends(require_admin)
request: Request,
db: Session = Depends(get_db),
admin: dict = Depends(require_admin)
): ):
""" """
Delete a setting from the database (reverts to environment variable or default). Delete a setting from the database (reverts to environment variable or default).
@@ -197,31 +166,24 @@ async def delete_setting(
try: try:
success = delete_setting_from_db(db, key) success = delete_setting_from_db(db, key)
if not success: if not success:
raise HTTPException( raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"Setting '{key}' not found in database")
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Setting '{key}' not found in database"
)
return { return {
"success": True, "success": True,
"message": f"Setting '{key}' deleted from database (will use environment variable or default)" "message": f"Setting '{key}' deleted from database (will use environment variable or default)",
} }
except HTTPException: except HTTPException:
raise raise
except Exception as e: except Exception as e:
logger.error(f"Error deleting setting {key}: {e}") logger.error(f"Error deleting setting {key}: {e}")
raise HTTPException( raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=f"Failed to delete setting: {key}"
detail=f"Failed to delete setting: {key}"
) )
@router.post("/bulk-update") @router.post("/bulk-update")
async def bulk_update_settings( async def bulk_update_settings(
updates: list[SettingUpdate], updates: list[SettingUpdate], request: Request, db: Session = Depends(get_db), admin: dict = Depends(require_admin)
request: Request,
db: Session = Depends(get_db),
admin: dict = Depends(require_admin)
): ):
""" """
Update multiple settings at once. Update multiple settings at once.
@@ -236,40 +198,19 @@ async def bulk_update_settings(
if update.value is not None: if update.value is not None:
is_valid, error_message = validate_setting_value(update.key, update.value) is_valid, error_message = validate_setting_value(update.key, update.value)
if not is_valid: if not is_valid:
errors.append({ errors.append({"key": update.key, "error": error_message})
"key": update.key,
"error": error_message
})
continue continue
# Save to database # Save to database
success = save_setting_to_db(db, update.key, update.value) success = save_setting_to_db(db, update.key, update.value)
if success: if success:
results.append({ results.append({"key": update.key, "value": update.value, "status": "success"})
"key": update.key,
"value": update.value,
"status": "success"
})
else: else:
errors.append({ errors.append({"key": update.key, "error": "Failed to save to database"})
"key": update.key,
"error": "Failed to save to database"
})
except Exception as e: except Exception as e:
logger.error(f"Error updating setting {update.key}: {e}") logger.error(f"Error updating setting {update.key}: {e}")
errors.append({ errors.append({"key": update.key, "error": str(e)})
"key": update.key,
"error": str(e)
})
restart_required = any( restart_required = any(get_setting_metadata(result["key"]).get("restart_required", False) for result in results)
get_setting_metadata(result["key"]).get("restart_required", False)
for result in results
)
return { return {"success": len(errors) == 0, "updated": results, "errors": errors, "restart_required": restart_required}
"success": len(errors) == 0,
"updated": results,
"errors": errors,
"restart_required": restart_required
}
+7 -2
View File
@@ -1,15 +1,18 @@
""" """
User-related API endpoints User-related API endpoints
""" """
from fastapi import APIRouter, Request, HTTPException
from hashlib import md5
import logging import logging
from hashlib import md5
from fastapi import APIRouter, HTTPException, Request
# Set up logging # Set up logging
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
router = APIRouter() router = APIRouter()
async def whoami_handler(request: Request): async def whoami_handler(request: Request):
""" """
Returns user info if logged in, else 401. Returns user info if logged in, else 401.
@@ -33,11 +36,13 @@ async def whoami_handler(request: Request):
return user_response return user_response
# Register the same handler under two different paths # Register the same handler under two different paths
@router.get("/whoami") @router.get("/whoami")
async def whoami(request: Request): async def whoami(request: Request):
return await whoami_handler(request) return await whoami_handler(request)
@router.get("/auth/whoami") @router.get("/auth/whoami")
async def auth_whoami(request: Request): async def auth_whoami(request: Request):
return await whoami_handler(request) return await whoami_handler(request)
+14 -26
View File
@@ -1,13 +1,12 @@
import os
import inspect
import hashlib import hashlib
import inspect
import pathlib
from functools import wraps from functools import wraps
from authlib.integrations.starlette_client import OAuth from authlib.integrations.starlette_client import OAuth
from fastapi import APIRouter, Request, status from fastapi import APIRouter, Request, status
from starlette.responses import RedirectResponse
from fastapi.templating import Jinja2Templates from fastapi.templating import Jinja2Templates
import pathlib from starlette.responses import RedirectResponse
from app.config import settings from app.config import settings
@@ -63,11 +62,12 @@ def get_gravatar_url(email):
"""Generate a Gravatar URL for the given email""" """Generate a Gravatar URL for the given email"""
email = email.lower().strip() email = email.lower().strip()
# MD5 is used here for Gravatar's URL generation (not for security), so usedforsecurity=False # MD5 is used here for Gravatar's URL generation (not for security), so usedforsecurity=False
email_hash = hashlib.md5(email.encode('utf-8'), usedforsecurity=False).hexdigest() email_hash = hashlib.md5(email.encode("utf-8"), usedforsecurity=False).hexdigest()
return f"https://www.gravatar.com/avatar/{email_hash}?d=identicon" return f"https://www.gravatar.com/avatar/{email_hash}?d=identicon"
if AUTH_ENABLED: if AUTH_ENABLED:
@router.get("/login") @router.get("/login")
async def login(request: Request): async def login(request: Request):
"""Show login page with appropriate authentication options""" """Show login page with appropriate authentication options"""
@@ -79,18 +79,15 @@ if AUTH_ENABLED:
"message": request.query_params.get("message"), "message": request.query_params.get("message"),
"show_oauth": OAUTH_CONFIGURED, "show_oauth": OAUTH_CONFIGURED,
"oauth_provider_name": OAUTH_PROVIDER_NAME, "oauth_provider_name": OAUTH_PROVIDER_NAME,
"app_version": settings.version # Changed from app_version to version "app_version": settings.version, # Changed from app_version to version
} },
) )
@router.get("/oauth-login") @router.get("/oauth-login")
async def oauth_login(request: Request): async def oauth_login(request: Request):
"""Handle OAuth login flow""" """Handle OAuth login flow"""
if not OAUTH_CONFIGURED: if not OAUTH_CONFIGURED:
return RedirectResponse( return RedirectResponse(url="/login?error=OAuth+not+configured", status_code=status.HTTP_302_FOUND)
url="/login?error=OAuth+not+configured",
status_code=status.HTTP_302_FOUND
)
redirect_uri = request.url_for("oauth_callback") redirect_uri = request.url_for("oauth_callback")
return await oauth.authentik.authorize_redirect(request, redirect_uri) return await oauth.authentik.authorize_redirect(request, redirect_uri)
@@ -103,8 +100,7 @@ if AUTH_ENABLED:
userinfo = token.get("userinfo") userinfo = token.get("userinfo")
if not userinfo: if not userinfo:
return RedirectResponse( return RedirectResponse(
url="/login?error=Failed+to+retrieve+user+information", url="/login?error=Failed+to+retrieve+user+information", status_code=status.HTTP_302_FOUND
status_code=status.HTTP_302_FOUND
) )
# Store user info in session # Store user info in session
@@ -137,8 +133,7 @@ if AUTH_ENABLED:
except Exception as e: except Exception as e:
print(f"OAuth authentication error: {str(e)}") print(f"OAuth authentication error: {str(e)}")
return RedirectResponse( return RedirectResponse(
url=f"/login?error=Authentication+failed:+{str(e)}", url=f"/login?error=Authentication+failed:+{str(e)}", status_code=status.HTTP_302_FOUND
status_code=status.HTTP_302_FOUND
) )
@router.post("/auth") @router.post("/auth")
@@ -148,8 +143,7 @@ if AUTH_ENABLED:
username = form_data.get("username") username = form_data.get("username")
password = form_data.get("password") password = form_data.get("password")
if (username == settings.admin_username and if username == settings.admin_username and password == settings.admin_password:
password == settings.admin_password):
# Create user session # Create user session
request.session["user"] = { request.session["user"] = {
"id": "admin", "id": "admin",
@@ -157,25 +151,19 @@ if AUTH_ENABLED:
"email": f"{username}@local.docuelevate", "email": f"{username}@local.docuelevate",
"preferred_username": username, "preferred_username": username,
"picture": "/static/images/default-avatar.svg", "picture": "/static/images/default-avatar.svg",
"is_admin": True "is_admin": True,
} }
# Redirect to original destination or default # Redirect to original destination or default
redirect_url = request.session.pop("redirect_after_login", "/upload") redirect_url = request.session.pop("redirect_after_login", "/upload")
return RedirectResponse(url=redirect_url, status_code=302) return RedirectResponse(url=redirect_url, status_code=302)
else: else:
return RedirectResponse( return RedirectResponse(url="/login?error=Invalid+username+or+password", status_code=302)
url="/login?error=Invalid+username+or+password",
status_code=302
)
@router.get("/logout") @router.get("/logout")
async def logout(request: Request): async def logout(request: Request):
"""Handle user logout""" """Handle user logout"""
request.session.pop("user", None) request.session.pop("user", None)
return RedirectResponse( return RedirectResponse(url="/login?message=You+have+been+logged+out+successfully", status_code=302)
url="/login?message=You+have+been+logged+out+successfully",
status_code=302
)
@router.get("/api/auth/whoami") @router.get("/api/auth/whoami")
+10 -7
View File
@@ -1,6 +1,8 @@
# app/celery_app.py # app/celery_app.py
from celery import Celery from celery import Celery
from celery.signals import task_failure
from app.config import settings from app.config import settings
celery = Celery( celery = Celery(
@@ -14,29 +16,30 @@ celery = Celery(
celery.conf.broker_connection_retry_on_startup = True celery.conf.broker_connection_retry_on_startup = True
# Set the default queue and routing so that tasks are enqueued on "document_processor" # Set the default queue and routing so that tasks are enqueued on "document_processor"
celery.conf.task_default_queue = 'document_processor' celery.conf.task_default_queue = "document_processor"
celery.conf.task_routes = { celery.conf.task_routes = {
"app.tasks.*": {"queue": "document_processor"}, "app.tasks.*": {"queue": "document_processor"},
} }
# Task failure notification handler
from celery.signals import task_failure
@task_failure.connect @task_failure.connect
def task_failure_handler(sender=None, task_id=None, exception=None, args=None, def task_failure_handler(
kwargs=None, traceback=None, einfo=None, **kw): sender=None, task_id=None, exception=None, args=None, kwargs=None, traceback=None, einfo=None, **kw
):
"""Handler for Celery task failures to send notifications""" """Handler for Celery task failures to send notifications"""
if getattr(settings, 'notify_on_task_failure', True): if getattr(settings, "notify_on_task_failure", True):
try: try:
# Import here to avoid circular imports # Import here to avoid circular imports
from app.utils.notification import notify_celery_failure from app.utils.notification import notify_celery_failure
notify_celery_failure( notify_celery_failure(
task_name=sender.name if sender else "Unknown", task_name=sender.name if sender else "Unknown",
task_id=task_id or "N/A", task_id=task_id or "N/A",
exc=exception, exc=exception,
args=args or [], args=args or [],
kwargs=kwargs or {} kwargs=kwargs or {},
) )
except Exception as e: except Exception as e:
import logging import logging
logging.exception(f"Failed to send task failure notification: {e}") logging.exception(f"Failed to send task failure notification: {e}")
+40 -33
View File
@@ -1,65 +1,72 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from app.config import settings from celery.schedules import crontab
# Ensure tasks are loaded
from app import tasks # noqa: F401 - Imports app/tasks.py so Celery can register tasks
# Import the shared Celery instance # Import the shared Celery instance
from app.celery_app import celery from app.celery_app import celery
from app.config import settings
# Ensure tasks are loaded from app.tasks.check_credentials import check_credentials
from app import tasks # <— This imports app/tasks.py so Celery can register tasks from app.tasks.convert_to_pdf import convert_to_pdf # noqa: F401
from app.tasks.embed_metadata_into_pdf import embed_metadata_into_pdf # noqa: F401
from app.tasks.extract_metadata_with_gpt import extract_metadata_with_gpt # noqa: F401
from app.tasks.imap_tasks import pull_all_inboxes # noqa: F401
# **Ensure all tasks are imported before Celery starts** # **Ensure all tasks are imported before Celery starts**
from app.tasks.process_document import process_document from app.tasks.process_document import process_document # noqa: F401
from app.tasks.process_with_azure_document_intelligence import process_with_azure_document_intelligence from app.tasks.process_with_azure_document_intelligence import process_with_azure_document_intelligence # noqa: F401
from app.tasks.rotate_pdf_pages import rotate_pdf_pages from app.tasks.refine_text_with_gpt import refine_text_with_gpt # noqa: F401
from app.tasks.refine_text_with_gpt import refine_text_with_gpt from app.tasks.rotate_pdf_pages import rotate_pdf_pages # noqa: F401
from app.tasks.extract_metadata_with_gpt import extract_metadata_with_gpt from app.tasks.send_to_all import send_to_all_destinations # noqa: F401
from app.tasks.embed_metadata_into_pdf import embed_metadata_into_pdf
from app.tasks.convert_to_pdf import convert_to_pdf
# Import new send tasks # Import new send tasks
from app.tasks.upload_to_dropbox import upload_to_dropbox from app.tasks.upload_to_dropbox import upload_to_dropbox # noqa: F401
from app.tasks.upload_to_paperless import upload_to_paperless from app.tasks.upload_to_email import upload_to_email # noqa: F401
from app.tasks.upload_to_nextcloud import upload_to_nextcloud from app.tasks.upload_to_ftp import upload_to_ftp # noqa: F401
from app.tasks.upload_to_google_drive import upload_to_google_drive from app.tasks.upload_to_google_drive import upload_to_google_drive # noqa: F401
from app.tasks.upload_to_webdav import upload_to_webdav from app.tasks.upload_to_nextcloud import upload_to_nextcloud # noqa: F401
from app.tasks.upload_to_s3 import upload_to_s3 from app.tasks.upload_to_onedrive import upload_to_onedrive # noqa: F401
from app.tasks.upload_to_onedrive import upload_to_onedrive from app.tasks.upload_to_paperless import upload_to_paperless # noqa: F401
from app.tasks.upload_to_ftp import upload_to_ftp from app.tasks.upload_to_s3 import upload_to_s3 # noqa: F401
from app.tasks.upload_to_sftp import upload_to_sftp from app.tasks.upload_to_sftp import upload_to_sftp # noqa: F401
from app.tasks.upload_to_email import upload_to_email from app.tasks.upload_to_webdav import upload_to_webdav # noqa: F401
from app.tasks.uptime_kuma_tasks import ping_uptime_kuma # noqa: F401
from app.tasks.imap_tasks import pull_all_inboxes
from app.tasks.send_to_all import send_to_all_destinations
from app.tasks.uptime_kuma_tasks import ping_uptime_kuma
from app.tasks.check_credentials import check_credentials
celery.conf.task_routes = { celery.conf.task_routes = {
"app.tasks.*": {"queue": "default"}, "app.tasks.*": {"queue": "default"},
} }
@celery.task @celery.task
def test_task(): def test_task():
return "Celery is working!" return "Celery is working!"
# If you want Celery Beat to run the poll task every minute, add:
from celery.schedules import crontab
# Run the check_credentials task at startup # Run the check_credentials task at startup
check_credentials.apply_async(countdown=10) # Run 10 seconds after worker starts check_credentials.apply_async(countdown=10) # Run 10 seconds after worker starts
celery.conf.beat_schedule = { celery.conf.beat_schedule = {
"poll-inboxes-every-minute": { "poll-inboxes-every-minute": (
{
"task": "app.tasks.imap_tasks.pull_all_inboxes", "task": "app.tasks.imap_tasks.pull_all_inboxes",
"schedule": crontab(minute="*/1"), # every 1 minute "schedule": crontab(minute="*/1"), # every 1 minute
"options": {"expires": 55}, # Ensure tasks don't pile up "options": {"expires": 55}, # Ensure tasks don't pile up
} if (settings.imap1_host or settings.imap2_host) else None, }
if (settings.imap1_host or settings.imap2_host)
else None
),
# Add Uptime Kuma ping task if configured # Add Uptime Kuma ping task if configured
"ping-uptime-kuma": { "ping-uptime-kuma": (
{
"task": "app.tasks.uptime_kuma_tasks.ping_uptime_kuma", "task": "app.tasks.uptime_kuma_tasks.ping_uptime_kuma",
"schedule": crontab(minute=f"*/{settings.uptime_kuma_ping_interval}"), "schedule": crontab(minute=f"*/{settings.uptime_kuma_ping_interval}"),
"options": {"expires": 55}, # Ensure tasks don't pile up "options": {"expires": 55}, # Ensure tasks don't pile up
} if settings.uptime_kuma_url else None, }
if settings.uptime_kuma_url
else None
),
# Check credentials every 5 minutes # Check credentials every 5 minutes
"check-credentials-regularly": { "check-credentials-regularly": {
"task": "app.tasks.check_credentials.check_credentials", "task": "app.tasks.check_credentials.check_credentials",
@@ -71,7 +78,7 @@ celery.conf.beat_schedule = {
"task": "app.tasks.check_credentials.check_credentials", "task": "app.tasks.check_credentials.check_credentials",
"schedule": crontab(hour="0", minute="0"), # Midnight "schedule": crontab(hour="0", minute="0"), # Midnight
"options": {"expires": 3600}, # 1 hour expiry "options": {"expires": 3600}, # 1 hour expiry
} },
} }
# Remove None entries from beat_schedule # Remove None entries from beat_schedule
+1 -2
View File
@@ -1,8 +1,7 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import os import os
from datetime import datetime from typing import Any, List, Optional, Union
from typing import Any, Dict, List, Optional, Union
from pydantic import Field, validator from pydantic import Field, validator
from pydantic_settings import BaseSettings from pydantic_settings import BaseSettings
+2 -2
View File
@@ -1,12 +1,12 @@
# app/database.py # app/database.py
import os
import logging import logging
import os
from sqlalchemy import create_engine, exc from sqlalchemy import create_engine, exc
from sqlalchemy.engine.url import make_url
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker from sqlalchemy.orm import sessionmaker
from sqlalchemy.engine.url import make_url
from app.config import settings from app.config import settings
+2 -2
View File
@@ -2,9 +2,9 @@
Frontend routes for the application. Frontend routes for the application.
This module is now a re-export of the modularized view routers. This module is now a re-export of the modularized view routers.
""" """
# Import and re-export the router from the views package # Import and re-export the router from the views package
from app.views import router from app.views import router # noqa: F401
# Keep the original router name for compatibility # Keep the original router name for compatibility
# This allows existing imports in main.py to continue working # This allows existing imports in main.py to continue working
+26 -34
View File
@@ -1,6 +1,6 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import os
import logging import logging
import os
import pathlib import pathlib
from contextlib import asynccontextmanager from contextlib import asynccontextmanager
@@ -8,21 +8,20 @@ from fastapi import FastAPI, HTTPException, Request, status
from fastapi.responses import JSONResponse from fastapi.responses import JSONResponse
from fastapi.staticfiles import StaticFiles from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates from fastapi.templating import Jinja2Templates
from starlette.middleware.sessions import SessionMiddleware
from starlette.config import Config from starlette.config import Config
from starlette.middleware.sessions import SessionMiddleware
from starlette.middleware.trustedhost import TrustedHostMiddleware from starlette.middleware.trustedhost import TrustedHostMiddleware
from uvicorn.middleware.proxy_headers import ProxyHeadersMiddleware from uvicorn.middleware.proxy_headers import ProxyHeadersMiddleware
from pathlib import Path
from app.database import init_db from app.api import router as api_router
from app.auth import router as auth_router
from app.config import settings from app.config import settings
from app.database import init_db
from app.utils.config_validator import check_all_configs from app.utils.config_validator import check_all_configs
from app.utils.notification import init_apprise, send_notification, notify_startup, notify_shutdown from app.utils.notification import init_apprise, notify_shutdown, notify_startup
# Import the routers - now using views directly instead of frontend # Import the routers - now using views directly instead of frontend
from app.views import router as frontend_router from app.views import router as frontend_router
from app.api import router as api_router
from app.auth import router as auth_router
# Explicitly include the files router # Explicitly include the files router
from app.views.files import router as files_router from app.views.files import router as files_router
@@ -32,8 +31,13 @@ config = Config(".env")
# Use settings.session_secret which has proper validation # Use settings.session_secret which has proper validation
# Fallback to raising an error if not set when auth is enabled # Fallback to raising an error if not set when auth is enabled
if settings.auth_enabled and not settings.session_secret: if settings.auth_enabled and not settings.session_secret:
raise ValueError("SESSION_SECRET must be set when AUTH_ENABLED=True. Generate one with: python -c 'import secrets; print(secrets.token_hex(32))'") raise ValueError(
SESSION_SECRET = settings.session_secret or "INSECURE_DEFAULT_FOR_DEVELOPMENT_ONLY_DO_NOT_USE_IN_PRODUCTION_MINIMUM_32_CHARS" "SESSION_SECRET must be set when AUTH_ENABLED=True. "
"Generate one with: python -c 'import secrets; print(secrets.token_hex(32))'"
)
SESSION_SECRET = (
settings.session_secret or "INSECURE_DEFAULT_FOR_DEVELOPMENT_ONLY_DO_NOT_USE_IN_PRODUCTION_MINIMUM_32_CHARS"
)
@asynccontextmanager @asynccontextmanager
@@ -60,14 +64,15 @@ async def lifespan(app: FastAPI):
# Force settings dump to log for troubleshooting # Force settings dump to log for troubleshooting
from app.utils.config_validator import dump_all_settings from app.utils.config_validator import dump_all_settings
dump_all_settings() dump_all_settings()
# Validate configuration # Validate configuration
config_issues = check_all_configs() config_issues = check_all_configs()
# Log overall status # Log overall status
has_issues = any(config_issues['email']) or any( has_issues = any(config_issues["email"]) or any(
len(issues) > 0 for provider, issues in config_issues['storage'].items() len(issues) > 0 for provider, issues in config_issues["storage"].items()
) )
if has_issues: if has_issues:
logging.warning("Application started with configuration issues - some features may be unavailable") logging.warning("Application started with configuration issues - some features may be unavailable")
@@ -101,11 +106,7 @@ app.add_middleware(SessionMiddleware, secret_key=SESSION_SECRET)
app.add_middleware(ProxyHeadersMiddleware, trusted_hosts="*") app.add_middleware(ProxyHeadersMiddleware, trusted_hosts="*")
# 3) (Optional but recommended) Restrict valid hosts: # 3) (Optional but recommended) Restrict valid hosts:
app.add_middleware(TrustedHostMiddleware, allowed_hosts=[ app.add_middleware(TrustedHostMiddleware, allowed_hosts=[settings.external_hostname, "localhost", "127.0.0.1"])
settings.external_hostname,
"localhost",
"127.0.0.1"
])
# Mount the static files directory # Mount the static files directory
static_dir = pathlib.Path(__file__).parents[1] / "frontend" / "static" static_dir = pathlib.Path(__file__).parents[1] / "frontend" / "static"
@@ -114,6 +115,7 @@ if os.path.exists(static_dir):
else: else:
print(f"WARNING: Static directory not found at {static_dir}. Static files will not be served.") print(f"WARNING: Static directory not found at {static_dir}. Static files will not be served.")
# Custom exception handlers that return JSON for API routes and HTML for frontend routes # Custom exception handlers that return JSON for API routes and HTML for frontend routes
@app.exception_handler(HTTPException) @app.exception_handler(HTTPException)
async def http_exception_handler(request: Request, exc: HTTPException): async def http_exception_handler(request: Request, exc: HTTPException):
@@ -123,30 +125,24 @@ async def http_exception_handler(request: Request, exc: HTTPException):
""" """
# For API routes, always return JSON # For API routes, always return JSON
if request.url.path.startswith("/api/"): if request.url.path.startswith("/api/"):
return JSONResponse( return JSONResponse(status_code=exc.status_code, content={"detail": exc.detail})
status_code=exc.status_code,
content={"detail": exc.detail}
)
# For frontend routes, return appropriate HTML templates # For frontend routes, return appropriate HTML templates
templates = Jinja2Templates(directory=str(static_dir.parent / "templates")) templates = Jinja2Templates(directory=str(static_dir.parent / "templates"))
# Handle 404 errors with a custom template # Handle 404 errors with a custom template
if exc.status_code == 404: if exc.status_code == 404:
return templates.TemplateResponse( return templates.TemplateResponse("404.html", {"request": request}, status_code=status.HTTP_404_NOT_FOUND)
"404.html",
{"request": request},
status_code=status.HTTP_404_NOT_FOUND
)
# For other HTTP errors, we could create specific templates or use a generic one # For other HTTP errors, we could create specific templates or use a generic one
# For now, return a simple error page # For now, return a simple error page
return templates.TemplateResponse( return templates.TemplateResponse(
"404.html", # Reuse 404 template for other errors, or create a generic error template "404.html", # Reuse 404 template for other errors, or create a generic error template
{"request": request}, {"request": request},
status_code=exc.status_code status_code=exc.status_code,
) )
@app.exception_handler(500) @app.exception_handler(500)
async def custom_500_handler(request: Request, exc: Exception): async def custom_500_handler(request: Request, exc: Exception):
""" """
@@ -156,27 +152,23 @@ async def custom_500_handler(request: Request, exc: Exception):
# For API routes, return JSON instead of HTML # For API routes, return JSON instead of HTML
if request.url.path.startswith("/api/"): if request.url.path.startswith("/api/"):
return JSONResponse( return JSONResponse(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, content={"detail": "Internal server error"}
content={"detail": "Internal server error"}
) )
# Serve the 500 template for non-API routes # Serve the 500 template for non-API routes
templates = Jinja2Templates(directory=str(static_dir.parent / "templates")) templates = Jinja2Templates(directory=str(static_dir.parent / "templates"))
return templates.TemplateResponse( return templates.TemplateResponse(
"500.html", "500.html", {"request": request, "exc": exc}, status_code=status.HTTP_500_INTERNAL_SERVER_ERROR
{"request": request, "exc": exc},
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR
) )
@app.get("/test-500") @app.get("/test-500")
def test_500(): def test_500():
raise RuntimeError("Testing forced 500 error!") raise RuntimeError("Testing forced 500 error!")
# Include the routers # Include the routers
app.include_router(frontend_router) app.include_router(frontend_router)
app.include_router(files_router) # Explicitly include the files router app.include_router(files_router) # Explicitly include the files router
app.include_router(auth_router) app.include_router(auth_router)
app.include_router(api_router, prefix="/api") app.include_router(api_router, prefix="/api")
+7 -3
View File
@@ -1,10 +1,10 @@
# app/models.py # app/models.py
#!/usr/bin/env python3
from sqlalchemy import Column, String, Integer, DateTime, func, ForeignKey from sqlalchemy import Column, DateTime, ForeignKey, Integer, String, func
from sqlalchemy.ext.declarative import declarative_base
from app.database import Base from app.database import Base
class DocumentMetadata(Base): class DocumentMetadata(Base):
__tablename__ = "documents" __tablename__ = "documents"
@@ -15,6 +15,7 @@ class DocumentMetadata(Base):
tags = Column(String) tags = Column(String)
summary = Column(String) summary = Column(String)
class FileRecord(Base): class FileRecord(Base):
__tablename__ = "files" __tablename__ = "files"
@@ -38,6 +39,7 @@ class FileRecord(Base):
# Timestamp when we inserted this record # Timestamp when we inserted this record
created_at = Column(DateTime(timezone=True), server_default=func.now()) created_at = Column(DateTime(timezone=True), server_default=func.now())
class ProcessingLog(Base): class ProcessingLog(Base):
__tablename__ = "processing_logs" __tablename__ = "processing_logs"
id = Column(Integer, primary_key=True, index=True) id = Column(Integer, primary_key=True, index=True)
@@ -48,8 +50,10 @@ class ProcessingLog(Base):
message = Column(String, nullable=True) # Error text or success note message = Column(String, nullable=True) # Error text or success note
timestamp = Column(DateTime(timezone=True), server_default=func.now()) timestamp = Column(DateTime(timezone=True), server_default=func.now())
class ApplicationSettings(Base): class ApplicationSettings(Base):
"""Store application settings in database with precedence over environment variables""" """Store application settings in database with precedence over environment variables"""
__tablename__ = "application_settings" __tablename__ = "application_settings"
id = Column(Integer, primary_key=True, index=True) id = Column(Integer, primary_key=True, index=True)
+4 -3
View File
@@ -1,10 +1,11 @@
from fastapi import APIRouter, HTTPException
from fastapi.responses import PlainTextResponse, HTMLResponse
from pathlib import Path from pathlib import Path
import os
from fastapi import APIRouter, HTTPException
from fastapi.responses import PlainTextResponse
router = APIRouter() router = APIRouter()
@router.get("/licenses/lgpl.txt", response_class=PlainTextResponse) @router.get("/licenses/lgpl.txt", response_class=PlainTextResponse)
async def get_lgpl_license(): async def get_lgpl_license():
""" """
+2 -2
View File
@@ -1,3 +1,3 @@
# Import tasks so they can be discovered by Celery # Import tasks so they can be discovered by Celery
from app.tasks.process_document import process_document from app.tasks.process_document import process_document # noqa: F401
from app.tasks.process_with_azure_document_intelligence import process_with_azure_document_intelligence from app.tasks.process_with_azure_document_intelligence import process_with_azure_document_intelligence # noqa: F401
+48 -36
View File
@@ -1,26 +1,29 @@
import logging
from app.celery_app import celery
from app.config import settings
from app.utils.notification import notify_credential_failure
import time
import os
import json
import asyncio import asyncio
import inspect import inspect
import json
import logging
import os
import time
# Import the test functions from API routes
from app.api.openai import test_openai_connection
from app.api.azure import test_azure_connection from app.api.azure import test_azure_connection
from app.api.dropbox import test_dropbox_token from app.api.dropbox import test_dropbox_token
from app.api.google_drive import test_google_drive_token from app.api.google_drive import test_google_drive_token
from app.api.onedrive import test_onedrive_token from app.api.onedrive import test_onedrive_token
# Import the test functions from API routes
from app.api.openai import test_openai_connection
from app.celery_app import celery
from app.config import settings
# Import config validation utilities # Import config validation utilities
from app.utils.config_validator import validate_storage_configs, get_provider_status from app.utils.config_validator import get_provider_status, validate_storage_configs
from app.utils.notification import notify_credential_failure
# Create an enhanced mock Request object for API functions that expect it # Create an enhanced mock Request object for API functions that expect it
class MockRequest: class MockRequest:
"""Mock request object with session and other attributes needed for API functions""" """Mock request object with session and other attributes needed for API functions"""
def __init__(self): def __init__(self):
self.session = {"user": {"id": "credential_checker", "name": "System Credential Checker"}} self.session = {"user": {"id": "credential_checker", "name": "System Credential Checker"}}
self.app = None self.app = None
@@ -34,16 +37,18 @@ class MockRequest:
async def form(self): async def form(self):
return {} return {}
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
# Path to store failure counts # Path to store failure counts
FAILURE_STATE_FILE = os.path.join(settings.workdir, 'credential_failures.json') FAILURE_STATE_FILE = os.path.join(settings.workdir, "credential_failures.json")
def get_failure_state(): def get_failure_state():
"""Read the failure state from file""" """Read the failure state from file"""
try: try:
if os.path.exists(FAILURE_STATE_FILE): if os.path.exists(FAILURE_STATE_FILE):
with open(FAILURE_STATE_FILE, 'r') as f: with open(FAILURE_STATE_FILE, "r") as f:
return json.load(f) return json.load(f)
except Exception as e: except Exception as e:
logger.error(f"Error reading failure state file: {e}") logger.error(f"Error reading failure state file: {e}")
@@ -51,14 +56,16 @@ def get_failure_state():
# Default empty state # Default empty state
return {} return {}
def save_failure_state(state): def save_failure_state(state):
"""Save failure state to file""" """Save failure state to file"""
try: try:
with open(FAILURE_STATE_FILE, 'w') as f: with open(FAILURE_STATE_FILE, "w") as f:
json.dump(state, f) json.dump(state, f)
except Exception as e: except Exception as e:
logger.error(f"Error saving failure state file: {e}") logger.error(f"Error saving failure state file: {e}")
# Helper function to get the inner function without the decorator # Helper function to get the inner function without the decorator
def unwrap_decorated_function(func): def unwrap_decorated_function(func):
"""Get the original function from a decorated function""" """Get the original function from a decorated function"""
@@ -66,6 +73,7 @@ def unwrap_decorated_function(func):
return unwrap_decorated_function(func.__wrapped__) return unwrap_decorated_function(func.__wrapped__)
return func return func
# Create synchronous versions of the test functions that bypass authentication # Create synchronous versions of the test functions that bypass authentication
def sync_test_openai_connection(): def sync_test_openai_connection():
"""Synchronous wrapper for the OpenAI test function that bypasses auth""" """Synchronous wrapper for the OpenAI test function that bypasses auth"""
@@ -76,6 +84,7 @@ def sync_test_openai_connection():
return asyncio.run(inner_func(request)) return asyncio.run(inner_func(request))
return inner_func(request) return inner_func(request)
def sync_test_azure_connection(): def sync_test_azure_connection():
"""Synchronous wrapper for the Azure test function that bypasses auth""" """Synchronous wrapper for the Azure test function that bypasses auth"""
inner_func = unwrap_decorated_function(test_azure_connection) inner_func = unwrap_decorated_function(test_azure_connection)
@@ -84,6 +93,7 @@ def sync_test_azure_connection():
return asyncio.run(inner_func(request)) return asyncio.run(inner_func(request))
return inner_func(request) return inner_func(request)
def sync_test_dropbox_token(): def sync_test_dropbox_token():
"""Synchronous wrapper for the Dropbox test function that bypasses auth""" """Synchronous wrapper for the Dropbox test function that bypasses auth"""
inner_func = unwrap_decorated_function(test_dropbox_token) inner_func = unwrap_decorated_function(test_dropbox_token)
@@ -92,6 +102,7 @@ def sync_test_dropbox_token():
return asyncio.run(inner_func(request)) return asyncio.run(inner_func(request))
return inner_func(request) return inner_func(request)
def sync_test_google_drive_token(): def sync_test_google_drive_token():
"""Synchronous wrapper for the Google Drive test function that bypasses auth""" """Synchronous wrapper for the Google Drive test function that bypasses auth"""
inner_func = unwrap_decorated_function(test_google_drive_token) inner_func = unwrap_decorated_function(test_google_drive_token)
@@ -100,6 +111,7 @@ def sync_test_google_drive_token():
return asyncio.run(inner_func(request)) return asyncio.run(inner_func(request))
return inner_func(request) return inner_func(request)
def sync_test_onedrive_token(): def sync_test_onedrive_token():
"""Synchronous wrapper for the OneDrive test function that bypasses auth""" """Synchronous wrapper for the OneDrive test function that bypasses auth"""
inner_func = unwrap_decorated_function(test_onedrive_token) inner_func = unwrap_decorated_function(test_onedrive_token)
@@ -108,6 +120,7 @@ def sync_test_onedrive_token():
return asyncio.run(inner_func(request)) return asyncio.run(inner_func(request))
return inner_func(request) return inner_func(request)
@celery.task @celery.task
def check_credentials(): def check_credentials():
"""Check all configured credentials and notify if any are invalid""" """Check all configured credentials and notify if any are invalid"""
@@ -129,32 +142,32 @@ def check_credentials():
"name": "OpenAI", "name": "OpenAI",
"check_func": sync_test_openai_connection, "check_func": sync_test_openai_connection,
"configured": provider_status.get("OpenAI", {}).get("configured", False), "configured": provider_status.get("OpenAI", {}).get("configured", False),
"config_issues": [] # OpenAI isn't in storage_configs "config_issues": [], # OpenAI isn't in storage_configs
}, },
{ {
"name": "Azure Document Intelligence", "name": "Azure Document Intelligence",
"check_func": sync_test_azure_connection, "check_func": sync_test_azure_connection,
"configured": provider_status.get("Azure AI", {}).get("configured", False), "configured": provider_status.get("Azure AI", {}).get("configured", False),
"config_issues": [] # Azure isn't in storage_configs "config_issues": [], # Azure isn't in storage_configs
}, },
{ {
"name": "Dropbox", "name": "Dropbox",
"check_func": sync_test_dropbox_token, "check_func": sync_test_dropbox_token,
"configured": provider_status.get("Dropbox", {}).get("configured", False), "configured": provider_status.get("Dropbox", {}).get("configured", False),
"config_issues": storage_configs.get("dropbox", []) "config_issues": storage_configs.get("dropbox", []),
}, },
{ {
"name": "Google Drive", "name": "Google Drive",
"check_func": sync_test_google_drive_token, "check_func": sync_test_google_drive_token,
"configured": provider_status.get("Google Drive", {}).get("configured", False), "configured": provider_status.get("Google Drive", {}).get("configured", False),
"config_issues": storage_configs.get("google_drive", []) "config_issues": storage_configs.get("google_drive", []),
}, },
{ {
"name": "OneDrive", "name": "OneDrive",
"check_func": sync_test_onedrive_token, "check_func": sync_test_onedrive_token,
"configured": provider_status.get("OneDrive", {}).get("configured", False), "configured": provider_status.get("OneDrive", {}).get("configured", False),
"config_issues": storage_configs.get("onedrive", []) "config_issues": storage_configs.get("onedrive", []),
} },
] ]
# Check each service # Check each service
@@ -168,13 +181,10 @@ def check_credentials():
# Skip services that aren't configured # Skip services that aren't configured
if not service["configured"]: if not service["configured"]:
config_issues = service["config_issues"] config_issues = service["config_issues"]
issue_msg = f"Not properly configured" + (f": {', '.join(config_issues)}" if config_issues else "") issue_msg = "Not properly configured" + (f": {', '.join(config_issues)}" if config_issues else "")
logger.info(f"Skipping {service_name}: {issue_msg}") logger.info(f"Skipping {service_name}: {issue_msg}")
results[service_name] = { results[service_name] = {"status": "unconfigured", "message": issue_msg}
"status": "unconfigured",
"message": issue_msg
}
continue continue
try: try:
@@ -186,10 +196,7 @@ def check_credentials():
error_message = result.get("message", "Unknown error") error_message = result.get("message", "Unknown error")
# Store the result # Store the result
results[service_name] = { results[service_name] = {"status": "valid" if is_valid else "invalid", "message": error_message}
"status": "valid" if is_valid else "invalid",
"message": error_message
}
if not is_valid: if not is_valid:
failures.append(service_name) failures.append(service_name)
@@ -204,10 +211,15 @@ def check_credentials():
notify_credential_failure(service_name, error_message) notify_credential_failure(service_name, error_message)
service_state["last_notified"] = current_time service_state["last_notified"] = current_time
service_state["recovered"] = False service_state["recovered"] = False
logger.warning(f"{service_name} credentials check failed ({service_state['count']} times): {error_message}") logger.warning(
f"{service_name} credentials check failed ({service_state['count']} times): {error_message}"
)
else: else:
# We're in cooldown mode # We're in cooldown mode
logger.warning(f"{service_name} credentials check failed ({service_state['count']} times): {error_message} - notification suppressed") logger.warning(
f"{service_name} credentials check failed ({service_state['count']} times): "
f"{error_message} - notification suppressed"
)
# Update failure state # Update failure state
failure_state[service_name] = service_state failure_state[service_name] = service_state
@@ -243,10 +255,7 @@ def check_credentials():
failure_state[service_name] = service_state failure_state[service_name] = service_state
# Store the error result # Store the error result
results[service_name] = { results[service_name] = {"status": "error", "message": error_message}
"status": "error",
"message": error_message
}
# Save updated failure state # Save updated failure state
save_failure_state(failure_state) save_failure_state(failure_state)
@@ -256,12 +265,15 @@ def check_credentials():
num_configured = len(configured_services) num_configured = len(configured_services)
# Summarize results # Summarize results
logger.info(f"Credential check completed. Configured services: {num_configured}, Valid: {num_configured - len(failures)}, Invalid: {len(failures)}") logger.info(
f"Credential check completed. Configured services: {num_configured}, "
f"Valid: {num_configured - len(failures)}, Invalid: {len(failures)}"
)
return { return {
"checked": num_configured, "checked": num_configured,
"unconfigured": len(services) - num_configured, "unconfigured": len(services) - num_configured,
"failures": len(failures), "failures": len(failures),
"results": results, "results": results,
"failure_state": failure_state "failure_state": failure_state,
} }
+41 -17
View File
@@ -1,20 +1,21 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import json
import logging
import os import os
import shutil import shutil
import tempfile import tempfile
import logging
import PyPDF2 # Replace fitz with PyPDF2 import PyPDF2 # Replace fitz with PyPDF2
import json
from app.config import settings
from app.tasks.retry_config import BaseTaskWithRetry
from app.tasks.finalize_document_storage import finalize_document_storage
# Import the shared Celery instance # Import the shared Celery instance
from app.celery_app import celery from app.celery_app import celery
from app.utils import log_task_progress from app.config import settings
from app.database import SessionLocal from app.database import SessionLocal
from app.models import FileRecord from app.models import FileRecord
from app.tasks.finalize_document_storage import finalize_document_storage
from app.tasks.retry_config import BaseTaskWithRetry
from app.utils import log_task_progress
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -26,6 +27,7 @@ logger = logging.getLogger(__name__)
TMP_SUBDIR = "tmp" TMP_SUBDIR = "tmp"
PROCESSED_SUBDIR = "processed" PROCESSED_SUBDIR = "processed"
def unique_filepath(directory, base_filename, extension=".pdf"): def unique_filepath(directory, base_filename, extension=".pdf"):
""" """
Returns a unique filepath in the specified directory. Returns a unique filepath in the specified directory.
@@ -41,6 +43,7 @@ def unique_filepath(directory, base_filename, extension=".pdf"):
return candidate return candidate
counter += 1 counter += 1
def persist_metadata(metadata, final_pdf_path): def persist_metadata(metadata, final_pdf_path):
""" """
Saves the metadata dictionary to a JSON file with the same base name as the final PDF. Saves the metadata dictionary to a JSON file with the same base name as the final PDF.
@@ -53,6 +56,7 @@ def persist_metadata(metadata, final_pdf_path):
json.dump(metadata, f, ensure_ascii=False, indent=2) json.dump(metadata, f, ensure_ascii=False, indent=2)
return json_path return json_path
@celery.task(base=BaseTaskWithRetry, bind=True) @celery.task(base=BaseTaskWithRetry, bind=True)
def embed_metadata_into_pdf(self, local_file_path: str, extracted_text: str, metadata: dict, file_id: int = None): def embed_metadata_into_pdf(self, local_file_path: str, extracted_text: str, metadata: dict, file_id: int = None):
""" """
@@ -70,7 +74,13 @@ def embed_metadata_into_pdf(self, local_file_path: str, extracted_text: str, met
""" """
task_id = self.request.id task_id = self.request.id
logger.info(f"[{task_id}] Starting metadata embedding for: {local_file_path}") logger.info(f"[{task_id}] Starting metadata embedding for: {local_file_path}")
log_task_progress(task_id, "embed_metadata_into_pdf", "in_progress", f"Embedding metadata into {os.path.basename(local_file_path)}", file_id=file_id) log_task_progress(
task_id,
"embed_metadata_into_pdf",
"in_progress",
f"Embedding metadata into {os.path.basename(local_file_path)}",
file_id=file_id,
)
# Get file_id from database if not provided (fallback only, prefer passing file_id explicitly) # Get file_id from database if not provided (fallback only, prefer passing file_id explicitly)
if file_id is None: if file_id is None:
@@ -93,7 +103,7 @@ def embed_metadata_into_pdf(self, local_file_path: str, extracted_text: str, met
original_file = local_file_path original_file = local_file_path
# Create a temporary file with the same extension as the original # Create a temporary file with the same extension as the original
_, ext = os.path.splitext(local_file_path) _, ext = os.path.splitext(local_file_path)
tmp_file = tempfile.NamedTemporaryFile(mode='wb', suffix=ext, prefix='processed_', delete=False) tmp_file = tempfile.NamedTemporaryFile(mode="wb", suffix=ext, prefix="processed_", delete=False)
processed_file = tmp_file.name processed_file = tmp_file.name
tmp_file.close() tmp_file.close()
@@ -105,7 +115,7 @@ def embed_metadata_into_pdf(self, local_file_path: str, extracted_text: str, met
log_task_progress(task_id, "modify_pdf", "in_progress", "Modifying PDF metadata", file_id=file_id) log_task_progress(task_id, "modify_pdf", "in_progress", "Modifying PDF metadata", file_id=file_id)
# Open the PDF and modify metadata # Open the PDF and modify metadata
with open(processed_file, 'rb') as file: with open(processed_file, "rb") as file:
pdf_reader = PyPDF2.PdfReader(file) pdf_reader = PyPDF2.PdfReader(file)
pdf_writer = PyPDF2.PdfWriter() pdf_writer = PyPDF2.PdfWriter()
@@ -114,15 +124,17 @@ def embed_metadata_into_pdf(self, local_file_path: str, extracted_text: str, met
pdf_writer.add_page(page) pdf_writer.add_page(page)
# Set PDF metadata # Set PDF metadata
pdf_writer.add_metadata({ pdf_writer.add_metadata(
{
"/Title": metadata.get("filename", "Unknown Document"), "/Title": metadata.get("filename", "Unknown Document"),
"/Author": metadata.get("absender", "Unknown"), "/Author": metadata.get("absender", "Unknown"),
"/Subject": metadata.get("document_type", "Unknown"), "/Subject": metadata.get("document_type", "Unknown"),
"/Keywords": ", ".join(metadata.get("tags", [])) "/Keywords": ", ".join(metadata.get("tags", [])),
}) }
)
# Write the modified PDF # Write the modified PDF
with open(processed_file, 'wb') as output_file: with open(processed_file, "wb") as output_file:
pdf_writer.write(output_file) pdf_writer.write(output_file)
logger.info(f"[{task_id}] Metadata embedded successfully in {processed_file}") logger.info(f"[{task_id}] Metadata embedded successfully in {processed_file}")
@@ -139,24 +151,36 @@ def embed_metadata_into_pdf(self, local_file_path: str, extracted_text: str, met
final_file_path = unique_filepath(final_dir, suggested_filename, extension=".pdf") final_file_path = unique_filepath(final_dir, suggested_filename, extension=".pdf")
logger.info(f"[{task_id}] Moving file to: {final_file_path}") logger.info(f"[{task_id}] Moving file to: {final_file_path}")
log_task_progress(task_id, "move_to_processed", "in_progress", f"Moving to processed: {suggested_filename}.pdf", file_id=file_id) log_task_progress(
task_id,
"move_to_processed",
"in_progress",
f"Moving to processed: {suggested_filename}.pdf",
file_id=file_id,
)
# Move the processed file using shutil.move to handle cross-device moves. # Move the processed file using shutil.move to handle cross-device moves.
shutil.move(processed_file, final_file_path) shutil.move(processed_file, final_file_path)
# Ensure the temporary file is deleted if it still exists. # Ensure the temporary file is deleted if it still exists.
if os.path.exists(processed_file): if os.path.exists(processed_file):
os.remove(processed_file) os.remove(processed_file)
log_task_progress(task_id, "move_to_processed", "success", f"Moved to: {os.path.basename(final_file_path)}", file_id=file_id) log_task_progress(
task_id, "move_to_processed", "success", f"Moved to: {os.path.basename(final_file_path)}", file_id=file_id
)
# Persist the metadata into a JSON file with the same base name. # Persist the metadata into a JSON file with the same base name.
logger.info(f"[{task_id}] Persisting metadata to JSON") logger.info(f"[{task_id}] Persisting metadata to JSON")
log_task_progress(task_id, "save_metadata_json", "in_progress", "Saving metadata JSON", file_id=file_id) log_task_progress(task_id, "save_metadata_json", "in_progress", "Saving metadata JSON", file_id=file_id)
json_path = persist_metadata(metadata, final_file_path) json_path = persist_metadata(metadata, final_file_path)
logger.info(f"[{task_id}] Metadata persisted to {json_path}") logger.info(f"[{task_id}] Metadata persisted to {json_path}")
log_task_progress(task_id, "save_metadata_json", "success", f"Saved: {os.path.basename(json_path)}", file_id=file_id) log_task_progress(
task_id, "save_metadata_json", "success", f"Saved: {os.path.basename(json_path)}", file_id=file_id
)
# Trigger the next step: final storage. # Trigger the next step: final storage.
logger.info(f"[{task_id}] Queueing final storage task") logger.info(f"[{task_id}] Queueing final storage task")
log_task_progress(task_id, "embed_metadata_into_pdf", "success", "Metadata embedded, queuing finalization", file_id=file_id) log_task_progress(
task_id, "embed_metadata_into_pdf", "success", "Metadata embedded, queuing finalization", file_id=file_id
)
finalize_document_storage.delay(original_file, final_file_path, metadata, file_id=file_id) finalize_document_storage.delay(original_file, final_file_path, metadata, file_id=file_id)
# After triggering final storage, delete the original file if it is in workdir/tmp. # After triggering final storage, delete the original file if it is in workdir/tmp.
+57 -48
View File
@@ -1,33 +1,32 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import json import json
import re import logging
import os import os
from app.config import settings import re
from app.tasks.retry_config import BaseTaskWithRetry
from app.tasks.embed_metadata_into_pdf import embed_metadata_into_pdf import openai
# Import the shared Celery instance # Import the shared Celery instance
from app.celery_app import celery from app.celery_app import celery
import openai from app.config import settings
import logging
from app.utils import log_task_progress
from app.database import SessionLocal from app.database import SessionLocal
from app.models import FileRecord from app.models import FileRecord
from app.tasks.embed_metadata_into_pdf import embed_metadata_into_pdf
from app.tasks.retry_config import BaseTaskWithRetry
from app.utils import log_task_progress
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
# Initialize OpenAI client dynamically with better error handling # Initialize OpenAI client dynamically with better error handling
try: try:
client = openai.OpenAI( client = openai.OpenAI(api_key=settings.openai_api_key, base_url=settings.openai_base_url)
api_key=settings.openai_api_key,
base_url=settings.openai_base_url
)
logger.info("OpenAI client initialized successfully") logger.info("OpenAI client initialized successfully")
except Exception as e: except Exception as e:
logger.error(f"Failed to initialize OpenAI client: {e}") logger.error(f"Failed to initialize OpenAI client: {e}")
client = None client = None
def extract_json_from_text(text): def extract_json_from_text(text):
""" """
Try to extract a JSON object from the text. Try to extract a JSON object from the text.
@@ -45,12 +44,15 @@ def extract_json_from_text(text):
return text[start : end + 1] return text[start : end + 1]
return None return None
@celery.task(base=BaseTaskWithRetry, bind=True) @celery.task(base=BaseTaskWithRetry, bind=True)
def extract_metadata_with_gpt(self, filename: str, cleaned_text: str, file_id: int = None): def extract_metadata_with_gpt(self, filename: str, cleaned_text: str, file_id: int = None):
"""Uses OpenAI to classify document metadata.""" """Uses OpenAI to classify document metadata."""
task_id = self.request.id task_id = self.request.id
logger.info(f"[{task_id}] Starting metadata extraction for: {filename}") logger.info(f"[{task_id}] Starting metadata extraction for: {filename}")
log_task_progress(task_id, "extract_metadata_with_gpt", "in_progress", f"Extracting metadata for {filename}", file_id=file_id) log_task_progress(
task_id, "extract_metadata_with_gpt", "in_progress", f"Extracting metadata for {filename}", file_id=file_id
)
# Get file_id from database if not provided # Get file_id from database if not provided
if file_id is None: if file_id is None:
@@ -62,37 +64,38 @@ def extract_metadata_with_gpt(self, filename: str, cleaned_text: str, file_id: i
if file_record: if file_record:
file_id = file_record.id file_id = file_record.id
prompt = f""" prompt = (
You are a specialized document analyzer trained to extract structured metadata from documents. "You are a specialized document analyzer trained to extract structured metadata from documents.\n"
Your task is to analyze the given text and return a well-structured JSON object. "Your task is to analyze the given text and return a well-structured JSON object.\n\n"
"Extract and return the following fields:\n"
Extract and return the following fields: "1. **filename**: Machine-readable filename "
1. **filename**: Machine-readable filename (YYYY-MM-DD_DescriptiveTitle, use only letters, numbers, periods, and underscores). "(YYYY-MM-DD_DescriptiveTitle, use only letters, numbers, periods, and underscores).\n"
2. **empfaenger**: The recipient, or "Unknown" if not found. "2. **empfaenger**: The recipient, or \"Unknown\" if not found.\n"
3. **absender**: The sender, or "Unknown" if not found. "3. **absender**: The sender, or \"Unknown\" if not found.\n"
4. **correspondent**: The entity or company that issued the document (shortest possible name, e.g., "Amazon" instead of "Amazon EU SARL, German branch"). "4. **correspondent**: The entity or company that issued the document "
5. **kommunikationsart**: One of [Behoerdlicher_Brief, Rechnung, Kontoauszug, Vertrag, Quittung, Privater_Brief, Einladung, Gewerbliche_Korrespondenz, Newsletter, Werbung, Sonstiges]. "(shortest possible name, e.g., \"Amazon\" instead of \"Amazon EU SARL, German branch\").\n"
6. **kommunikationskategorie**: One of [Amtliche_Postbehoerdliche_Dokumente, Finanz_und_Vertragsdokumente, Geschaeftliche_Kommunikation, Private_Korrespondenz, Sonstige_Informationen]. "5. **kommunikationsart**: One of [Behoerdlicher_Brief, Rechnung, Kontoauszug, Vertrag, "
7. **document_type**: Precise classification (e.g., Invoice, Contract, Information, Unknown). "Quittung, Privater_Brief, Einladung, Gewerbliche_Korrespondenz, Newsletter, Werbung, Sonstiges].\n"
8. **tags**: A list of up to 4 relevant thematic keywords. "6. **kommunikationskategorie**: One of [Amtliche_Postbehoerdliche_Dokumente, "
9. **language**: Detected document language (ISO 639-1 code, e.g., "de" or "en"). "Finanz_und_Vertragsdokumente, Geschaeftliche_Kommunikation, "
10. **title**: A human-readable title summarizing the document content. "Private_Korrespondenz, Sonstige_Informationen].\n"
11. **confidence_score**: A numeric value (0-100) indicating the confidence level of the extracted metadata. "7. **document_type**: Precise classification (e.g., Invoice, Contract, Information, Unknown).\n"
12. **reference_number**: Extracted invoice/order/reference number if available. "8. **tags**: A list of up to 4 relevant thematic keywords.\n"
13. **monetary_amounts**: A list of key monetary values detected in the document. "9. **language**: Detected document language (ISO 639-1 code, e.g., \"de\" or \"en\").\n"
"10. **title**: A human-readable title summarizing the document content.\n"
### Important Rules: "11. **confidence_score**: A numeric value (0-100) indicating the confidence level "
- **OCR Correction**: Assume the text has been corrected for OCR errors. "of the extracted metadata.\n"
- **Tagging**: Max 4 tags, avoiding generic or overly specific terms. "12. **reference_number**: Extracted invoice/order/reference number if available.\n"
- **Title**: Concise, no addresses, and contains key identifying features. "13. **monetary_amounts**: A list of key monetary values detected in the document.\n\n"
- **Date Selection**: Use the most relevant date if multiple are found. "### Important Rules:\n"
- **Output Language**: Maintain the document's original language. "- **OCR Correction**: Assume the text has been corrected for OCR errors.\n"
"- **Tagging**: Max 4 tags, avoiding generic or overly specific terms.\n"
Extracted text: "- **Title**: Concise, no addresses, and contains key identifying features.\n"
{cleaned_text} "- **Date Selection**: Use the most relevant date if multiple are found.\n"
"- **Output Language**: Maintain the document's original language.\n\n"
Return only valid JSON with no additional commentary. f"Extracted text:\n{cleaned_text}\n\n"
""" "Return only valid JSON with no additional commentary.\n"
)
try: try:
logger.info(f"[{task_id}] Sending classification request for {filename}...") logger.info(f"[{task_id}] Sending classification request for {filename}...")
@@ -101,9 +104,9 @@ Return only valid JSON with no additional commentary.
model=settings.openai_model, model=settings.openai_model,
messages=[ messages=[
{"role": "system", "content": "You are an intelligent document classifier."}, {"role": "system", "content": "You are an intelligent document classifier."},
{"role": "user", "content": prompt} {"role": "user", "content": prompt},
], ],
temperature=0 temperature=0,
) )
content = completion.choices[0].message.content content = completion.choices[0].message.content
@@ -113,16 +116,22 @@ Return only valid JSON with no additional commentary.
json_text = extract_json_from_text(content) json_text = extract_json_from_text(content)
if not json_text: if not json_text:
logger.error(f"[{task_id}] Could not find valid JSON in GPT response for {filename}.") logger.error(f"[{task_id}] Could not find valid JSON in GPT response for {filename}.")
log_task_progress(task_id, "extract_metadata_with_gpt", "failure", "Invalid JSON in response", file_id=file_id) log_task_progress(
task_id, "extract_metadata_with_gpt", "failure", "Invalid JSON in response", file_id=file_id
)
return {} return {}
metadata = json.loads(json_text) metadata = json.loads(json_text)
logger.info(f"[{task_id}] Extracted metadata: {metadata}") logger.info(f"[{task_id}] Extracted metadata: {metadata}")
log_task_progress(task_id, "parse_metadata", "success", f"Parsed metadata: {list(metadata.keys())}", file_id=file_id) log_task_progress(
task_id, "parse_metadata", "success", f"Parsed metadata: {list(metadata.keys())}", file_id=file_id
)
# Trigger the next step: embedding metadata into the PDF # Trigger the next step: embedding metadata into the PDF
logger.info(f"[{task_id}] Queueing metadata embedding task") logger.info(f"[{task_id}] Queueing metadata embedding task")
log_task_progress(task_id, "extract_metadata_with_gpt", "success", "Metadata extracted, queuing embed task", file_id=file_id) log_task_progress(
task_id, "extract_metadata_with_gpt", "success", "Metadata extracted, queuing embed task", file_id=file_id
)
embed_metadata_into_pdf.delay(filename, cleaned_text, metadata, file_id) embed_metadata_into_pdf.delay(filename, cleaned_text, metadata, file_id)
return {"s3_file": filename, "metadata": metadata} return {"s3_file": filename, "metadata": metadata}
+23 -22
View File
@@ -2,21 +2,22 @@
import logging import logging
import os import os
from app.config import settings
from app.tasks.retry_config import BaseTaskWithRetry
# Import the shared Celery instance # Import the shared Celery instance
from app.celery_app import celery from app.celery_app import celery
from app.config import settings
from app.database import SessionLocal
from app.models import FileRecord
from app.tasks.retry_config import BaseTaskWithRetry
# Import the aggregator task and validator # Import the aggregator task and validator
from app.tasks.send_to_all import send_to_all_destinations, get_configured_services_from_validator from app.tasks.send_to_all import get_configured_services_from_validator, send_to_all_destinations
# Import notification utility
from app.utils.notification import notify_file_processed
# Import database and logging utils from main # Import database and logging utils from main
from app.utils import log_task_progress from app.utils import log_task_progress
from app.database import SessionLocal
from app.models import FileRecord # Import notification utility
from app.utils.notification import notify_file_processed
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -32,16 +33,20 @@ def finalize_document_storage(self, original_file: str, processed_file: str, met
logger.info(f"[{task_id}] Finalizing document storage for {processed_file}") logger.info(f"[{task_id}] Finalizing document storage for {processed_file}")
# 1. Update Database Status (From Main) # 1. Update Database Status (From Main)
log_task_progress(task_id, "finalize_document_storage", "in_progress", f"Finalizing: {os.path.basename(processed_file)}", file_id=file_id) log_task_progress(
task_id,
"finalize_document_storage",
"in_progress",
f"Finalizing: {os.path.basename(processed_file)}",
file_id=file_id,
)
# Get file_id from database if not provided (fallback logic from Main) # Get file_id from database if not provided (fallback logic from Main)
if file_id is None: if file_id is None:
with SessionLocal() as db: with SessionLocal() as db:
# Only as a last resort, try to find by exact match on local_filename # Only as a last resort, try to find by exact match on local_filename
tmp_path = os.path.join(settings.workdir, "tmp", os.path.basename(original_file)) tmp_path = os.path.join(settings.workdir, "tmp", os.path.basename(original_file))
file_record = db.query(FileRecord).filter( file_record = db.query(FileRecord).filter(FileRecord.local_filename == tmp_path).first()
FileRecord.local_filename == tmp_path
).first()
if file_record: if file_record:
file_id = file_record.id file_id = file_record.id
@@ -54,7 +59,7 @@ def finalize_document_storage(self, original_file: str, processed_file: str, met
for service_name, is_configured in configured_services.items(): for service_name, is_configured in configured_services.items():
if is_configured: if is_configured:
# Format service names for display # Format service names for display
display_name = service_name.replace('_', ' ').title() display_name = service_name.replace("_", " ").title()
configured_destinations.append(display_name) configured_destinations.append(display_name)
except Exception as e: except Exception as e:
logger.warning(f"[WARNING] Could not determine configured destinations: {e}") logger.warning(f"[WARNING] Could not determine configured destinations: {e}")
@@ -63,7 +68,9 @@ def finalize_document_storage(self, original_file: str, processed_file: str, met
# 3. Queue Uploads (Merged) # 3. Queue Uploads (Merged)
# Uses Main branch signature to ensure file_id is passed, but keeps logic structure # Uses Main branch signature to ensure file_id is passed, but keeps logic structure
logger.info(f"[{task_id}] Queueing uploads to all destinations") logger.info(f"[{task_id}] Queueing uploads to all destinations")
log_task_progress(task_id, "finalize_document_storage", "success", "Queuing uploads to destinations", file_id=file_id) log_task_progress(
task_id, "finalize_document_storage", "success", "Queuing uploads to destinations", file_id=file_id
)
# Note: send_to_all_destinations is asynchronous and queues upload tasks # Note: send_to_all_destinations is asynchronous and queues upload tasks
# We pass 'True' (delete_after) and 'file_id' as per Main branch requirements # We pass 'True' (delete_after) and 'file_id' as per Main branch requirements
@@ -78,15 +85,9 @@ def finalize_document_storage(self, original_file: str, processed_file: str, met
filename = os.path.basename(processed_file) filename = os.path.basename(processed_file)
notify_file_processed( notify_file_processed(
filename=filename, filename=filename, file_size=file_size, metadata=metadata, destinations=configured_destinations
file_size=file_size,
metadata=metadata,
destinations=configured_destinations
) )
except Exception as e: except Exception as e:
logger.warning(f"[WARNING] Failed to send file processed notification: {e}") logger.warning(f"[WARNING] Failed to send file processed notification: {e}")
return { return {"status": "Completed", "file": processed_file}
"status": "Completed",
"file": processed_file
}
+16 -21
View File
@@ -1,16 +1,18 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import os
import json
import email import email
import imaplib import imaplib
import json
import logging import logging
import redis import os
import re import re
from datetime import datetime, timedelta, timezone from datetime import datetime, timedelta, timezone
import redis
from celery import shared_task from celery import shared_task
from app.config import settings from app.config import settings
from app.tasks.process_document import process_document # Updated import
from app.tasks.convert_to_pdf import convert_to_pdf # new conversion task from app.tasks.convert_to_pdf import convert_to_pdf # new conversion task
from app.tasks.process_document import process_document # Updated import
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -141,8 +143,7 @@ def check_and_pull_mailbox(
) )
def pull_inbox(mailbox_key, host, port, username, password, use_ssl, def pull_inbox(mailbox_key, host, port, username, password, use_ssl, delete_after_process):
delete_after_process):
""" """
Connects to the IMAP inbox, fetches new unread emails from the last 3 days, Connects to the IMAP inbox, fetches new unread emails from the last 3 days,
and processes attachments while preserving the original unread status. and processes attachments while preserving the original unread status.
@@ -153,8 +154,7 @@ def pull_inbox(mailbox_key, host, port, username, password, use_ssl,
For non-Gmail mailboxes, it falls back to selecting the INBOX with a SINCE/UNSEEN filter. For non-Gmail mailboxes, it falls back to selecting the INBOX with a SINCE/UNSEEN filter.
""" """
logger.info("Connecting to %s at %s:%s (SSL=%s)", logger.info("Connecting to %s at %s:%s (SSL=%s)", mailbox_key, host, port, use_ssl)
mailbox_key, host, port, use_ssl)
processed_emails = load_processed_emails() processed_emails = load_processed_emails()
try: try:
@@ -177,13 +177,11 @@ def pull_inbox(mailbox_key, host, port, username, password, use_ssl,
else: else:
# For non-Gmail, select INBOX and use SINCE/UNSEEN query. # For non-Gmail, select INBOX and use SINCE/UNSEEN query.
mail.select("INBOX") mail.select("INBOX")
since_date = (datetime.now(timezone.utc) - timedelta(days=3) since_date = (datetime.now(timezone.utc) - timedelta(days=3)).strftime("%d-%b-%Y")
).strftime("%d-%b-%Y") status, search_data = mail.search(None, f"(SINCE {since_date} UNSEEN)")
status, search_data = mail.search(None, f'(SINCE {since_date} UNSEEN)')
if status != "OK": if status != "OK":
logger.warning("Search failed on mailbox %s. Status=%s", logger.warning("Search failed on mailbox %s. Status=%s", mailbox_key, status)
mailbox_key, status)
mail.close() mail.close()
mail.logout() mail.logout()
return return
@@ -194,8 +192,7 @@ def pull_inbox(mailbox_key, host, port, username, password, use_ssl,
for num in msg_numbers: for num in msg_numbers:
status, msg_data = mail.fetch(num, "(RFC822)") status, msg_data = mail.fetch(num, "(RFC822)")
if status != "OK": if status != "OK":
logger.warning("Failed to fetch message %s in %s. Status=%s", logger.warning("Failed to fetch message %s in %s. Status=%s", num, mailbox_key, status)
num, mailbox_key, status)
continue continue
raw_email = msg_data[0][1] raw_email = msg_data[0][1]
@@ -213,8 +210,7 @@ def pull_inbox(mailbox_key, host, port, username, password, use_ssl,
# For Gmail, check if the email already has the "Ingested" label. # For Gmail, check if the email already has the "Ingested" label.
if is_gmail_host: if is_gmail_host:
if email_already_has_label(mail, num, "Ingested"): if email_already_has_label(mail, num, "Ingested"):
logger.info("Skipping email %s in %s, already labeled 'Ingested'.", logger.info("Skipping email %s in %s, already labeled 'Ingested'.", msg_id, mailbox_key)
msg_id, mailbox_key)
continue continue
# Process attachments (and convert non-PDF files). # Process attachments (and convert non-PDF files).
@@ -296,13 +292,12 @@ def fetch_attachments_and_enqueue(email_message):
continue continue
# Check if it's a PDF file by extension, regardless of MIME type # Check if it's a PDF file by extension, regardless of MIME type
is_pdf_by_extension = filename.lower().endswith('.pdf') is_pdf_by_extension = filename.lower().endswith(".pdf")
mime_type = part.get_content_type() mime_type = part.get_content_type()
# Accept file if it has an allowed MIME type OR it's a PDF by extension # Accept file if it has an allowed MIME type OR it's a PDF by extension
if mime_type not in ALLOWED_MIME_TYPES and not is_pdf_by_extension: if mime_type not in ALLOWED_MIME_TYPES and not is_pdf_by_extension:
logger.info("Skipping attachment %s with MIME type %s", logger.info("Skipping attachment %s with MIME type %s", filename, mime_type)
filename, mime_type)
continue continue
file_path = os.path.join(settings.workdir, filename) file_path = os.path.join(settings.workdir, filename)
@@ -401,7 +396,7 @@ def find_all_mail_xlist(mail):
Returns the folder name if found, otherwise None. Returns the folder name if found, otherwise None.
""" """
tag = mail._new_tag().decode("ascii") tag = mail._new_tag().decode("ascii")
command_str = f"{tag} XLIST \"\" \"*\"" command_str = f'{tag} XLIST "" "*"'
mail.send((command_str + "\r\n").encode("utf-8")) mail.send((command_str + "\r\n").encode("utf-8"))
all_mail_folder = None all_mail_folder = None
@@ -1,23 +1,23 @@
import os
import logging import logging
import os
import azure.core.exceptions
import PyPDF2 import PyPDF2
from azure.core.credentials import AzureKeyCredential
from azure.ai.documentintelligence import DocumentIntelligenceClient from azure.ai.documentintelligence import DocumentIntelligenceClient
from azure.ai.documentintelligence.models import AnalyzeOutputOption, AnalyzeResult from azure.ai.documentintelligence.models import AnalyzeOutputOption, AnalyzeResult
import azure.core.exceptions from azure.core.credentials import AzureKeyCredential
from app.celery_app import celery
from app.config import settings from app.config import settings
from app.tasks.retry_config import BaseTaskWithRetry from app.tasks.retry_config import BaseTaskWithRetry
from app.tasks.rotate_pdf_pages import rotate_pdf_pages from app.tasks.rotate_pdf_pages import rotate_pdf_pages
from app.celery_app import celery
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
# Initialize Azure Document Intelligence client with error handling # Initialize Azure Document Intelligence client with error handling
try: try:
document_intelligence_client = DocumentIntelligenceClient( document_intelligence_client = DocumentIntelligenceClient(
endpoint=settings.azure_endpoint, endpoint=settings.azure_endpoint, credential=AzureKeyCredential(settings.azure_ai_key)
credential=AzureKeyCredential(settings.azure_ai_key)
) )
logger.info("Azure Document Intelligence client initialized successfully") logger.info("Azure Document Intelligence client initialized successfully")
except (ValueError, azure.core.exceptions.ClientAuthenticationError) as e: except (ValueError, azure.core.exceptions.ClientAuthenticationError) as e:
@@ -33,16 +33,18 @@ AZURE_DOC_INTELLIGENCE_LIMITS = {
"max_pages": 2000, "max_pages": 2000,
} }
def get_pdf_page_count(file_path): def get_pdf_page_count(file_path):
"""Get the number of pages in a PDF file.""" """Get the number of pages in a PDF file."""
try: try:
with open(file_path, 'rb') as file: with open(file_path, "rb") as file:
pdf_reader = PyPDF2.PdfReader(file) pdf_reader = PyPDF2.PdfReader(file)
return len(pdf_reader.pages) return len(pdf_reader.pages)
except Exception as e: except Exception as e:
logger.error(f"Error getting PDF page count: {e}") logger.error(f"Error getting PDF page count: {e}")
return None return None
def check_page_rotation(result, filename): def check_page_rotation(result, filename):
""" """
Checks if pages in the document are rotated and logs the rotation information. Checks if pages in the document are rotated and logs the rotation information.
@@ -57,12 +59,12 @@ def check_page_rotation(result, filename):
logger.error(f"Checking rotation for document: {filename}") logger.error(f"Checking rotation for document: {filename}")
rotation_data = {} rotation_data = {}
if not hasattr(result, 'pages') or not result.pages: if not hasattr(result, "pages") or not result.pages:
logger.error(f"No page information available for rotation check: {filename}") logger.error(f"No page information available for rotation check: {filename}")
return rotation_data return rotation_data
for i, page in enumerate(result.pages): for i, page in enumerate(result.pages):
if hasattr(page, 'angle'): if hasattr(page, "angle"):
rotation_angle = page.angle rotation_angle = page.angle
if rotation_angle != 0: if rotation_angle != 0:
logger.error(f"Page {i+1} is rotated by {rotation_angle} degrees") logger.error(f"Page {i+1} is rotated by {rotation_angle} degrees")
@@ -75,6 +77,7 @@ def check_page_rotation(result, filename):
return rotation_data return rotation_data
@celery.task(base=BaseTaskWithRetry) @celery.task(base=BaseTaskWithRetry)
def process_with_azure_document_intelligence(filename: str, file_id: int = None): def process_with_azure_document_intelligence(filename: str, file_id: int = None):
""" """
@@ -101,13 +104,15 @@ def process_with_azure_document_intelligence(filename: str, file_id: int = None)
# Check file size against service limits # Check file size against service limits
file_size = os.path.getsize(tmp_file_path) file_size = os.path.getsize(tmp_file_path)
if file_size > AZURE_DOC_INTELLIGENCE_LIMITS["max_file_size_bytes"]: if file_size > AZURE_DOC_INTELLIGENCE_LIMITS["max_file_size_bytes"]:
error_msg = f"File size ({file_size / (1024 * 1024):.2f} MB) exceeds Azure Document Intelligence limit of 500 MB" error_msg = (
f"File size ({file_size / (1024 * 1024):.2f} MB) exceeds Azure Document Intelligence limit of 500 MB"
)
logger.error(error_msg) logger.error(error_msg)
return {"error": error_msg, "file": filename, "status": "Failed - Size limit exceeded"} return {"error": error_msg, "file": filename, "status": "Failed - Size limit exceeded"}
# For PDF files, check page count against service limits # For PDF files, check page count against service limits
# "Fail open" approach: only reject if we're sure it exceeds the limit # "Fail open" approach: only reject if we're sure it exceeds the limit
if filename.lower().endswith('.pdf'): if filename.lower().endswith(".pdf"):
page_count = get_pdf_page_count(tmp_file_path) page_count = get_pdf_page_count(tmp_file_path)
if page_count is not None and page_count > AZURE_DOC_INTELLIGENCE_LIMITS["max_pages"]: if page_count is not None and page_count > AZURE_DOC_INTELLIGENCE_LIMITS["max_pages"]:
error_msg = f"PDF page count ({page_count}) exceeds Azure Document Intelligence limit of 2000 pages" error_msg = f"PDF page count ({page_count}) exceeds Azure Document Intelligence limit of 2000 pages"
@@ -130,9 +135,7 @@ def process_with_azure_document_intelligence(filename: str, file_id: int = None)
rotation_data = check_page_rotation(result, filename) rotation_data = check_page_rotation(result, filename)
# Retrieve the processed searchable PDF # Retrieve the processed searchable PDF
response = document_intelligence_client.get_analyze_result_pdf( response = document_intelligence_client.get_analyze_result_pdf(model_id=result.model_id, result_id=operation_id)
model_id=result.model_id, result_id=operation_id
)
searchable_pdf_path = tmp_file_path # Overwrite the original PDF location searchable_pdf_path = tmp_file_path # Overwrite the original PDF location
with open(searchable_pdf_path, "wb") as writer: with open(searchable_pdf_path, "wb") as writer:
writer.writelines(response) writer.writelines(response)
+14 -10
View File
@@ -1,17 +1,15 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from app.config import settings
import openai import openai
from app.tasks.retry_config import BaseTaskWithRetry
# Import the shared Celery instance # Import the shared Celery instance
from app.celery_app import celery from app.celery_app import celery
from app.config import settings
from app.tasks.retry_config import BaseTaskWithRetry
# Initialize OpenAI client dynamically # Initialize OpenAI client dynamically
client = openai.OpenAI( client = openai.OpenAI(api_key=settings.openai_api_key, base_url=settings.openai_base_url)
api_key=settings.openai_api_key,
base_url=settings.openai_base_url
)
@celery.task(base=BaseTaskWithRetry) @celery.task(base=BaseTaskWithRetry)
def refine_text_with_gpt(filename: str, raw_text: str): def refine_text_with_gpt(filename: str, raw_text: str):
@@ -19,16 +17,22 @@ def refine_text_with_gpt(filename: str, raw_text: str):
response = client.chat.completions.create( response = client.chat.completions.create(
model=settings.openai_model, model=settings.openai_model,
messages=[ messages=[
{"role": "system", "content": "Clean and format the following text. The idea is that the text you see comes from an OCR system and your task is to eliminate OCR errors. Keep the original language when doing so."}, {
{"role": "user", "content": raw_text} "role": "system",
] "content": (
"Clean and format the following text. The idea is that the text you see comes from an OCR "
"system and your task is to eliminate OCR errors. Keep the original language when doing so."
),
},
{"role": "user", "content": raw_text},
],
) )
cleaned_text = response.choices[0].message.content cleaned_text = response.choices[0].message.content
# Trigger next task (import locally if needed to avoid circular imports) # Trigger next task (import locally if needed to avoid circular imports)
from app.tasks.extract_metadata_with_gpt import extract_metadata_with_gpt from app.tasks.extract_metadata_with_gpt import extract_metadata_with_gpt
extract_metadata_with_gpt.delay(filename, cleaned_text) extract_metadata_with_gpt.delay(filename, cleaned_text)
return {"filename": filename, "cleaned_text": cleaned_text} return {"filename": filename, "cleaned_text": cleaned_text}
+1 -1
View File
@@ -2,8 +2,8 @@
from celery import Task from celery import Task
class BaseTaskWithRetry(Task): class BaseTaskWithRetry(Task):
autoretry_for = (Exception,) autoretry_for = (Exception,)
retry_kwargs = {"max_retries": 3, "countdown": 10} # 3 retries, 10s delay retry_kwargs = {"max_retries": 3, "countdown": 10} # 3 retries, 10s delay
retry_backoff = True # Exponential backoff retry_backoff = True # Exponential backoff
+24 -13
View File
@@ -1,16 +1,17 @@
import os
import logging
import PyPDF2
import math
import json import json
import logging
import os
import PyPDF2
from app.config import settings
from app.tasks.retry_config import BaseTaskWithRetry
from app.tasks.extract_metadata_with_gpt import extract_metadata_with_gpt
from app.celery_app import celery from app.celery_app import celery
from app.config import settings
from app.tasks.extract_metadata_with_gpt import extract_metadata_with_gpt
from app.tasks.retry_config import BaseTaskWithRetry
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
def determine_rotation_angle(detected_angle): def determine_rotation_angle(detected_angle):
""" """
Determine the optimal rotation angle based on detected angle. Determine the optimal rotation angle based on detected angle.
@@ -46,6 +47,7 @@ def determine_rotation_angle(detected_angle):
logger.info(f"Detected angle {detected_angle}° rounded to {closest_90_multiple}°, will rotate by {rotation_value}°") logger.info(f"Detected angle {detected_angle}° rounded to {closest_90_multiple}°, will rotate by {rotation_value}°")
return rotation_value return rotation_value
@celery.task(base=BaseTaskWithRetry) @celery.task(base=BaseTaskWithRetry)
def rotate_pdf_pages(filename: str, extracted_text: str, rotation_data=None, file_id: int = None): def rotate_pdf_pages(filename: str, extracted_text: str, rotation_data=None, file_id: int = None):
""" """
@@ -85,7 +87,7 @@ def rotate_pdf_pages(filename: str, extracted_text: str, rotation_data=None, fil
applied_rotations = {} applied_rotations = {}
# Load the PDF # Load the PDF
with open(pdf_path, 'rb') as file: with open(pdf_path, "rb") as file:
pdf_reader = PyPDF2.PdfReader(file) pdf_reader = PyPDF2.PdfReader(file)
pdf_writer = PyPDF2.PdfWriter() pdf_writer = PyPDF2.PdfWriter()
@@ -101,21 +103,30 @@ def rotate_pdf_pages(filename: str, extracted_text: str, rotation_data=None, fil
if rotation_angle > 0: if rotation_angle > 0:
# PyPDF2 uses clockwise rotation in 90-degree increments # PyPDF2 uses clockwise rotation in 90-degree increments
page.rotate(rotation_angle) page.rotate(rotation_angle)
logger.info(f"Page {page_idx+1} rotated by {rotation_angle}° (from detected {detected_angle}°)") logger.info(
f"Page {page_idx+1} rotated by {rotation_angle}° "
f"(from detected {detected_angle}°)"
)
applied_rotations[str(page_idx)] = rotation_angle applied_rotations[str(page_idx)] = rotation_angle
else: else:
logger.info(f"Page {page_idx+1} had detected angle {detected_angle}° but determined it doesn't need rotation") logger.info(
f"Page {page_idx+1} had detected angle {detected_angle}° "
"but determined it doesn't need rotation"
)
pdf_writer.add_page(page) pdf_writer.add_page(page)
# Save the rotated PDF # Save the rotated PDF
with open(pdf_path, 'wb') as output_file: with open(pdf_path, "wb") as output_file:
pdf_writer.write(output_file) pdf_writer.write(output_file)
if applied_rotations: if applied_rotations:
logger.info(f"Successfully rotated PDF: {filename} with rotations: {json.dumps(applied_rotations)}") logger.info(f"Successfully rotated PDF: {filename} with rotations: {json.dumps(applied_rotations)}")
else: else:
logger.info(f"Detected rotations in {filename} but no rotations were actually applied (angles too small or not multiples of 90°)") logger.info(
f"Detected rotations in {filename} but no rotations were actually applied "
"(angles too small or not multiples of 90°)"
)
# Continue with metadata extraction # Continue with metadata extraction
extract_metadata_with_gpt.delay(filename, extracted_text, file_id) extract_metadata_with_gpt.delay(filename, extracted_text, file_id)
@@ -124,7 +135,7 @@ def rotate_pdf_pages(filename: str, extracted_text: str, rotation_data=None, fil
"file": filename, "file": filename,
"status": "rotated" if applied_rotations else "no_rotation_needed", "status": "rotated" if applied_rotations else "no_rotation_needed",
"detected_rotations": rotation_data, "detected_rotations": rotation_data,
"applied_rotations": applied_rotations "applied_rotations": applied_rotations,
} }
except Exception as e: except Exception as e:
+68 -62
View File
@@ -1,83 +1,80 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import os
import logging import logging
from app.config import settings import os
from app.tasks.retry_config import BaseTaskWithRetry
from app.tasks.upload_to_dropbox import upload_to_dropbox
from app.tasks.upload_to_nextcloud import upload_to_nextcloud
from app.tasks.upload_to_paperless import upload_to_paperless
from app.tasks.upload_to_google_drive import upload_to_google_drive
from app.tasks.upload_to_webdav import upload_to_webdav
from app.tasks.upload_to_ftp import upload_to_ftp
from app.tasks.upload_to_sftp import upload_to_sftp
from app.tasks.upload_to_email import upload_to_email
from app.tasks.upload_to_onedrive import upload_to_onedrive
from app.tasks.upload_to_s3 import upload_to_s3
from app.utils.config_validator import get_provider_status
from app.celery_app import celery from app.celery_app import celery
from app.utils import log_task_progress from app.config import settings
from app.database import SessionLocal from app.database import SessionLocal
from app.models import FileRecord from app.models import FileRecord
from app.tasks.retry_config import BaseTaskWithRetry
from app.tasks.upload_to_dropbox import upload_to_dropbox
from app.tasks.upload_to_email import upload_to_email
from app.tasks.upload_to_ftp import upload_to_ftp
from app.tasks.upload_to_google_drive import upload_to_google_drive
from app.tasks.upload_to_nextcloud import upload_to_nextcloud
from app.tasks.upload_to_onedrive import upload_to_onedrive
from app.tasks.upload_to_paperless import upload_to_paperless
from app.tasks.upload_to_s3 import upload_to_s3
from app.tasks.upload_to_sftp import upload_to_sftp
from app.tasks.upload_to_webdav import upload_to_webdav
from app.utils import log_task_progress
from app.utils.config_validator import get_provider_status
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
def _should_upload_to_dropbox(): def _should_upload_to_dropbox():
return (settings.dropbox_app_key and return settings.dropbox_app_key and settings.dropbox_app_secret and settings.dropbox_refresh_token
settings.dropbox_app_secret and
settings.dropbox_refresh_token)
def _should_upload_to_nextcloud(): def _should_upload_to_nextcloud():
return (settings.nextcloud_upload_url and return settings.nextcloud_upload_url and settings.nextcloud_username and settings.nextcloud_password
settings.nextcloud_username and
settings.nextcloud_password)
def _should_upload_to_paperless(): def _should_upload_to_paperless():
return (settings.paperless_ngx_api_token and return settings.paperless_ngx_api_token and settings.paperless_host
settings.paperless_host)
def _should_upload_to_google_drive(): def _should_upload_to_google_drive():
# Check for OAuth configuration # Check for OAuth configuration
if getattr(settings, 'google_drive_use_oauth', False): if getattr(settings, "google_drive_use_oauth", False):
return (settings.google_drive_client_id and return (
settings.google_drive_client_secret and settings.google_drive_client_id
settings.google_drive_refresh_token and and settings.google_drive_client_secret
settings.google_drive_folder_id) and settings.google_drive_refresh_token
and settings.google_drive_folder_id
)
# Or check for service account configuration # Or check for service account configuration
else: else:
return (settings.google_drive_credentials_json and return settings.google_drive_credentials_json and settings.google_drive_folder_id
settings.google_drive_folder_id)
def _should_upload_to_webdav(): def _should_upload_to_webdav():
return (settings.webdav_url and return settings.webdav_url and settings.webdav_username and settings.webdav_password
settings.webdav_username and
settings.webdav_password)
def _should_upload_to_ftp(): def _should_upload_to_ftp():
return (settings.ftp_host and return settings.ftp_host and settings.ftp_username and settings.ftp_password
settings.ftp_username and
settings.ftp_password)
def _should_upload_to_sftp(): def _should_upload_to_sftp():
return (settings.sftp_host and return settings.sftp_host and settings.sftp_username and (settings.sftp_password or settings.sftp_private_key)
settings.sftp_username and
(settings.sftp_password or settings.sftp_private_key))
def _should_upload_to_email(): def _should_upload_to_email():
return (settings.email_host and return (
settings.email_username and settings.email_host and settings.email_username and settings.email_password and settings.email_default_recipient
settings.email_password and )
settings.email_default_recipient)
def _should_upload_to_onedrive(): def _should_upload_to_onedrive():
return (settings.onedrive_client_id and return settings.onedrive_client_id and settings.onedrive_client_secret and settings.onedrive_refresh_token
settings.onedrive_client_secret and
settings.onedrive_refresh_token)
def _should_upload_to_s3(): def _should_upload_to_s3():
return (settings.s3_bucket_name and return settings.s3_bucket_name and settings.aws_access_key_id and settings.aws_secret_access_key
settings.aws_access_key_id and
settings.aws_secret_access_key)
def get_configured_services_from_validator(): def get_configured_services_from_validator():
""" """
@@ -97,16 +94,17 @@ def get_configured_services_from_validator():
"SFTP Storage": "sftp", "SFTP Storage": "sftp",
"Email": "email", "Email": "email",
"OneDrive": "onedrive", "OneDrive": "onedrive",
"S3 Storage": "s3" "S3 Storage": "s3",
} }
result = {} result = {}
for provider_name, internal_name in service_map.items(): for provider_name, internal_name in service_map.items():
if provider_name in providers: if provider_name in providers:
result[internal_name] = providers[provider_name].get('configured', False) result[internal_name] = providers[provider_name].get("configured", False)
return result return result
@celery.task(base=BaseTaskWithRetry, bind=True) @celery.task(base=BaseTaskWithRetry, bind=True)
def send_to_all_destinations(self, file_path: str, use_validator=True, file_id: int = None): def send_to_all_destinations(self, file_path: str, use_validator=True, file_id: int = None):
""" """
@@ -126,16 +124,24 @@ def send_to_all_destinations(self, file_path: str, use_validator=True, file_id:
raise FileNotFoundError(f"File not found: {file_path}") raise FileNotFoundError(f"File not found: {file_path}")
logger.info(f"[{task_id}] Sending {file_path} to all configured destinations") logger.info(f"[{task_id}] Sending {file_path} to all configured destinations")
log_task_progress(task_id, "send_to_all_destinations", "in_progress", f"Distributing: {os.path.basename(file_path)}", file_id=file_id) log_task_progress(
task_id,
"send_to_all_destinations",
"in_progress",
f"Distributing: {os.path.basename(file_path)}",
file_id=file_id,
)
# Get file_id from database if not provided (fallback only, prefer passing file_id explicitly) # Get file_id from database if not provided (fallback only, prefer passing file_id explicitly)
if file_id is None: if file_id is None:
with SessionLocal() as db: with SessionLocal() as db:
# Only as a last resort, try to find by basename match # Only as a last resort, try to find by basename match
# This should not be needed if file_id is passed correctly through the chain # This should not be needed if file_id is passed correctly through the chain
file_record = db.query(FileRecord).filter( file_record = (
FileRecord.local_filename == os.path.join(settings.workdir, "tmp", os.path.basename(file_path)) db.query(FileRecord)
).first() .filter(FileRecord.local_filename == os.path.join(settings.workdir, "tmp", os.path.basename(file_path)))
.first()
)
if file_record: if file_record:
file_id = file_record.id file_id = file_record.id
@@ -226,12 +232,16 @@ def send_to_all_destinations(self, file_path: str, use_validator=True, file_id:
# Queue the upload task if service is configured # Queue the upload task if service is configured
if is_configured: if is_configured:
logger.info(f"[{task_id}] Queueing {file_path} for {service_name} upload") logger.info(f"[{task_id}] Queueing {file_path} for {service_name} upload")
log_task_progress(task_id, f"queue_{service_name}", "in_progress", f"Queueing upload to {service_name}", file_id=file_id) log_task_progress(
task_id, f"queue_{service_name}", "in_progress", f"Queueing upload to {service_name}", file_id=file_id
)
try: try:
task = service["upload_func"].delay(file_path, file_id=file_id) task = service["upload_func"].delay(file_path, file_id=file_id)
results[f"{service_name}_task_id"] = task.id results[f"{service_name}_task_id"] = task.id
queued_count += 1 queued_count += 1
log_task_progress(task_id, f"queue_{service_name}", "success", f"Queued for {service_name}", file_id=file_id) log_task_progress(
task_id, f"queue_{service_name}", "success", f"Queued for {service_name}", file_id=file_id
)
except Exception as e: except Exception as e:
logger.error(f"[{task_id}] Failed to queue {service_name} task: {str(e)}") logger.error(f"[{task_id}] Failed to queue {service_name} task: {str(e)}")
results[f"{service_name}_error"] = str(e) results[f"{service_name}_error"] = str(e)
@@ -240,8 +250,4 @@ def send_to_all_destinations(self, file_path: str, use_validator=True, file_id:
logger.info(f"[{task_id}] Queued {queued_count} upload tasks") logger.info(f"[{task_id}] Queued {queued_count} upload tasks")
log_task_progress(task_id, "send_to_all_destinations", "success", f"Queued {queued_count} uploads", file_id=file_id) log_task_progress(task_id, "send_to_all_destinations", "success", f"Queued {queued_count} uploads", file_id=file_id)
return { return {"status": "Queued", "file_path": file_path, "tasks": results}
"status": "Queued",
"file_path": file_path,
"tasks": results
}
+37 -32
View File
@@ -1,31 +1,32 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import os
import logging import logging
import requests import os
import dropbox import dropbox
import requests
from dropbox.exceptions import ApiError, AuthError from dropbox.exceptions import ApiError, AuthError
from app.celery_app import celery
from app.config import settings from app.config import settings
from app.tasks.retry_config import BaseTaskWithRetry from app.tasks.retry_config import BaseTaskWithRetry
from app.celery_app import celery
from app.utils.filename_utils import get_unique_filename, sanitize_filename, extract_remote_path
from app.utils import log_task_progress from app.utils import log_task_progress
from app.database import SessionLocal from app.utils.filename_utils import extract_remote_path, get_unique_filename
from app.models import FileRecord
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
def _validate_dropbox_settings(): def _validate_dropbox_settings():
"""Validate that all required Dropbox settings are available.""" """Validate that all required Dropbox settings are available."""
missing = [] missing = []
if not hasattr(settings, 'dropbox_refresh_token') or not settings.dropbox_refresh_token: if not hasattr(settings, "dropbox_refresh_token") or not settings.dropbox_refresh_token:
missing.append("refresh token") missing.append("refresh token")
if not hasattr(settings, 'dropbox_app_key') or not settings.dropbox_app_key: if not hasattr(settings, "dropbox_app_key") or not settings.dropbox_app_key:
missing.append("app key") missing.append("app key")
if not hasattr(settings, 'dropbox_app_secret') or not settings.dropbox_app_secret: if not hasattr(settings, "dropbox_app_secret") or not settings.dropbox_app_secret:
missing.append("app secret") missing.append("app secret")
if missing: if missing:
@@ -34,6 +35,7 @@ def _validate_dropbox_settings():
return True return True
def get_dropbox_access_token(): def get_dropbox_access_token():
"""Refresh the Dropbox access token using the stored refresh token from ENV.""" """Refresh the Dropbox access token using the stored refresh token from ENV."""
@@ -59,6 +61,7 @@ def get_dropbox_access_token():
logger.error(error_msg) logger.error(error_msg)
raise Exception(error_msg) raise Exception(error_msg)
def get_dropbox_client(): def get_dropbox_client():
""" """
Create and return an authenticated Dropbox client using the configured refresh token. Create and return an authenticated Dropbox client using the configured refresh token.
@@ -83,11 +86,7 @@ def get_dropbox_client():
# Create a Dropbox client with refresh token # Create a Dropbox client with refresh token
try: try:
try: dbx = dropbox.Dropbox(app_key=app_key, app_secret=app_secret, oauth2_refresh_token=refresh_token)
dbx = dropbox.Dropbox(
app_key=app_key,
app_secret=app_secret,
oauth2_refresh_token=refresh_token
# Test the connection # Test the connection
dbx.users_get_current_account() dbx.users_get_current_account()
@@ -102,6 +101,7 @@ def get_dropbox_client():
logger.error(f"Error creating Dropbox client: {str(e)}") logger.error(f"Error creating Dropbox client: {str(e)}")
raise raise
@celery.task(base=BaseTaskWithRetry, bind=True) @celery.task(base=BaseTaskWithRetry, bind=True)
def upload_to_dropbox(self, file_path: str, file_id: int = None): def upload_to_dropbox(self, file_path: str, file_id: int = None):
""" """
@@ -113,7 +113,13 @@ def upload_to_dropbox(self, file_path: str, file_id: int = None):
""" """
task_id = self.request.id task_id = self.request.id
logger.info(f"[{task_id}] Starting Dropbox upload: {file_path}") logger.info(f"[{task_id}] Starting Dropbox upload: {file_path}")
logger.info(f"[{task_id}] Starting Dropbox upload: {file_path}") log_task_progress(
task_id,
"upload_to_dropbox",
"in_progress",
f"Uploading to Dropbox: {os.path.basename(file_path)}",
file_id=file_id,
)
if not os.path.exists(file_path): if not os.path.exists(file_path):
error_msg = f"File not found: {file_path}" error_msg = f"File not found: {file_path}"
@@ -122,9 +128,14 @@ def upload_to_dropbox(self, file_path: str, file_id: int = None):
raise FileNotFoundError(error_msg) raise FileNotFoundError(error_msg)
# Check if Dropbox is properly configured # Check if Dropbox is properly configured
# Check if Dropbox is properly configured if not (
if not (hasattr(settings, 'dropbox_app_key') and settings.dropbox_app_key and hasattr(settings, "dropbox_app_key")
hasattr(settings, 'dropbox_app_secret') and settings.dropbox_app_secret and and settings.dropbox_app_key
and hasattr(settings, "dropbox_app_secret")
and settings.dropbox_app_secret
and hasattr(settings, "dropbox_refresh_token")
and settings.dropbox_refresh_token
):
logger.info(f"[{task_id}] Dropbox upload skipped: Missing configuration") logger.info(f"[{task_id}] Dropbox upload skipped: Missing configuration")
log_task_progress(task_id, "upload_to_dropbox", "success", "Skipped: Not configured", file_id=file_id) log_task_progress(task_id, "upload_to_dropbox", "success", "Skipped: Not configured", file_id=file_id)
return {"status": "Skipped", "reason": "Dropbox settings not configured"} return {"status": "Skipped", "reason": "Dropbox settings not configured"}
@@ -151,7 +162,7 @@ def upload_to_dropbox(self, file_path: str, file_id: int = None):
# Get a unique path in case of collision # Get a unique path in case of collision
remote_full_path = f"/{remote_path}" # Dropbox paths should start with / remote_full_path = f"/{remote_path}" # Dropbox paths should start with /
remote_full_path = f"/{remote_path}" # Dropbox paths should start with / remote_full_path = remote_full_path.replace("//", "/") # Clean double slashes
# Check for potential file collision and get a unique name if needed # Check for potential file collision and get a unique name if needed
dropbox_path = get_unique_filename(remote_full_path, check_exists_in_dropbox) dropbox_path = get_unique_filename(remote_full_path, check_exists_in_dropbox)
@@ -159,7 +170,7 @@ def upload_to_dropbox(self, file_path: str, file_id: int = None):
# Upload the file # Upload the file
logger.info(f"[{task_id}] Uploading {filename} to Dropbox at {dropbox_path}") logger.info(f"[{task_id}] Uploading {filename} to Dropbox at {dropbox_path}")
log_task_progress(task_id, "upload_file", "in_progress", f"Uploading to {dropbox_path}", file_id=file_id) log_task_progress(task_id, "upload_file", "in_progress", f"Uploading to {dropbox_path}", file_id=file_id)
log_task_progress(task_id, "upload_file", "in_progress", f"Uploading to {dropbox_path}", file_id=file_id) with open(file_path, "rb") as file_data:
# Use files_upload_session for large files to avoid timeouts # Use files_upload_session for large files to avoid timeouts
file_size = os.path.getsize(file_path) file_size = os.path.getsize(file_path)
if file_size > 10 * 1024 * 1024: # 10 MB threshold for chunked upload if file_size > 10 * 1024 * 1024: # 10 MB threshold for chunked upload
@@ -178,7 +189,7 @@ def upload_to_dropbox(self, file_path: str, file_id: int = None):
dbx.files_upload_session_finish( dbx.files_upload_session_finish(
file_data.read(chunk_size), file_data.read(chunk_size),
cursor, cursor,
cursor, dropbox.files.CommitInfo(path=dropbox_path, mode=dropbox.files.WriteMode.overwrite),
) )
else: else:
# More chunks to upload # More chunks to upload
@@ -187,19 +198,13 @@ def upload_to_dropbox(self, file_path: str, file_id: int = None):
else: else:
# Small file, direct upload # Small file, direct upload
file_data.seek(0) file_data.seek(0)
file_data.seek(0) dbx.files_upload(file_data.read(), dropbox_path, mode=dropbox.files.WriteMode.overwrite)
dbx.files_upload(
file_data.read(),
dropbox_path,
mode=dropbox.files.WriteMode.overwrite
logger.info(f"[{task_id}] Successfully uploaded {filename} to Dropbox at {dropbox_path}") logger.info(f"[{task_id}] Successfully uploaded {filename} to Dropbox at {dropbox_path}")
logger.info(f"[{task_id}] Successfully uploaded {filename} to Dropbox at {dropbox_path}") log_task_progress(
log_task_progress(task_id, "upload_to_dropbox", "success", f"Uploaded to Dropbox: {dropbox_path}", file_id=file_id) task_id, "upload_to_dropbox", "success", f"Uploaded to Dropbox: {dropbox_path}", file_id=file_id
return { )
"status": "Completed", return {"status": "Completed", "file_path": file_path, "dropbox_path": dropbox_path}
"file_path": file_path,
"dropbox_path": dropbox_path
except AuthError: except AuthError:
error_msg = f"Authentication failed while uploading {filename} to Dropbox. Check token." error_msg = f"Authentication failed while uploading {filename} to Dropbox. Check token."
+48 -34
View File
@@ -1,24 +1,26 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import os
import json import json
import logging
import os
import smtplib import smtplib
import socket import socket
import logging from datetime import datetime
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication from email.mime.application import MIMEApplication
from email.mime.image import MIMEImage from email.mime.image import MIMEImage
from datetime import datetime from email.mime.multipart import MIMEMultipart
from pathlib import Path from email.mime.text import MIMEText
from jinja2 import Environment, FileSystemLoader, select_autoescape from jinja2 import Environment, FileSystemLoader, select_autoescape
from app.celery_app import celery
from app.config import settings from app.config import settings
from app.tasks.retry_config import BaseTaskWithRetry from app.tasks.retry_config import BaseTaskWithRetry
from app.celery_app import celery
from app.utils import log_task_progress from app.utils import log_task_progress
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
def get_email_template(template_name="default.html"): def get_email_template(template_name="default.html"):
""" """
Load email template from one of these locations in order of precedence: Load email template from one of these locations in order of precedence:
@@ -30,10 +32,9 @@ def get_email_template(template_name="default.html"):
workdir_template_path = os.path.join(settings.workdir, "templates", "email") workdir_template_path = os.path.join(settings.workdir, "templates", "email")
if os.path.exists(workdir_template_path): if os.path.exists(workdir_template_path):
env = Environment( env = Environment(
loader=FileSystemLoader(workdir_template_path), loader=FileSystemLoader(workdir_template_path), autoescape=select_autoescape(["html", "xml"])
autoescape=select_autoescape(['html', 'xml'])
) )
env.globals['now'] = datetime.now # Add the now function to the Jinja environment env.globals["now"] = datetime.now # Add the now function to the Jinja environment
template = env.get_template(template_name) template = env.get_template(template_name)
logger.info(f"Using custom email template from workdir: {template_name}") logger.info(f"Using custom email template from workdir: {template_name}")
return template return template
@@ -45,11 +46,8 @@ def get_email_template(template_name="default.html"):
# Get the app directory path (where this file is) # Get the app directory path (where this file is)
current_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) current_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
app_template_path = os.path.join(current_dir, "templates", "email") app_template_path = os.path.join(current_dir, "templates", "email")
env = Environment( env = Environment(loader=FileSystemLoader(app_template_path), autoescape=select_autoescape(["html", "xml"]))
loader=FileSystemLoader(app_template_path), env.globals["now"] = datetime.now # Add the now function to the Jinja environment
autoescape=select_autoescape(['html', 'xml'])
)
env.globals['now'] = datetime.now # Add the now function to the Jinja environment
template = env.get_template(template_name) template = env.get_template(template_name)
logger.info(f"Using built-in email template: {template_name}") logger.info(f"Using built-in email template: {template_name}")
return template return template
@@ -57,6 +55,7 @@ def get_email_template(template_name="default.html"):
logger.error(f"Failed to load built-in email template: {str(e)}") logger.error(f"Failed to load built-in email template: {str(e)}")
raise ValueError(f"Could not find any valid email template: {str(e)}") raise ValueError(f"Could not find any valid email template: {str(e)}")
def extract_metadata_from_file(file_path): def extract_metadata_from_file(file_path):
""" """
Try to extract metadata from a file using several methods: Try to extract metadata from a file using several methods:
@@ -68,10 +67,10 @@ def extract_metadata_from_file(file_path):
metadata = {} metadata = {}
# Check for separate metadata JSON file # Check for separate metadata JSON file
metadata_path = os.path.splitext(file_path)[0] + '.json' metadata_path = os.path.splitext(file_path)[0] + ".json"
if os.path.exists(metadata_path): if os.path.exists(metadata_path):
try: try:
with open(metadata_path, 'r', encoding='utf-8') as f: with open(metadata_path, "r", encoding="utf-8") as f:
metadata = json.load(f) metadata = json.load(f)
logger.info(f"Loaded metadata from external JSON file: {metadata_path}") logger.info(f"Loaded metadata from external JSON file: {metadata_path}")
return metadata return metadata
@@ -83,6 +82,7 @@ def extract_metadata_from_file(file_path):
return metadata return metadata
def attach_logo(msg): def attach_logo(msg):
"""Attach the DocuElevate logo to the email with proper Content-ID.""" """Attach the DocuElevate logo to the email with proper Content-ID."""
try: try:
@@ -99,14 +99,14 @@ def attach_logo(msg):
logo_path = os.path.join(app_dir, "..", "frontend", "static", "logo.png") logo_path = os.path.join(app_dir, "..", "frontend", "static", "logo.png")
if os.path.exists(logo_path): if os.path.exists(logo_path):
with open(logo_path, 'rb') as img: with open(logo_path, "rb") as img:
logo_data = img.read() logo_data = img.read()
# Determine image MIME type based on extension # Determine image MIME type based on extension
mimetype = 'image/svg+xml' if logo_path.endswith('.svg') else 'image/png' mimetype = "image/svg+xml" if logo_path.endswith(".svg") else "image/png"
logo_attach = MIMEImage(logo_data, mimetype) logo_attach = MIMEImage(logo_data, mimetype)
logo_attach.add_header('Content-ID', '<logo>') logo_attach.add_header("Content-ID", "<logo>")
logo_attach.add_header('Content-Disposition', 'inline', filename='logo.png') logo_attach.add_header("Content-Disposition", "inline", filename="logo.png")
msg.attach(logo_attach) msg.attach(logo_attach)
logger.info(f"Logo attached from {logo_path}") logger.info(f"Logo attached from {logo_path}")
return True return True
@@ -118,6 +118,7 @@ def attach_logo(msg):
logger.warning(f"Error attaching logo: {str(e)}") logger.warning(f"Error attaching logo: {str(e)}")
return False return False
def _prepare_recipients(recipients): def _prepare_recipients(recipients):
"""Helper function to prepare email recipients list.""" """Helper function to prepare email recipients list."""
if not recipients: if not recipients:
@@ -130,6 +131,7 @@ def _prepare_recipients(recipients):
return [recipients], None # Convert single email to list return [recipients], None # Convert single email to list
return recipients, None return recipients, None
def _send_email_with_smtp(msg, filename, recipients): def _send_email_with_smtp(msg, filename, recipients):
"""Helper function to handle SMTP connection and sending.""" """Helper function to handle SMTP connection and sending."""
try: try:
@@ -160,8 +162,18 @@ def _send_email_with_smtp(msg, filename, recipients):
logger.error(error_msg) logger.error(error_msg)
return {"status": "Failed", "reason": error_msg, "error": str(e)} return {"status": "Failed", "reason": error_msg, "error": str(e)}
@celery.task(base=BaseTaskWithRetry, bind=True) @celery.task(base=BaseTaskWithRetry, bind=True)
def upload_to_email(self, file_path: str, recipients=None, subject=None, message=None, template_name="default.html", include_metadata=True, file_id: int = None): def upload_to_email(
self,
file_path: str,
recipients=None,
subject=None,
message=None,
template_name="default.html",
include_metadata=True,
file_id: int = None,
):
""" """
Sends a file via email to the specified recipients. Sends a file via email to the specified recipients.
If recipients is None, uses the configured default email recipient. If recipients is None, uses the configured default email recipient.
@@ -198,8 +210,10 @@ def upload_to_email(self, file_path: str, recipients=None, subject=None, message
return {"status": "Skipped", "reason": error_msg} return {"status": "Skipped", "reason": error_msg}
# Log email configuration for debugging # Log email configuration for debugging
logger.debug(f"[{task_id}] Email config - Host: {settings.email_host}, Port: {settings.email_port}, " logger.debug(
f"Username: {settings.email_username}, TLS: {settings.email_use_tls}") f"[{task_id}] Email config - Host: {settings.email_host}, Port: {settings.email_port}, "
f"Username: {settings.email_username}, TLS: {settings.email_use_tls}"
)
# Process recipients # Process recipients
recipients, error = _prepare_recipients(recipients) recipients, error = _prepare_recipients(recipients)
@@ -218,13 +232,13 @@ def upload_to_email(self, file_path: str, recipients=None, subject=None, message
try: try:
# Create the email # Create the email
msg = MIMEMultipart('related') msg = MIMEMultipart("related")
msg['From'] = settings.email_sender or settings.email_username msg["From"] = settings.email_sender or settings.email_username
msg['To'] = ", ".join(recipients) msg["To"] = ", ".join(recipients)
msg['Subject'] = subject msg["Subject"] = subject
# Create alternative part for HTML content # Create alternative part for HTML content
alt_part = MIMEMultipart('alternative') alt_part = MIMEMultipart("alternative")
msg.attach(alt_part) msg.attach(alt_part)
# Attach logo to the email # Attach logo to the email
@@ -243,24 +257,24 @@ def upload_to_email(self, file_path: str, recipients=None, subject=None, message
"metadata": metadata, "metadata": metadata,
"has_metadata": bool(metadata), "has_metadata": bool(metadata),
"has_logo": has_logo, "has_logo": has_logo,
"current_year": datetime.now().year "current_year": datetime.now().year,
} }
# Render HTML body # Render HTML body
html_content = template.render(**context) html_content = template.render(**context)
alt_part.attach(MIMEText(html_content, 'html')) alt_part.attach(MIMEText(html_content, "html"))
# Attach the file # Attach the file
with open(file_path, "rb") as file: with open(file_path, "rb") as file:
attachment = MIMEApplication(file.read(), _subtype="pdf") attachment = MIMEApplication(file.read(), _subtype="pdf")
attachment.add_header('Content-Disposition', f'attachment; filename="{filename}"') attachment.add_header("Content-Disposition", f'attachment; filename="{filename}"')
msg.attach(attachment) msg.attach(attachment)
# Send the email through SMTP # Send the email through SMTP
error_result = _send_email_with_smtp(msg, filename, recipients) error_result = _send_email_with_smtp(msg, filename, recipients)
if error_result: if error_result:
logger.error(f"[{task_id}] Failed to send email: {error_result.get('reason')}") logger.error(f"[{task_id}] Failed to send email: {error_result.get('reason')}")
log_task_progress(task_id, "upload_to_email", "failure", error_result.get('reason'), file_id=file_id) log_task_progress(task_id, "upload_to_email", "failure", error_result.get("reason"), file_id=file_id)
return error_result return error_result
logger.info(f"[{task_id}] Successfully sent {filename} via email to {len(recipients)} recipients") logger.info(f"[{task_id}] Successfully sent {filename} via email to {len(recipients)} recipients")
@@ -272,7 +286,7 @@ def upload_to_email(self, file_path: str, recipients=None, subject=None, message
"recipients": recipients, "recipients": recipients,
"subject": subject, "subject": subject,
"metadata_included": bool(metadata), "metadata_included": bool(metadata),
"logo_included": has_logo "logo_included": has_logo,
} }
except Exception as e: except Exception as e:
+19 -35
View File
@@ -1,17 +1,19 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import os
# Security Warning: FTP is an insecure protocol. FTPS (FTP_TLS) is strongly recommended. # Security Warning: FTP is an insecure protocol. FTPS (FTP_TLS) is strongly recommended.
# This module attempts to use FTPS by default and falls back to plaintext FTP only if configured. # This module attempts to use FTPS by default and falls back to plaintext FTP only if configured.
import ftplib # nosec B402 - FTP usage is intentional for legacy server support import ftplib # nosec B402 - FTP usage is intentional for legacy server support
import logging
import os
from app.celery_app import celery
from app.config import settings from app.config import settings
from app.tasks.retry_config import BaseTaskWithRetry from app.tasks.retry_config import BaseTaskWithRetry
from app.celery_app import celery
from app.utils import log_task_progress from app.utils import log_task_progress
import logging
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@celery.task(base=BaseTaskWithRetry, bind=True) @celery.task(base=BaseTaskWithRetry, bind=True)
def upload_to_ftp(self, file_path: str, file_id: int = None): def upload_to_ftp(self, file_path: str, file_id: int = None):
""" """
@@ -49,23 +51,17 @@ def upload_to_ftp(self, file_path: str, file_id: int = None):
try: try:
# First attempt FTPS (FTP with TLS) # First attempt FTPS (FTP with TLS)
use_tls = getattr(settings, 'ftp_use_tls', True) # Default to try TLS use_tls = getattr(settings, "ftp_use_tls", True) # Default to try TLS
allow_plaintext = getattr(settings, 'ftp_allow_plaintext', True) # Default to allow plaintext fallback allow_plaintext = getattr(settings, "ftp_allow_plaintext", True) # Default to allow plaintext fallback
if use_tls: if use_tls:
try: try:
logger.info(f"Attempting FTPS connection to {settings.ftp_host}") logger.info(f"Attempting FTPS connection to {settings.ftp_host}")
ftp = ftplib.FTP_TLS() ftp = ftplib.FTP_TLS()
ftp.connect( ftp.connect(host=settings.ftp_host, port=settings.ftp_port or 21)
host=settings.ftp_host,
port=settings.ftp_port or 21
)
# Login with credentials # Login with credentials
ftp.login( ftp.login(user=settings.ftp_username, passwd=settings.ftp_password)
user=settings.ftp_username,
passwd=settings.ftp_password
)
# Enable data protection - encrypt the data channel # Enable data protection - encrypt the data channel
ftp.prot_p() ftp.prot_p()
@@ -79,16 +75,10 @@ def upload_to_ftp(self, file_path: str, file_id: int = None):
logger.warning(f"FTPS connection failed, falling back to regular FTP: {str(e)}") logger.warning(f"FTPS connection failed, falling back to regular FTP: {str(e)}")
# Fall back to regular FTP - only if explicitly allowed by configuration # Fall back to regular FTP - only if explicitly allowed by configuration
ftp = ftplib.FTP() # nosec B321 - Fallback to FTP intentional when configured ftp = ftplib.FTP() # nosec B321 - Fallback to FTP intentional when configured
ftp.connect( ftp.connect(host=settings.ftp_host, port=settings.ftp_port or 21)
host=settings.ftp_host,
port=settings.ftp_port or 21
)
# Login with credentials # Login with credentials
ftp.login( ftp.login(user=settings.ftp_username, passwd=settings.ftp_password)
user=settings.ftp_username,
passwd=settings.ftp_password
)
else: else:
# Check if plaintext is allowed when TLS is explicitly disabled # Check if plaintext is allowed when TLS is explicitly disabled
if not allow_plaintext: if not allow_plaintext:
@@ -99,16 +89,10 @@ def upload_to_ftp(self, file_path: str, file_id: int = None):
# Directly use regular FTP if TLS is explicitly disabled # Directly use regular FTP if TLS is explicitly disabled
logger.warning("Using plaintext FTP - connection is NOT encrypted!") logger.warning("Using plaintext FTP - connection is NOT encrypted!")
ftp = ftplib.FTP() # nosec B321 - Plaintext FTP intentional when explicitly configured ftp = ftplib.FTP() # nosec B321 - Plaintext FTP intentional when explicitly configured
ftp.connect( ftp.connect(host=settings.ftp_host, port=settings.ftp_port or 21)
host=settings.ftp_host,
port=settings.ftp_port or 21
)
# Login with credentials # Login with credentials
ftp.login( ftp.login(user=settings.ftp_username, passwd=settings.ftp_password)
user=settings.ftp_username,
passwd=settings.ftp_password
)
# Change to target directory if specified # Change to target directory if specified
if settings.ftp_folder: if settings.ftp_folder:
@@ -116,7 +100,7 @@ def upload_to_ftp(self, file_path: str, file_id: int = None):
# Try to navigate to the directory, create if it doesn't exist # Try to navigate to the directory, create if it doesn't exist
ftp_folder = settings.ftp_folder ftp_folder = settings.ftp_folder
# Remove leading slash if present # Remove leading slash if present
if ftp_folder.startswith('/'): if ftp_folder.startswith("/"):
ftp_folder = ftp_folder[1:] ftp_folder = ftp_folder[1:]
# Try to change to the directory # Try to change to the directory
@@ -124,8 +108,8 @@ def upload_to_ftp(self, file_path: str, file_id: int = None):
ftp.cwd(ftp_folder) ftp.cwd(ftp_folder)
except ftplib.error_perm: except ftplib.error_perm:
# Create directory structure if it doesn't exist # Create directory structure if it doesn't exist
folders = ftp_folder.split('/') folders = ftp_folder.split("/")
current_dir = '' current_dir = ""
for folder in folders: for folder in folders:
if folder: if folder:
current_dir += f"/{folder}" current_dir += f"/{folder}"
@@ -140,8 +124,8 @@ def upload_to_ftp(self, file_path: str, file_id: int = None):
raise Exception(error_msg) raise Exception(error_msg)
# Upload the file # Upload the file
with open(file_path, 'rb') as file_data: with open(file_path, "rb") as file_data:
ftp.storbinary(f'STOR {filename}', file_data) ftp.storbinary(f"STOR {filename}", file_data)
# Close FTP connection # Close FTP connection
ftp.quit() ftp.quit()
@@ -153,7 +137,7 @@ def upload_to_ftp(self, file_path: str, file_id: int = None):
"file": file_path, "file": file_path,
"ftp_host": settings.ftp_host, "ftp_host": settings.ftp_host,
"ftp_path": f"{settings.ftp_folder}/{filename}" if settings.ftp_folder else filename, "ftp_path": f"{settings.ftp_folder}/{filename}" if settings.ftp_folder else filename,
"used_tls": isinstance(ftp, ftplib.FTP_TLS) "used_tls": isinstance(ftp, ftplib.FTP_TLS),
} }
except Exception as e: except Exception as e:
+52 -44
View File
@@ -2,24 +2,25 @@
app/tasks/upload_to_google_drive.py app/tasks/upload_to_google_drive.py
""" """
import os
import json import json
import logging import logging
import os
from google.auth.exceptions import RefreshError
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials as OAuthCredentials
from google.oauth2.service_account import Credentials
from googleapiclient.discovery import build from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload from googleapiclient.http import MediaFileUpload
from google.oauth2.service_account import Credentials
from google.oauth2.credentials import Credentials as OAuthCredentials
from google_auth_oauthlib.flow import Flow
from google.auth.transport.requests import Request
from google.auth.exceptions import RefreshError
from app.celery_app import celery
from app.config import settings from app.config import settings
from app.tasks.retry_config import BaseTaskWithRetry from app.tasks.retry_config import BaseTaskWithRetry
from app.celery_app import celery
from app.utils import log_task_progress from app.utils import log_task_progress
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
def get_drive_service_oauth(): def get_drive_service_oauth():
""" """
Get Google Drive service using OAuth credentials. Get Google Drive service using OAuth credentials.
@@ -27,9 +28,11 @@ def get_drive_service_oauth():
""" """
try: try:
# Check for required OAuth settings # Check for required OAuth settings
if not (settings.google_drive_client_id and if not (
settings.google_drive_client_secret and settings.google_drive_client_id
settings.google_drive_refresh_token): and settings.google_drive_client_secret
and settings.google_drive_refresh_token
):
logger.error("Google Drive OAuth credentials not fully configured") logger.error("Google Drive OAuth credentials not fully configured")
return None return None
@@ -41,14 +44,14 @@ def get_drive_service_oauth():
client_id=settings.google_drive_client_id, client_id=settings.google_drive_client_id,
client_secret=settings.google_drive_client_secret, client_secret=settings.google_drive_client_secret,
# Use only drive.file scope # Use only drive.file scope
scopes=['https://www.googleapis.com/auth/drive.file'] scopes=["https://www.googleapis.com/auth/drive.file"],
) )
# Refresh the access token # Refresh the access token
credentials.refresh(Request()) credentials.refresh(Request())
# Build and return the service # Build and return the service
service = build('drive', 'v3', credentials=credentials) service = build("drive", "v3", credentials=credentials)
return service return service
except RefreshError as e: except RefreshError as e:
@@ -58,6 +61,7 @@ def get_drive_service_oauth():
logger.error(f"Failed to authenticate with Google Drive OAuth: {str(e)}") logger.error(f"Failed to authenticate with Google Drive OAuth: {str(e)}")
return None return None
def get_google_drive_service(): def get_google_drive_service():
""" """
Authenticate with Google Drive API using service account credentials Authenticate with Google Drive API using service account credentials
@@ -65,7 +69,7 @@ def get_google_drive_service():
""" """
try: try:
# Check if we should use OAuth instead of service account # Check if we should use OAuth instead of service account
if getattr(settings, 'google_drive_use_oauth', False): if getattr(settings, "google_drive_use_oauth", False):
return get_drive_service_oauth() return get_drive_service_oauth()
# Load service account credentials from settings # Load service account credentials from settings
@@ -75,8 +79,7 @@ def get_google_drive_service():
credentials_dict = json.loads(settings.google_drive_credentials_json) credentials_dict = json.loads(settings.google_drive_credentials_json)
credentials = Credentials.from_service_account_info( credentials = Credentials.from_service_account_info(
credentials_dict, credentials_dict, scopes=["https://www.googleapis.com/auth/drive"]
scopes=['https://www.googleapis.com/auth/drive']
) )
# Delegate to user if specified # Delegate to user if specified
@@ -84,13 +87,14 @@ def get_google_drive_service():
credentials = credentials.with_subject(settings.google_drive_delegate_to) credentials = credentials.with_subject(settings.google_drive_delegate_to)
# Build and return the service # Build and return the service
service = build('drive', 'v3', credentials=credentials) service = build("drive", "v3", credentials=credentials)
return service return service
except Exception as e: except Exception as e:
logger.error(f"Failed to authenticate with Google Drive: {str(e)}") logger.error(f"Failed to authenticate with Google Drive: {str(e)}")
return None return None
def extract_metadata_from_file(file_path): def extract_metadata_from_file(file_path):
""" """
Try to extract metadata from a file using several methods: Try to extract metadata from a file using several methods:
@@ -99,10 +103,10 @@ def extract_metadata_from_file(file_path):
Returns a dictionary of metadata or empty dict if not found Returns a dictionary of metadata or empty dict if not found
""" """
# Check for separate metadata JSON file # Check for separate metadata JSON file
metadata_path = os.path.splitext(file_path)[0] + '.json' metadata_path = os.path.splitext(file_path)[0] + ".json"
if os.path.exists(metadata_path): if os.path.exists(metadata_path):
try: try:
with open(metadata_path, 'r', encoding='utf-8') as f: with open(metadata_path, "r", encoding="utf-8") as f:
metadata = json.load(f) metadata = json.load(f)
logger.info(f"Loaded metadata from external JSON file: {metadata_path}") logger.info(f"Loaded metadata from external JSON file: {metadata_path}")
return metadata return metadata
@@ -111,6 +115,7 @@ def extract_metadata_from_file(file_path):
return {} return {}
def truncate_property_value(key, value, max_bytes=100): def truncate_property_value(key, value, max_bytes=100):
""" """
Truncate a property value to ensure the key+value stays under the byte limit. Truncate a property value to ensure the key+value stays under the byte limit.
@@ -121,8 +126,8 @@ def truncate_property_value(key, value, max_bytes=100):
str_value = str(value) str_value = str(value)
# Calculate current size of key and value in bytes # Calculate current size of key and value in bytes
key_bytes = len(key.encode('utf-8')) key_bytes = len(key.encode("utf-8"))
value_bytes = len(str_value.encode('utf-8')) value_bytes = len(str_value.encode("utf-8"))
total_bytes = key_bytes + value_bytes total_bytes = key_bytes + value_bytes
# If under limit, return original value # If under limit, return original value
@@ -134,7 +139,7 @@ def truncate_property_value(key, value, max_bytes=100):
bytes_to_trim = total_bytes - max_bytes + 4 bytes_to_trim = total_bytes - max_bytes + 4
# Iteratively truncate the string until it's under the byte limit # Iteratively truncate the string until it's under the byte limit
while len(str_value.encode('utf-8')) > value_bytes - bytes_to_trim: while len(str_value.encode("utf-8")) > value_bytes - bytes_to_trim:
str_value = str_value[:-1] str_value = str_value[:-1]
# Add ellipsis to indicate truncation # Add ellipsis to indicate truncation
@@ -143,6 +148,7 @@ def truncate_property_value(key, value, max_bytes=100):
return str_value return str_value
@celery.task(base=BaseTaskWithRetry, bind=True) @celery.task(base=BaseTaskWithRetry, bind=True)
def upload_to_google_drive(self, file_path: str, include_metadata=True, file_id: int = None): def upload_to_google_drive(self, file_path: str, include_metadata=True, file_id: int = None):
""" """
@@ -156,7 +162,11 @@ def upload_to_google_drive(self, file_path: str, include_metadata=True, file_id:
task_id = self.request.id task_id = self.request.id
logger.info(f"[{task_id}] Starting Google Drive upload: {file_path}") logger.info(f"[{task_id}] Starting Google Drive upload: {file_path}")
log_task_progress( log_task_progress(
task_id, "upload_to_google_drive", "in_progress", f"Uploading to Google Drive: {os.path.basename(file_path)}", file_id=file_id task_id,
"upload_to_google_drive",
"in_progress",
f"Uploading to Google Drive: {os.path.basename(file_path)}",
file_id=file_id,
) )
if not os.path.exists(file_path): if not os.path.exists(file_path):
@@ -184,17 +194,17 @@ def upload_to_google_drive(self, file_path: str, include_metadata=True, file_id:
# Prepare the file metadata # Prepare the file metadata
file_metadata = { file_metadata = {
'name': filename, "name": filename,
} }
# If folder ID is specified, set parent folder # If folder ID is specified, set parent folder
if settings.google_drive_folder_id: if settings.google_drive_folder_id:
file_metadata['parents'] = [settings.google_drive_folder_id] file_metadata["parents"] = [settings.google_drive_folder_id]
# Add custom properties if metadata exists # Add custom properties if metadata exists
if metadata: if metadata:
# Google Drive properties must be strings and can't be nested objects # Google Drive properties must be strings and can't be nested objects
file_metadata['properties'] = {} file_metadata["properties"] = {}
# Only add a few important top-level metadata fields as properties # Only add a few important top-level metadata fields as properties
# Skip nested objects and long values to avoid the 124-byte limit # Skip nested objects and long values to avoid the 124-byte limit
@@ -212,46 +222,44 @@ def upload_to_google_drive(self, file_path: str, include_metadata=True, file_id:
logger.warning(f"Skipping metadata property {key}: {str(e)}") logger.warning(f"Skipping metadata property {key}: {str(e)}")
# Only use the safe properties # Only use the safe properties
file_metadata['properties'] = safe_properties file_metadata["properties"] = safe_properties
# Add minimal appProperties # Add minimal appProperties
file_metadata['appProperties'] = { file_metadata["appProperties"] = {"docuelevate": "true"}
'docuelevate': 'true'
}
# Add metadata to file description for better visibility in Google Drive UI # Add metadata to file description for better visibility in Google Drive UI
# Description has much higher size limits than properties # Description has much higher size limits than properties
formatted_json = json.dumps(metadata, indent=2) formatted_json = json.dumps(metadata, indent=2)
file_metadata['description'] = f"Document Metadata:\n\n```json\n{formatted_json}\n```" file_metadata["description"] = f"Document Metadata:\n\n```json\n{formatted_json}\n```"
logger.debug(f"Adding metadata to Google Drive file: {json.dumps(file_metadata['properties'])}") logger.debug(f"Adding metadata to Google Drive file: {json.dumps(file_metadata['properties'])}")
# Upload file with metadata # Upload file with metadata
media = MediaFileUpload( media = MediaFileUpload(file_path, mimetype="application/pdf", resumable=True)
file_path,
mimetype='application/pdf', file = (
resumable=True service.files()
.create(
body=file_metadata, media_body=media, fields="id,name,webViewLink,properties,appProperties,description"
)
.execute()
) )
file = service.files().create(
body=file_metadata,
media_body=media,
fields='id,name,webViewLink,properties,appProperties,description'
).execute()
# Log success details # Log success details
google_drive_file_id = file.get('id') google_drive_file_id = file.get("id")
web_view_link = file.get('webViewLink') web_view_link = file.get("webViewLink")
logger.info(f"[{task_id}] Successfully uploaded {filename} to Google Drive with ID: {google_drive_file_id}") logger.info(f"[{task_id}] Successfully uploaded {filename} to Google Drive with ID: {google_drive_file_id}")
logger.info(f"[{task_id}] File accessible at: {web_view_link}") logger.info(f"[{task_id}] File accessible at: {web_view_link}")
log_task_progress(task_id, "upload_to_google_drive", "success", f"Uploaded to Google Drive: {filename}", file_id=file_id) log_task_progress(
task_id, "upload_to_google_drive", "success", f"Uploaded to Google Drive: {filename}", file_id=file_id
)
result = { result = {
"status": "Completed", "status": "Completed",
"file_path": file_path, "file_path": file_path,
"google_drive_file_id": google_drive_file_id, "google_drive_file_id": google_drive_file_id,
"google_drive_web_link": web_view_link "google_drive_web_link": web_view_link,
} }
# Add metadata info to result if included # Add metadata info to result if included
+46 -36
View File
@@ -1,19 +1,20 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import os
import logging import logging
import os
import requests import requests
from requests.auth import HTTPBasicAuth from requests.auth import HTTPBasicAuth
from app.config import settings
from app.celery_app import celery from app.celery_app import celery
from app.config import settings
from app.tasks.retry_config import BaseTaskWithRetry from app.tasks.retry_config import BaseTaskWithRetry
from app.utils.filename_utils import get_unique_filename, sanitize_filename, extract_remote_path
from app.utils import log_task_progress from app.utils import log_task_progress
from app.database import SessionLocal from app.utils.filename_utils import extract_remote_path, get_unique_filename
from app.models import FileRecord
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@celery.task(base=BaseTaskWithRetry, bind=True) @celery.task(base=BaseTaskWithRetry, bind=True)
def upload_to_nextcloud(self, file_path: str, file_id: int = None): def upload_to_nextcloud(self, file_path: str, file_id: int = None):
""" """
@@ -25,7 +26,13 @@ def upload_to_nextcloud(self, file_path: str, file_id: int = None):
""" """
task_id = self.request.id task_id = self.request.id
logger.info(f"[{task_id}] Starting Nextcloud upload: {file_path}") logger.info(f"[{task_id}] Starting Nextcloud upload: {file_path}")
log_task_progress(task_id, "upload_to_nextcloud", "in_progress", f"Uploading to Nextcloud: {os.path.basename(file_path)}", file_id=file_id) log_task_progress(
task_id,
"upload_to_nextcloud",
"in_progress",
f"Uploading to Nextcloud: {os.path.basename(file_path)}",
file_id=file_id,
)
if not os.path.exists(file_path): if not os.path.exists(file_path):
error_msg = f"File not found: {file_path}" error_msg = f"File not found: {file_path}"
@@ -35,43 +42,44 @@ def upload_to_nextcloud(self, file_path: str, file_id: int = None):
# For Nextcloud, we need to check for 'nextcloud_upload_url' instead of 'nextcloud_url' # For Nextcloud, we need to check for 'nextcloud_upload_url' instead of 'nextcloud_url'
# This is what's shown in your env view # This is what's shown in your env view
if not (getattr(settings, 'nextcloud_upload_url', None) and if not (
getattr(settings, 'nextcloud_username', None) and getattr(settings, "nextcloud_upload_url", None)
getattr(settings, 'nextcloud_password', None)): and getattr(settings, "nextcloud_username", None)
and getattr(settings, "nextcloud_password", None)
):
logger.info(f"[{task_id}] Nextcloud upload skipped: Missing configuration") logger.info(f"[{task_id}] Nextcloud upload skipped: Missing configuration")
log_task_progress(task_id, "upload_to_nextcloud", "success", "Skipped: Not configured", file_id=file_id) log_task_progress(task_id, "upload_to_nextcloud", "success", "Skipped: Not configured", file_id=file_id)
return {"status": "Skipped", "reason": "Nextcloud settings not configured"} return {"status": "Skipped", "reason": "Nextcloud settings not configured"}
filename = os.path.basename(file_path) filename = os.path.basename(file_path)
sanitized_filename = sanitize_filename(filename)
try: try:
# Prepare WebDAV URL - use nextcloud_upload_url instead of nextcloud_url # Prepare WebDAV URL - use nextcloud_upload_url instead of nextcloud_url
webdav_url = settings.nextcloud_upload_url webdav_url = settings.nextcloud_upload_url
if not webdav_url.endswith('/'): if not webdav_url.endswith("/"):
webdav_url += '/' webdav_url += "/"
# Calculate remote path based on local file structure # Calculate remote path based on local file structure
remote_base = getattr(settings, 'nextcloud_folder', '') or "" remote_base = getattr(settings, "nextcloud_folder", "") or ""
remote_path = extract_remote_path(file_path, settings.workdir, remote_base) remote_path = extract_remote_path(file_path, settings.workdir, remote_base)
full_url = f"{webdav_url}/{remote_path}" full_url = f"{webdav_url}/{remote_path}"
# Remove any double slashes (except in http://) # Remove any double slashes (except in http://)
full_url = full_url.replace('://', '$PLACEHOLDER$') full_url = full_url.replace("://", "$PLACEHOLDER$")
while '//' in full_url: while "//" in full_url:
full_url = full_url.replace('//', '/') full_url = full_url.replace("//", "/")
full_url = full_url.replace('$PLACEHOLDER$', '://') full_url = full_url.replace("$PLACEHOLDER$", "://")
# Function to check if file exists in Nextcloud # Function to check if file exists in Nextcloud
def check_exists_in_nextcloud(path): def check_exists_in_nextcloud(path):
check_url = f"{webdav_url}{os.path.dirname(path)}" check_url = f"{webdav_url}{os.path.dirname(path)}"
try: try:
response = requests.request( response = requests.request(
'PROPFIND', "PROPFIND",
check_url, check_url,
auth=HTTPBasicAuth(settings.nextcloud_username, settings.nextcloud_password), auth=HTTPBasicAuth(settings.nextcloud_username, settings.nextcloud_password),
headers={'Depth': '1'}, headers={"Depth": "1"},
timeout=10 timeout=10,
) )
return path in response.text return path in response.text
@@ -84,53 +92,55 @@ def upload_to_nextcloud(self, file_path: str, file_id: int = None):
full_url = f"{webdav_url}/{remote_path}" full_url = f"{webdav_url}/{remote_path}"
# Fix double slashes again # Fix double slashes again
full_url = full_url.replace('://', '$PLACEHOLDER$') full_url = full_url.replace("://", "$PLACEHOLDER$")
while '//' in full_url: while "//" in full_url:
full_url = full_url.replace('//', '/') full_url = full_url.replace("//", "/")
full_url = full_url.replace('$PLACEHOLDER$', '://') full_url = full_url.replace("$PLACEHOLDER$", "://")
# Create necessary parent folders # Create necessary parent folders
parent_dirs = os.path.dirname(remote_path) parent_dirs = os.path.dirname(remote_path)
if parent_dirs: if parent_dirs:
current_path = "" current_path = ""
for folder in parent_dirs.split('/'): for folder in parent_dirs.split("/"):
if not folder: if not folder:
continue continue
current_path += f"{folder}/" current_path += f"{folder}/"
mkdir_url = f"{webdav_url}/{current_path}" mkdir_url = f"{webdav_url}/{current_path}"
# Fix double slashes # Fix double slashes
mkdir_url = mkdir_url.replace('://', '$PLACEHOLDER$') mkdir_url = mkdir_url.replace("://", "$PLACEHOLDER$")
while '//' in mkdir_url: while "//" in mkdir_url:
mkdir_url = mkdir_url.replace('//', '/') mkdir_url = mkdir_url.replace("//", "/")
mkdir_url = mkdir_url.replace('$PLACEHOLDER$', '://') mkdir_url = mkdir_url.replace("$PLACEHOLDER$", "://")
requests.request( requests.request(
'MKCOL', "MKCOL",
mkdir_url, mkdir_url,
auth=HTTPBasicAuth(settings.nextcloud_username, settings.nextcloud_password), auth=HTTPBasicAuth(settings.nextcloud_username, settings.nextcloud_password),
timeout=10 timeout=10,
) )
# Upload the file # Upload the file
logger.info(f"[{task_id}] Uploading {filename} to Nextcloud at {full_url}") logger.info(f"[{task_id}] Uploading {filename} to Nextcloud at {full_url}")
log_task_progress(task_id, "upload_file", "in_progress", f"Uploading to {remote_path}", file_id=file_id) log_task_progress(task_id, "upload_file", "in_progress", f"Uploading to {remote_path}", file_id=file_id)
with open(file_path, 'rb') as file_data: with open(file_path, "rb") as file_data:
response = requests.put( response = requests.put(
full_url, full_url,
data=file_data, data=file_data,
auth=HTTPBasicAuth(settings.nextcloud_username, settings.nextcloud_password), auth=HTTPBasicAuth(settings.nextcloud_username, settings.nextcloud_password),
headers={'Content-Type': 'application/octet-stream'}, headers={"Content-Type": "application/octet-stream"},
timeout=settings.http_request_timeout # Use configured timeout for large files timeout=settings.http_request_timeout, # Use configured timeout for large files
) )
if response.status_code in (201, 204): # Created or No Content if response.status_code in (201, 204): # Created or No Content
logger.info(f"[{task_id}] Successfully uploaded {filename} to Nextcloud at {remote_path}") logger.info(f"[{task_id}] Successfully uploaded {filename} to Nextcloud at {remote_path}")
log_task_progress(task_id, "upload_to_nextcloud", "success", f"Uploaded to Nextcloud: {remote_path}", file_id=file_id) log_task_progress(
task_id, "upload_to_nextcloud", "success", f"Uploaded to Nextcloud: {remote_path}", file_id=file_id
)
return { return {
"status": "Completed", "status": "Completed",
"file_path": file_path, "file_path": file_path,
"nextcloud_path": remote_path, "nextcloud_path": remote_path,
"response_code": response.status_code "response_code": response.status_code,
} }
else: else:
error_msg = f"Failed to upload {filename} to Nextcloud: {response.status_code} - {response.text}" error_msg = f"Failed to upload {filename} to Nextcloud: {response.status_code} - {response.text}"
+27 -35
View File
@@ -1,18 +1,21 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import logging
import os import os
import time import time
import logging
import requests
import msal
import urllib.parse import urllib.parse
import msal
import requests
from app.celery_app import celery
from app.config import settings from app.config import settings
from app.tasks.retry_config import BaseTaskWithRetry from app.tasks.retry_config import BaseTaskWithRetry
from app.celery_app import celery
from app.utils import log_task_progress from app.utils import log_task_progress
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
def get_onedrive_token(): def get_onedrive_token():
""" """
Get an access token for Microsoft Graph API using the appropriate flow. Get an access token for Microsoft Graph API using the appropriate flow.
@@ -36,14 +39,13 @@ def get_onedrive_token():
app = msal.ConfidentialClientApplication( app = msal.ConfidentialClientApplication(
client_id=settings.onedrive_client_id, client_id=settings.onedrive_client_id,
client_credential=settings.onedrive_client_secret, client_credential=settings.onedrive_client_secret,
authority=f"https://login.microsoftonline.com/{tenant}" authority=f"https://login.microsoftonline.com/{tenant}",
) )
# Request new token using refresh token # Request new token using refresh token
logger.info("Attempting to acquire token using refresh token") logger.info("Attempting to acquire token using refresh token")
token_response = app.acquire_token_by_refresh_token( token_response = app.acquire_token_by_refresh_token(
refresh_token=settings.onedrive_refresh_token, refresh_token=settings.onedrive_refresh_token, scopes=scopes
scopes=scopes
) )
if "access_token" not in token_response: if "access_token" not in token_response:
@@ -51,7 +53,7 @@ def get_onedrive_token():
error_desc = token_response.get("error_description", "Unknown error") error_desc = token_response.get("error_description", "Unknown error")
# Log more details about the error # Log more details about the error
logger.error(f"Failed to get access token using refresh token") logger.error("Failed to get access token using refresh token")
logger.error(f"Error code: {error}") logger.error(f"Error code: {error}")
logger.error(f"Error description: {error_desc}") logger.error(f"Error description: {error_desc}")
@@ -78,13 +80,11 @@ def get_onedrive_token():
app = msal.ConfidentialClientApplication( app = msal.ConfidentialClientApplication(
client_id=settings.onedrive_client_id, client_id=settings.onedrive_client_id,
client_credential=settings.onedrive_client_secret, client_credential=settings.onedrive_client_secret,
authority=authority authority=authority,
) )
# Acquire token for application # Acquire token for application
token_response = app.acquire_token_for_client( token_response = app.acquire_token_for_client(scopes=scopes)
scopes=scopes
)
if "access_token" not in token_response: if "access_token" not in token_response:
error = token_response.get("error", "") error = token_response.get("error", "")
@@ -96,6 +96,7 @@ def get_onedrive_token():
else: else:
raise ValueError("For personal Microsoft accounts, ONEDRIVE_REFRESH_TOKEN must be configured") raise ValueError("For personal Microsoft accounts, ONEDRIVE_REFRESH_TOKEN must be configured")
def create_upload_session(filename, folder_path, access_token): def create_upload_session(filename, folder_path, access_token):
"""Creates an upload session for large files in Microsoft Graph API.""" """Creates an upload session for large files in Microsoft Graph API."""
# Construct the API endpoint # Construct the API endpoint
@@ -104,11 +105,11 @@ def create_upload_session(filename, folder_path, access_token):
# Format the folder path correctly and properly encode for URL # Format the folder path correctly and properly encode for URL
if folder_path: if folder_path:
# Remove leading/trailing slashes # Remove leading/trailing slashes
folder_path = folder_path.strip('/') folder_path = folder_path.strip("/")
# URL encode the path components separately # URL encode the path components separately
path_components = folder_path.split('/') path_components = folder_path.split("/")
encoded_path = '/'.join(urllib.parse.quote(component) for component in path_components) encoded_path = "/".join(urllib.parse.quote(component) for component in path_components)
# Also encode the filename # Also encode the filename
encoded_filename = urllib.parse.quote(filename) encoded_filename = urllib.parse.quote(filename)
@@ -121,16 +122,9 @@ def create_upload_session(filename, folder_path, access_token):
url = f"{base_url}{item_path}" url = f"{base_url}{item_path}"
# Add required request body (can be empty JSON object) # Add required request body (can be empty JSON object)
request_body = { request_body = {"item": {"@microsoft.graph.conflictBehavior": "replace"}}
"item": {
"@microsoft.graph.conflictBehavior": "replace"
}
}
headers = { headers = {"Authorization": f"Bearer {access_token}", "Content-Type": "application/json"}
"Authorization": f"Bearer {access_token}",
"Content-Type": "application/json"
}
logger.info(f"Creating upload session for {filename} at path {folder_path}") logger.info(f"Creating upload session for {filename} at path {folder_path}")
@@ -148,6 +142,7 @@ def create_upload_session(filename, folder_path, access_token):
logger.error(f"Request body: {request_body}") logger.error(f"Request body: {request_body}")
raise Exception(error_msg) raise Exception(error_msg)
def upload_large_file(file_path, upload_url): def upload_large_file(file_path, upload_url):
""" """
Upload a large file to OneDrive using the upload session URL. Upload a large file to OneDrive using the upload session URL.
@@ -160,7 +155,7 @@ def upload_large_file(file_path, upload_url):
chunk_size = 10 * 1024 * 1024 chunk_size = 10 * 1024 * 1024
# Open and read file in chunks # Open and read file in chunks
with open(file_path, 'rb') as f: with open(file_path, "rb") as f:
# Process file in chunks # Process file in chunks
chunk_number = 0 chunk_number = 0
while True: while True:
@@ -176,10 +171,7 @@ def upload_large_file(file_path, upload_url):
content_range = f"bytes {chunk_start}-{chunk_end}/{file_size}" content_range = f"bytes {chunk_start}-{chunk_end}/{file_size}"
# Upload chunk # Upload chunk
headers = { headers = {"Content-Length": str(len(chunk)), "Content-Range": content_range}
"Content-Length": str(len(chunk)),
"Content-Range": content_range
}
# Try to upload chunk with retries # Try to upload chunk with retries
max_retries = 3 max_retries = 3
@@ -188,10 +180,7 @@ def upload_large_file(file_path, upload_url):
for attempt in range(max_retries): for attempt in range(max_retries):
try: try:
response = requests.put( response = requests.put(
upload_url, upload_url, headers=headers, data=chunk, timeout=settings.http_request_timeout
headers=headers,
data=chunk,
timeout=settings.http_request_timeout
) )
# Check if successful # Check if successful
@@ -208,7 +197,9 @@ def upload_large_file(file_path, upload_url):
time.sleep(retry_delay * (attempt + 1)) time.sleep(retry_delay * (attempt + 1))
if response.status_code not in (201, 202): if response.status_code not in (201, 202):
raise Exception(f"Failed to upload chunk after {max_retries} attempts: {response.status_code} - {response.text}") raise Exception(
f"Failed to upload chunk after {max_retries} attempts: {response.status_code} - {response.text}"
)
# Move to next chunk # Move to next chunk
chunk_number += 1 chunk_number += 1
@@ -217,6 +208,7 @@ def upload_large_file(file_path, upload_url):
# The last response should contain the file metadata # The last response should contain the file metadata
return response.json() return response.json()
@celery.task(base=BaseTaskWithRetry, bind=True) @celery.task(base=BaseTaskWithRetry, bind=True)
def upload_to_onedrive(self, file_path: str, file_id: int = None): def upload_to_onedrive(self, file_path: str, file_id: int = None):
""" """
@@ -274,7 +266,7 @@ def upload_to_onedrive(self, file_path: str, file_id: int = None):
"status": "Completed", "status": "Completed",
"file_path": file_path, "file_path": file_path,
"onedrive_path": f"{settings.onedrive_folder_path}/{filename}", "onedrive_path": f"{settings.onedrive_folder_path}/{filename}",
"web_url": web_url "web_url": web_url,
} }
except Exception as e: except Exception as e:
+33 -27
View File
@@ -1,29 +1,26 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import os
import json
import time
import requests
import logging import logging
from typing import Dict, Any import os
import time
import requests
from app.celery_app import celery
from app.config import settings from app.config import settings
from app.tasks.retry_config import BaseTaskWithRetry from app.tasks.retry_config import BaseTaskWithRetry
from app.celery_app import celery
from app.utils import log_task_progress from app.utils import log_task_progress
from app.database import SessionLocal
from app.models import FileRecord
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
POLL_MAX_ATTEMPTS = 10 POLL_MAX_ATTEMPTS = 10
POLL_INTERVAL_SEC = 3 POLL_INTERVAL_SEC = 3
def _get_headers(): def _get_headers():
"""Returns HTTP headers for Paperless-ngx API calls.""" """Returns HTTP headers for Paperless-ngx API calls."""
return { return {"Authorization": f"Token {settings.paperless_ngx_api_token}"}
"Authorization": f"Token {settings.paperless_ngx_api_token}"
}
def _paperless_api_url(path: str) -> str: def _paperless_api_url(path: str) -> str:
""" """
@@ -35,6 +32,7 @@ def _paperless_api_url(path: str) -> str:
path = "/" + path path = "/" + path
return f"{host}{path}" return f"{host}{path}"
def poll_task_for_document_id(task_id: str) -> int: def poll_task_for_document_id(task_id: str) -> int:
""" """
Polls /api/tasks/?task_id=<uuid> until we get status=SUCCESS or FAILURE, Polls /api/tasks/?task_id=<uuid> until we get status=SUCCESS or FAILURE,
@@ -49,14 +47,13 @@ def poll_task_for_document_id(task_id: str) -> int:
while attempts < POLL_MAX_ATTEMPTS: while attempts < POLL_MAX_ATTEMPTS:
try: try:
resp = requests.get(url, headers=_get_headers(), params={"task_id": task_id}, timeout=settings.http_request_timeout) resp = requests.get(
url, headers=_get_headers(), params={"task_id": task_id}, timeout=settings.http_request_timeout
)
resp.raise_for_status() resp.raise_for_status()
tasks_data = resp.json() tasks_data = resp.json()
except requests.exceptions.RequestException as exc: except requests.exceptions.RequestException as exc:
logger.warning( logger.warning("Failed to poll for task_id='%s'. Attempt=%d Error=%s", task_id, attempts + 1, exc)
"Failed to poll for task_id='%s'. Attempt=%d Error=%s",
task_id, attempts + 1, exc
)
time.sleep(POLL_INTERVAL_SEC) time.sleep(POLL_INTERVAL_SEC)
attempts += 1 attempts += 1
continue continue
@@ -71,18 +68,15 @@ def poll_task_for_document_id(task_id: str) -> int:
doc_str = task_info.get("related_document") doc_str = task_info.get("related_document")
if doc_str: if doc_str:
return int(doc_str) return int(doc_str)
raise RuntimeError( raise RuntimeError(f"Task {task_id} completed but no doc ID found. Task info: {task_info}")
f"Task {task_id} completed but no doc ID found. Task info: {task_info}"
)
elif status == "FAILURE": elif status == "FAILURE":
raise RuntimeError(f"Task {task_id} failed: {task_info.get('result')}") raise RuntimeError(f"Task {task_id} failed: {task_info.get('result')}")
attempts += 1 attempts += 1
time.sleep(POLL_INTERVAL_SEC) time.sleep(POLL_INTERVAL_SEC)
raise TimeoutError( raise TimeoutError(f"Task {task_id} didn't reach SUCCESS within {POLL_MAX_ATTEMPTS} attempts.")
f"Task {task_id} didn't reach SUCCESS within {POLL_MAX_ATTEMPTS} attempts."
)
@celery.task(base=BaseTaskWithRetry, bind=True) @celery.task(base=BaseTaskWithRetry, bind=True)
def upload_to_paperless(self, file_path: str, file_id: int = None): def upload_to_paperless(self, file_path: str, file_id: int = None):
@@ -95,7 +89,13 @@ def upload_to_paperless(self, file_path: str, file_id: int = None):
""" """
task_id = self.request.id task_id = self.request.id
logger.info(f"[{task_id}] Starting Paperless upload: {file_path}") logger.info(f"[{task_id}] Starting Paperless upload: {file_path}")
log_task_progress(task_id, "upload_to_paperless", "in_progress", f"Uploading to Paperless: {os.path.basename(file_path)}", file_id=file_id) log_task_progress(
task_id,
"upload_to_paperless",
"in_progress",
f"Uploading to Paperless: {os.path.basename(file_path)}",
file_id=file_id,
)
if not os.path.exists(file_path): if not os.path.exists(file_path):
error_msg = f"File not found: {file_path}" error_msg = f"File not found: {file_path}"
@@ -125,13 +125,17 @@ def upload_to_paperless(self, file_path: str, file_id: int = None):
try: try:
logger.debug("Posting document to Paperless: file=%s", filename) logger.debug("Posting document to Paperless: file=%s", filename)
resp = requests.post(post_url, headers=_get_headers(), files=files, data=data, timeout=settings.http_request_timeout) resp = requests.post(
post_url, headers=_get_headers(), files=files, data=data, timeout=settings.http_request_timeout
)
resp.raise_for_status() resp.raise_for_status()
except requests.exceptions.RequestException as exc: except requests.exceptions.RequestException as exc:
error_msg = f"Failed to upload to Paperless: {exc}" error_msg = f"Failed to upload to Paperless: {exc}"
logger.error( logger.error(
f"[{task_id}] Failed to upload document '%s' to Paperless. Error: %s. Response=%s", f"[{task_id}] Failed to upload document '%s' to Paperless. Error: %s. Response=%s",
file_path, exc, getattr(exc.response, "text", "<no response>") file_path,
exc,
getattr(exc.response, "text", "<no response>"),
) )
log_task_progress(task_id, "upload_to_paperless", "failure", error_msg, file_id=file_id) log_task_progress(task_id, "upload_to_paperless", "failure", error_msg, file_id=file_id)
raise raise
@@ -145,11 +149,13 @@ def upload_to_paperless(self, file_path: str, file_id: int = None):
log_task_progress(task_id, "poll_task", "in_progress", "Waiting for Paperless processing", file_id=file_id) log_task_progress(task_id, "poll_task", "in_progress", "Waiting for Paperless processing", file_id=file_id)
doc_id = poll_task_for_document_id(raw_task_id) doc_id = poll_task_for_document_id(raw_task_id)
logger.info(f"[{task_id}] Document {file_path} successfully ingested => ID={doc_id}") logger.info(f"[{task_id}] Document {file_path} successfully ingested => ID={doc_id}")
log_task_progress(task_id, "upload_to_paperless", "success", f"Uploaded to Paperless: Doc ID {doc_id}", file_id=file_id) log_task_progress(
task_id, "upload_to_paperless", "success", f"Uploaded to Paperless: Doc ID {doc_id}", file_id=file_id
)
return { return {
"status": "Completed", "status": "Completed",
"paperless_task_id": raw_task_id, "paperless_task_id": raw_task_id,
"paperless_document_id": doc_id, "paperless_document_id": doc_id,
"file_path": file_path "file_path": file_path,
} }
+5 -8
View File
@@ -1,12 +1,14 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import os
import logging import logging
import os
import boto3 import boto3
from botocore.exceptions import ClientError from botocore.exceptions import ClientError
from app.celery_app import celery
from app.config import settings from app.config import settings
from app.tasks.retry_config import BaseTaskWithRetry from app.tasks.retry_config import BaseTaskWithRetry
from app.celery_app import celery
from app.utils import log_task_progress from app.utils import log_task_progress
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -76,12 +78,7 @@ def upload_to_s3(self, file_path: str, file_id: int = None):
extra_args["ACL"] = settings.s3_acl extra_args["ACL"] = settings.s3_acl
# Upload file # Upload file
s3_client.upload_file( s3_client.upload_file(file_path, settings.s3_bucket_name, s3_key, ExtraArgs=extra_args)
file_path,
settings.s3_bucket_name,
s3_key,
ExtraArgs=extra_args
)
# Generate URL to the file (useful for public files) # Generate URL to the file (useful for public files)
# For private files, this is just a reference and won't be accessible directly # For private files, this is just a reference and won't be accessible directly
+14 -16
View File
@@ -1,17 +1,19 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import os
import logging import logging
import os
import paramiko import paramiko
from pathlib import Path
from app.config import settings
from app.celery_app import celery from app.celery_app import celery
from app.config import settings
from app.tasks.retry_config import BaseTaskWithRetry from app.tasks.retry_config import BaseTaskWithRetry
from app.utils.filename_utils import get_unique_filename, sanitize_filename, extract_remote_path
from app.utils import log_task_progress from app.utils import log_task_progress
from app.utils.filename_utils import extract_remote_path, get_unique_filename, sanitize_filename
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@celery.task(base=BaseTaskWithRetry, bind=True) @celery.task(base=BaseTaskWithRetry, bind=True)
def upload_to_sftp(self, file_path: str, file_id: int = None): def upload_to_sftp(self, file_path: str, file_id: int = None):
""" """
@@ -48,7 +50,7 @@ def upload_to_sftp(self, file_path: str, file_id: int = None):
# Security: Host key verification # Security: Host key verification
# WARNING: AutoAddPolicy automatically trusts unknown host keys (vulnerable to MITM attacks) # WARNING: AutoAddPolicy automatically trusts unknown host keys (vulnerable to MITM attacks)
# For production, use RejectPolicy and configure known_hosts, or WarningPolicy at minimum # For production, use RejectPolicy and configure known_hosts, or WarningPolicy at minimum
if getattr(settings, 'sftp_disable_host_key_verification', True): if getattr(settings, "sftp_disable_host_key_verification", True):
logger.warning( logger.warning(
"SFTP host key verification is DISABLED - connections are vulnerable to MITM attacks. " "SFTP host key verification is DISABLED - connections are vulnerable to MITM attacks. "
"For production, set SFTP_DISABLE_HOST_KEY_VERIFICATION=false and configure known_hosts." "For production, set SFTP_DISABLE_HOST_KEY_VERIFICATION=false and configure known_hosts."
@@ -68,8 +70,8 @@ def upload_to_sftp(self, file_path: str, file_id: int = None):
} }
# Check for authentication methods - use key if available, otherwise try password # Check for authentication methods - use key if available, otherwise try password
sftp_key_path = getattr(settings, 'sftp_private_key', None) sftp_key_path = getattr(settings, "sftp_private_key", None)
sftp_key_passphrase = getattr(settings, 'sftp_private_key_passphrase', None) sftp_key_passphrase = getattr(settings, "sftp_private_key_passphrase", None)
if sftp_key_path and os.path.exists(sftp_key_path): if sftp_key_path and os.path.exists(sftp_key_path):
logger.info(f"Using SSH key authentication with key: {sftp_key_path}") logger.info(f"Using SSH key authentication with key: {sftp_key_path}")
@@ -96,8 +98,8 @@ def upload_to_sftp(self, file_path: str, file_id: int = None):
remote_path = extract_remote_path(file_path, settings.workdir, remote_base) remote_path = extract_remote_path(file_path, settings.workdir, remote_base)
# Ensure the remote path starts with a slash if the base folder does # Ensure the remote path starts with a slash if the base folder does
if remote_base.startswith('/') and not remote_path.startswith('/'): if remote_base.startswith("/") and not remote_path.startswith("/"):
remote_path = '/' + remote_path remote_path = "/" + remote_path
# Function to check if file exists in SFTP server # Function to check if file exists in SFTP server
def check_exists_in_sftp(path): def check_exists_in_sftp(path):
@@ -138,19 +140,15 @@ def upload_to_sftp(self, file_path: str, file_id: int = None):
sftp.close() sftp.close()
ssh.close() ssh.close()
return { return {"status": "Completed", "file_path": file_path, "sftp_path": remote_path}
"status": "Completed",
"file_path": file_path,
"sftp_path": remote_path
}
except Exception as e: except Exception as e:
# Make sure connections are closed # Make sure connections are closed
try: try:
if 'sftp' in locals(): if "sftp" in locals():
sftp.close() sftp.close()
ssh.close() ssh.close()
except: except Exception:
pass pass
error_msg = f"Failed to upload {filename} to SFTP server: {str(e)}" error_msg = f"Failed to upload {filename} to SFTP server: {str(e)}"
+15 -6
View File
@@ -1,16 +1,19 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import logging
import os import os
import requests
from urllib.parse import urljoin from urllib.parse import urljoin
import requests
from app.celery_app import celery
from app.config import settings from app.config import settings
from app.tasks.retry_config import BaseTaskWithRetry from app.tasks.retry_config import BaseTaskWithRetry
from app.celery_app import celery
from app.utils import log_task_progress from app.utils import log_task_progress
import logging
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@celery.task(base=BaseTaskWithRetry, bind=True) @celery.task(base=BaseTaskWithRetry, bind=True)
def upload_to_webdav(self, file_path: str, file_id: int = None): def upload_to_webdav(self, file_path: str, file_id: int = None):
""" """
@@ -23,7 +26,11 @@ def upload_to_webdav(self, file_path: str, file_id: int = None):
task_id = self.request.id task_id = self.request.id
logger.info(f"[{task_id}] Starting WebDAV upload: {file_path}") logger.info(f"[{task_id}] Starting WebDAV upload: {file_path}")
log_task_progress( log_task_progress(
task_id, "upload_to_webdav", "in_progress", f"Uploading to WebDAV: {os.path.basename(file_path)}", file_id=file_id task_id,
"upload_to_webdav",
"in_progress",
f"Uploading to WebDAV: {os.path.basename(file_path)}",
file_id=file_id,
) )
if not os.path.exists(file_path): if not os.path.exists(file_path):
@@ -65,13 +72,15 @@ def upload_to_webdav(self, file_path: str, file_id: int = None):
auth=(settings.webdav_username, settings.webdav_password), auth=(settings.webdav_username, settings.webdav_password),
data=file_data, data=file_data,
verify=settings.webdav_verify_ssl if hasattr(settings, "webdav_verify_ssl") else True, verify=settings.webdav_verify_ssl if hasattr(settings, "webdav_verify_ssl") else True,
timeout=settings.http_request_timeout timeout=settings.http_request_timeout,
) )
# Check if upload was successful # Check if upload was successful
if response.status_code in (200, 201, 204): if response.status_code in (200, 201, 204):
logger.info(f"[{task_id}] Successfully uploaded {filename} to WebDAV at {webdav_url}.") logger.info(f"[{task_id}] Successfully uploaded {filename} to WebDAV at {webdav_url}.")
log_task_progress(task_id, "upload_to_webdav", "success", f"Uploaded to WebDAV: {filename}", file_id=file_id) log_task_progress(
task_id, "upload_to_webdav", "success", f"Uploaded to WebDAV: {filename}", file_id=file_id
)
return {"status": "Completed", "file": file_path, "url": webdav_url} return {"status": "Completed", "file": file_path, "url": webdav_url}
else: else:
error_msg = f"Failed to upload {filename} to WebDAV: {response.status_code} - {response.text}" error_msg = f"Failed to upload {filename} to WebDAV: {response.status_code} - {response.text}"
+3 -2
View File
@@ -1,11 +1,12 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import logging
import os import os
import subprocess import subprocess
import logging
from app.celery_app import celery
from app.config import settings from app.config import settings
from app.tasks.retry_config import BaseTaskWithRetry from app.tasks.retry_config import BaseTaskWithRetry
from app.celery_app import celery
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
+2
View File
@@ -1,6 +1,7 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import logging import logging
import requests import requests
from celery import shared_task from celery import shared_task
@@ -8,6 +9,7 @@ from app.config import settings
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@shared_task @shared_task
def ping_uptime_kuma(): def ping_uptime_kuma():
""" """
+2 -2
View File
@@ -1,7 +1,7 @@
# app/utils.py # app/utils.py
# This file is deprecated. Functions have been moved to the utils package. # This file is deprecated. Functions have been moved to the utils package.
# To avoid breaking existing imports, we'll import and re-export the functions # To avoid breaking existing imports, we'll import and re-export the functions
from app.utils.file_operations import hash_file from app.utils.file_operations import hash_file # noqa: F401
from app.utils.logging import log_task_progress from app.utils.logging import log_task_progress # noqa: F401
# These functions are now available directly from the app.utils package # These functions are now available directly from the app.utils package
+1 -1
View File
@@ -7,4 +7,4 @@ from app.utils.file_operations import hash_file
from app.utils.logging import log_task_progress from app.utils.logging import log_task_progress
# Export all the functions that should be available when importing from app.utils # Export all the functions that should be available when importing from app.utils
__all__ = ['hash_file', 'log_task_progress'] __all__ = ["hash_file", "log_task_progress"]
+6 -5
View File
@@ -9,6 +9,7 @@ This module provides functionality to:
import logging import logging
from typing import Any, Optional, Union from typing import Any, Optional, Union
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
from app.models import ApplicationSettings from app.models import ApplicationSettings
@@ -79,15 +80,15 @@ def convert_setting_value(value: Optional[str], field_type: Any) -> Any:
return None return None
# Handle Optional types # Handle Optional types
origin = getattr(field_type, '__origin__', None) origin = getattr(field_type, "__origin__", None)
if origin is Union: if origin is Union:
# Get the non-None type from Union (for Optional) # Get the non-None type from Union (for Optional)
args = getattr(field_type, '__args__', ()) args = getattr(field_type, "__args__", ())
field_type = next((arg for arg in args if arg is not type(None)), str) field_type = next((arg for arg in args if arg is not type(None)), str)
# Convert based on type # Convert based on type
if field_type == bool: if field_type == bool:
return value.lower() in ('true', '1', 'yes', 'y', 't') return value.lower() in ("true", "1", "yes", "y", "t")
elif field_type == int: elif field_type == int:
try: try:
return int(value) return int(value)
@@ -100,10 +101,10 @@ def convert_setting_value(value: Optional[str], field_type: Any) -> Any:
except ValueError: except ValueError:
logger.warning(f"Failed to convert '{value}' to float, returning 0.0") logger.warning(f"Failed to convert '{value}' to float, returning 0.0")
return 0.0 return 0.0
elif field_type == list or getattr(field_type, '__origin__', None) == list: elif field_type == list or getattr(field_type, "__origin__", None) == list:
# Handle list types - assume comma-separated values # Handle list types - assume comma-separated values
if isinstance(value, str): if isinstance(value, str):
return [item.strip() for item in value.split(',') if item.strip()] return [item.strip() for item in value.split(",") if item.strip()]
return value return value
else: else:
# Default to string # Default to string
+16 -20
View File
@@ -4,29 +4,25 @@ Configuration validation for the application.
This file serves as a backward-compatible interface to the config_validator package. This file serves as a backward-compatible interface to the config_validator package.
""" """
# Import and re-export all functions from the new package
from app.utils.config_validator.validators import (
validate_email_config,
validate_storage_configs,
validate_notification_config,
check_all_configs
)
from app.utils.config_validator.masking import mask_sensitive_value from app.utils.config_validator.masking import mask_sensitive_value
from app.utils.config_validator.providers import get_provider_status from app.utils.config_validator.providers import get_provider_status
from app.utils.config_validator.settings_display import ( from app.utils.config_validator.settings_display import dump_all_settings, get_settings_for_display
get_settings_for_display,
dump_all_settings # Import and re-export all functions from the new package
from app.utils.config_validator.validators import (
check_all_configs,
validate_email_config,
validate_notification_config,
validate_storage_configs,
) )
__all__ = [ __all__ = [
'validate_email_config', "validate_email_config",
'validate_storage_configs', "validate_storage_configs",
'validate_notification_config', "validate_notification_config",
'mask_sensitive_value', "mask_sensitive_value",
'get_provider_status', "get_provider_status",
'get_settings_for_display', "get_settings_for_display",
'dump_all_settings', "dump_all_settings",
'check_all_configs' "check_all_configs",
] ]
+16 -19
View File
@@ -2,28 +2,25 @@
Configuration validation package for the application. Configuration validation package for the application.
""" """
from app.utils.config_validator.validators import (
validate_email_config,
validate_storage_configs,
validate_notification_config,
validate_auth_config,
check_all_configs
)
from app.utils.config_validator.masking import mask_sensitive_value from app.utils.config_validator.masking import mask_sensitive_value
from app.utils.config_validator.providers import get_provider_status from app.utils.config_validator.providers import get_provider_status
from app.utils.config_validator.settings_display import ( from app.utils.config_validator.settings_display import dump_all_settings, get_settings_for_display
get_settings_for_display, from app.utils.config_validator.validators import (
dump_all_settings check_all_configs,
validate_auth_config,
validate_email_config,
validate_notification_config,
validate_storage_configs,
) )
__all__ = [ __all__ = [
'validate_email_config', "validate_email_config",
'validate_storage_configs', "validate_storage_configs",
'validate_notification_config', "validate_notification_config",
'validate_auth_config', "validate_auth_config",
'mask_sensitive_value', "mask_sensitive_value",
'get_provider_status', "get_provider_status",
'get_settings_for_display', "get_settings_for_display",
'dump_all_settings', "dump_all_settings",
'check_all_configs' "check_all_configs",
] ]
+1
View File
@@ -2,6 +2,7 @@
Module for masking sensitive information in configuration values Module for masking sensitive information in configuration values
""" """
def mask_sensitive_value(value): def mask_sensitive_value(value):
""" """
Masks sensitive values like API keys in logs and output Masks sensitive values like API keys in logs and output
+162 -133
View File
@@ -5,6 +5,7 @@ Module for handling provider status information
from app.config import settings from app.config import settings
from app.utils.config_validator.masking import mask_sensitive_value from app.utils.config_validator.masking import mask_sensitive_value
def get_provider_status(): def get_provider_status():
""" """
Returns status information for all configured providers Returns status information for all configured providers
@@ -12,287 +13,315 @@ def get_provider_status():
providers = {} providers = {}
# Add Authentication configuration # Add Authentication configuration
auth_enabled = getattr(settings, 'auth_enabled', False) auth_enabled = getattr(settings, "auth_enabled", False)
using_oidc = bool(getattr(settings, 'authentik_client_id', None) and using_oidc = bool(
getattr(settings, 'authentik_client_secret', None) and getattr(settings, "authentik_client_id", None)
getattr(settings, 'authentik_config_url', None)) and getattr(settings, "authentik_client_secret", None)
and getattr(settings, "authentik_config_url", None)
)
auth_method = "OIDC" if using_oidc else "Basic Auth" if auth_enabled else "None" auth_method = "OIDC" if using_oidc else "Basic Auth" if auth_enabled else "None"
providers["Authentication"] = { providers["Authentication"] = {
"name": "Authentication", "name": "Authentication",
"icon": "fa-solid fa-lock", "icon": "fa-solid fa-lock",
"configured": bool(auth_enabled and "configured": bool(auth_enabled and (getattr(settings, "admin_username", None) or using_oidc)),
(getattr(settings, 'admin_username', None) or
using_oidc)),
"enabled": auth_enabled, "enabled": auth_enabled,
"description": "Access control and user authentication", "description": "Access control and user authentication",
"details": { "details": {
"method": auth_method, "method": auth_method,
"provider_name": getattr(settings, 'oauth_provider_name', 'Not set') if using_oidc else "N/A", "provider_name": getattr(settings, "oauth_provider_name", "Not set") if using_oidc else "N/A",
"session_security": "Configured" if getattr(settings, 'session_secret', None) else "Not configured" "session_security": "Configured" if getattr(settings, "session_secret", None) else "Not configured",
} },
} }
# Add Notification configuration - Make sure this provider is near the top of the list # Add Notification configuration - Make sure this provider is near the top of the list
providers["Notifications"] = { providers["Notifications"] = {
"name": "Notifications", "name": "Notifications",
"icon": "fa-solid fa-bell", "icon": "fa-solid fa-bell",
"configured": bool(getattr(settings, 'notification_urls', None)), "configured": bool(getattr(settings, "notification_urls", None)),
"enabled": True, "enabled": True,
"description": "Send system notifications via various services", "description": "Send system notifications via various services",
"details": { "details": {
"services": str(len(getattr(settings, 'notification_urls', []))) + " service(s) configured" if getattr(settings, 'notification_urls', None) else "Not configured", "services": (
"task_failure": getattr(settings, 'notify_on_task_failure', True), str(len(getattr(settings, "notification_urls", []))) + " service(s) configured"
"credential_failure": getattr(settings, 'notify_on_credential_failure', True), if getattr(settings, "notification_urls", None)
"startup": getattr(settings, 'notify_on_startup', True), else "Not configured"
"shutdown": getattr(settings, 'notify_on_shutdown', False) ),
"task_failure": getattr(settings, "notify_on_task_failure", True),
"credential_failure": getattr(settings, "notify_on_credential_failure", True),
"startup": getattr(settings, "notify_on_startup", True),
"shutdown": getattr(settings, "notify_on_shutdown", False),
}, },
"testable": True, "testable": True,
"test_endpoint": "/api/diagnostic/test-notification" "test_endpoint": "/api/diagnostic/test-notification",
} }
# Add AI services first # Add AI services first
providers["OpenAI"] = { providers["OpenAI"] = {
"name": "OpenAI", "name": "OpenAI",
"icon": "fa-brands fa-openai", "icon": "fa-brands fa-openai",
"configured": bool(getattr(settings, 'openai_api_key', None) and "configured": bool(
str(getattr(settings, 'openai_api_key', '')).startswith('sk-')), getattr(settings, "openai_api_key", None) and str(getattr(settings, "openai_api_key", "")).startswith("sk-")
),
"enabled": True, "enabled": True,
"description": "AI-powered document analysis and metadata extraction", "description": "AI-powered document analysis and metadata extraction",
"details": { "details": {
"api_key": mask_sensitive_value(getattr(settings, 'openai_api_key', None)), "api_key": mask_sensitive_value(getattr(settings, "openai_api_key", None)),
"base_url": getattr(settings, 'openai_base_url', 'https://api.openai.com/v1'), "base_url": getattr(settings, "openai_base_url", "https://api.openai.com/v1"),
"model": getattr(settings, 'openai_model', 'gpt-4') "model": getattr(settings, "openai_model", "gpt-4"),
} },
} }
providers["Azure AI"] = { providers["Azure AI"] = {
"name": "Azure AI", "name": "Azure AI",
"icon": "fa-solid fa-robot", "icon": "fa-solid fa-robot",
"configured": bool(getattr(settings, 'azure_ai_key', None) and "configured": bool(getattr(settings, "azure_ai_key", None) and getattr(settings, "azure_endpoint", None)),
getattr(settings, 'azure_endpoint', None)),
"enabled": True, "enabled": True,
"description": "Microsoft Azure Document Intelligence", "description": "Microsoft Azure Document Intelligence",
"details": { "details": {
"api_key": mask_sensitive_value(getattr(settings, 'azure_ai_key', None)), "api_key": mask_sensitive_value(getattr(settings, "azure_ai_key", None)),
"endpoint": getattr(settings, 'azure_endpoint', 'Not set'), "endpoint": getattr(settings, "azure_endpoint", "Not set"),
"region": getattr(settings, 'azure_region', 'Not set') "region": getattr(settings, "azure_region", "Not set"),
} },
} }
# Add Dropbox configuration - alphabetically ordered providers # Add Dropbox configuration - alphabetically ordered providers
providers["Dropbox"] = { providers["Dropbox"] = {
"name": "Dropbox", "name": "Dropbox",
"icon": "fa-brands fa-dropbox", "icon": "fa-brands fa-dropbox",
"configured": bool(getattr(settings, 'dropbox_app_key', None) and "configured": bool(
getattr(settings, 'dropbox_app_secret', None) and getattr(settings, "dropbox_app_key", None)
getattr(settings, 'dropbox_refresh_token', None)), and getattr(settings, "dropbox_app_secret", None)
and getattr(settings, "dropbox_refresh_token", None)
),
"enabled": True, "enabled": True,
"description": "Upload files to Dropbox cloud storage", "description": "Upload files to Dropbox cloud storage",
"details": { "details": {
"folder": getattr(settings, 'dropbox_folder', 'Not set'), "folder": getattr(settings, "dropbox_folder", "Not set"),
"app_key": getattr(settings, 'dropbox_app_key', 'Not set'), "app_key": getattr(settings, "dropbox_app_key", "Not set"),
"app_secret": mask_sensitive_value(getattr(settings, 'dropbox_app_secret', None)), "app_secret": mask_sensitive_value(getattr(settings, "dropbox_app_secret", None)),
"refresh_token": mask_sensitive_value(getattr(settings, 'dropbox_refresh_token', None)) "refresh_token": mask_sensitive_value(getattr(settings, "dropbox_refresh_token", None)),
} },
} }
# Add Email configuration # Add Email configuration
providers["Email"] = { providers["Email"] = {
"name": "Email", "name": "Email",
"icon": "fa-solid fa-envelope", "icon": "fa-solid fa-envelope",
"configured": bool(getattr(settings, 'email_host', None) and "configured": bool(
getattr(settings, 'email_default_recipient', None)), getattr(settings, "email_host", None) and getattr(settings, "email_default_recipient", None)
),
"enabled": True, "enabled": True,
"description": "Send documents via email", "description": "Send documents via email",
"details": { "details": {
"host": getattr(settings, 'email_host', 'Not set'), "host": getattr(settings, "email_host", "Not set"),
"port": getattr(settings, 'email_port', 'Not set'), "port": getattr(settings, "email_port", "Not set"),
"username": getattr(settings, 'email_username', 'Not set'), "username": getattr(settings, "email_username", "Not set"),
"password": mask_sensitive_value(getattr(settings, 'email_password', None)), "password": mask_sensitive_value(getattr(settings, "email_password", None)),
"use_tls": getattr(settings, 'email_use_tls', 'Not set'), "use_tls": getattr(settings, "email_use_tls", "Not set"),
"sender": getattr(settings, 'email_sender', 'Not set'), "sender": getattr(settings, "email_sender", "Not set"),
"default_recipient": getattr(settings, 'email_default_recipient', 'Not set') "default_recipient": getattr(settings, "email_default_recipient", "Not set"),
} },
} }
# Add FTP configuration to providers # Add FTP configuration to providers
providers["FTP Storage"] = { providers["FTP Storage"] = {
"name": "FTP Storage", "name": "FTP Storage",
"icon": "fa-solid fa-server", "icon": "fa-solid fa-server",
"configured": bool(getattr(settings, 'ftp_host', None) and "configured": bool(
getattr(settings, 'ftp_username', None) and getattr(settings, "ftp_host", None)
getattr(settings, 'ftp_password', None)), and getattr(settings, "ftp_username", None)
and getattr(settings, "ftp_password", None)
),
"enabled": True, "enabled": True,
"description": "Upload files to FTP server", "description": "Upload files to FTP server",
"details": { "details": {
"host": getattr(settings, 'ftp_host', 'Not set'), "host": getattr(settings, "ftp_host", "Not set"),
"port": getattr(settings, 'ftp_port', 'Not set'), "port": getattr(settings, "ftp_port", "Not set"),
"username": getattr(settings, 'ftp_username', 'Not set'), "username": getattr(settings, "ftp_username", "Not set"),
"password": mask_sensitive_value(getattr(settings, 'ftp_password', None)), "password": mask_sensitive_value(getattr(settings, "ftp_password", None)),
"folder": getattr(settings, 'ftp_folder', 'Not set'), "folder": getattr(settings, "ftp_folder", "Not set"),
"tls": getattr(settings, 'ftp_use_tls', True), "tls": getattr(settings, "ftp_use_tls", True),
"allow_plaintext": getattr(settings, 'ftp_allow_plaintext', True) "allow_plaintext": getattr(settings, "ftp_allow_plaintext", True),
} },
} }
# Check Google Drive configuration # Check Google Drive configuration
gdrive_oauth_configured = bool(getattr(settings, 'google_drive_client_id', None) and gdrive_oauth_configured = bool(
getattr(settings, 'google_drive_client_secret', None) and getattr(settings, "google_drive_client_id", None)
getattr(settings, 'google_drive_refresh_token', None)) and getattr(settings, "google_drive_client_secret", None)
and getattr(settings, "google_drive_refresh_token", None)
)
gdrive_sa_configured = bool(getattr(settings, 'google_drive_credentials_json', None)) gdrive_sa_configured = bool(getattr(settings, "google_drive_credentials_json", None))
# Determine if using OAuth or service account # Determine if using OAuth or service account
use_oauth = getattr(settings, 'google_drive_use_oauth', False) use_oauth = getattr(settings, "google_drive_use_oauth", False)
is_configured = (use_oauth and gdrive_oauth_configured) or (not use_oauth and gdrive_sa_configured) is_configured = (use_oauth and gdrive_oauth_configured) or (not use_oauth and gdrive_sa_configured)
providers["Google Drive"] = { providers["Google Drive"] = {
"name": "Google Drive", "name": "Google Drive",
"icon": "fa-brands fa-google-drive", "icon": "fa-brands fa-google-drive",
"configured": is_configured and bool(getattr(settings, 'google_drive_folder_id', None)), "configured": is_configured and bool(getattr(settings, "google_drive_folder_id", None)),
"enabled": True, "enabled": True,
"description": "Store documents in Google Drive", "description": "Store documents in Google Drive",
"details": { "details": {
"auth_type": "OAuth" if use_oauth else "Service Account", "auth_type": "OAuth" if use_oauth else "Service Account",
"client_id": getattr(settings, 'google_drive_client_id', 'Not set') if use_oauth else 'N/A', "client_id": getattr(settings, "google_drive_client_id", "Not set") if use_oauth else "N/A",
"client_secret": mask_sensitive_value(getattr(settings, 'google_drive_client_secret', None)) if use_oauth else 'N/A', "client_secret": (
"refresh_token": mask_sensitive_value(getattr(settings, 'google_drive_refresh_token', None)) if use_oauth else 'N/A', mask_sensitive_value(getattr(settings, "google_drive_client_secret", None)) if use_oauth else "N/A"
"credentials_json": mask_sensitive_value(getattr(settings, 'google_drive_credentials_json', None)) if not use_oauth else 'N/A', ),
"folder_id": getattr(settings, 'google_drive_folder_id', 'Not set'), "refresh_token": (
"delegate": getattr(settings, 'google_drive_delegate_to', 'Not set') if not use_oauth else 'N/A' mask_sensitive_value(getattr(settings, "google_drive_refresh_token", None)) if use_oauth else "N/A"
} ),
"credentials_json": (
mask_sensitive_value(getattr(settings, "google_drive_credentials_json", None))
if not use_oauth
else "N/A"
),
"folder_id": getattr(settings, "google_drive_folder_id", "Not set"),
"delegate": getattr(settings, "google_drive_delegate_to", "Not set") if not use_oauth else "N/A",
},
} }
# Check NextCloud configuration # Check NextCloud configuration
nextcloud_url = getattr(settings, 'nextcloud_upload_url', 'Not set') nextcloud_url = getattr(settings, "nextcloud_upload_url", "Not set")
# Extract base URL from WebDAV URL (remove the /remote.php part and everything after it) # Extract base URL from WebDAV URL (remove the /remote.php part and everything after it)
nextcloud_base_url = nextcloud_url nextcloud_base_url = nextcloud_url
if nextcloud_url != 'Not set' and nextcloud_url is not None and '/remote.php' in nextcloud_url: if nextcloud_url != "Not set" and nextcloud_url is not None and "/remote.php" in nextcloud_url:
nextcloud_base_url = nextcloud_url.split('/remote.php')[0] nextcloud_base_url = nextcloud_url.split("/remote.php")[0]
providers["NextCloud"] = { providers["NextCloud"] = {
"name": "NextCloud", "name": "NextCloud",
"icon": "fa-solid fa-cloud", "icon": "fa-solid fa-cloud",
"configured": bool(getattr(settings, 'nextcloud_upload_url', None) and "configured": bool(
getattr(settings, 'nextcloud_username', None) and getattr(settings, "nextcloud_upload_url", None)
getattr(settings, 'nextcloud_password', None)), and getattr(settings, "nextcloud_username", None)
and getattr(settings, "nextcloud_password", None)
),
"enabled": True, "enabled": True,
"description": "Store documents in NextCloud", "description": "Store documents in NextCloud",
"details": { "details": {
"url": getattr(settings, 'nextcloud_upload_url', 'Not set'), "url": getattr(settings, "nextcloud_upload_url", "Not set"),
"base_url": nextcloud_base_url, "base_url": nextcloud_base_url,
"username": getattr(settings, 'nextcloud_username', 'Not set'), "username": getattr(settings, "nextcloud_username", "Not set"),
"password": mask_sensitive_value(getattr(settings, 'nextcloud_password', None)), "password": mask_sensitive_value(getattr(settings, "nextcloud_password", None)),
"folder": getattr(settings, 'nextcloud_folder', 'Not set') "folder": getattr(settings, "nextcloud_folder", "Not set"),
} },
} }
# Check OneDrive configuration # Check OneDrive configuration
providers["OneDrive"] = { providers["OneDrive"] = {
"name": "OneDrive", "name": "OneDrive",
"icon": "fa-brands fa-microsoft", "icon": "fa-brands fa-microsoft",
"configured": bool(getattr(settings, 'onedrive_client_id', None) and "configured": bool(
getattr(settings, 'onedrive_client_secret', None) and getattr(settings, "onedrive_client_id", None)
getattr(settings, 'onedrive_refresh_token', None)), and getattr(settings, "onedrive_client_secret", None)
and getattr(settings, "onedrive_refresh_token", None)
),
"enabled": True, "enabled": True,
"description": "Store documents in Microsoft OneDrive", "description": "Store documents in Microsoft OneDrive",
"details": { "details": {
"client_id": getattr(settings, 'onedrive_client_id', 'Not set'), "client_id": getattr(settings, "onedrive_client_id", "Not set"),
"client_secret": mask_sensitive_value(getattr(settings, 'onedrive_client_secret', None)), "client_secret": mask_sensitive_value(getattr(settings, "onedrive_client_secret", None)),
"tenant_id": getattr(settings, 'onedrive_tenant_id', 'Not set'), "tenant_id": getattr(settings, "onedrive_tenant_id", "Not set"),
"refresh_token": mask_sensitive_value(getattr(settings, 'onedrive_refresh_token', None)), "refresh_token": mask_sensitive_value(getattr(settings, "onedrive_refresh_token", None)),
"folder": getattr(settings, 'onedrive_folder_path', 'Not set') "folder": getattr(settings, "onedrive_folder_path", "Not set"),
} },
} }
# Check Paperless configuration # Check Paperless configuration
providers["Paperless-ngx"] = { providers["Paperless-ngx"] = {
"name": "Paperless-ngx", "name": "Paperless-ngx",
"icon": "fa-solid fa-file-lines", "icon": "fa-solid fa-file-lines",
"configured": bool(getattr(settings, 'paperless_host', None) and "configured": bool(
getattr(settings, 'paperless_ngx_api_token', None)), getattr(settings, "paperless_host", None) and getattr(settings, "paperless_ngx_api_token", None)
),
"enabled": True, "enabled": True,
"description": "Document management system for digital archives", "description": "Document management system for digital archives",
"details": { "details": {
"host": getattr(settings, 'paperless_host', 'Not set'), "host": getattr(settings, "paperless_host", "Not set"),
"api_token": mask_sensitive_value(getattr(settings, 'paperless_ngx_api_token', None)) "api_token": mask_sensitive_value(getattr(settings, "paperless_ngx_api_token", None)),
} },
} }
# Check S3 configuration # Check S3 configuration
providers["S3 Storage"] = { providers["S3 Storage"] = {
"name": "S3 Storage", "name": "S3 Storage",
"icon": "fa-brands fa-aws", "icon": "fa-brands fa-aws",
"configured": bool(getattr(settings, 's3_bucket_name', None) and "configured": bool(
getattr(settings, 'aws_access_key_id', None) and getattr(settings, "s3_bucket_name", None)
getattr(settings, 'aws_secret_access_key', None)), and getattr(settings, "aws_access_key_id", None)
and getattr(settings, "aws_secret_access_key", None)
),
"enabled": True, "enabled": True,
"description": "Store documents in S3-compatible object storage", "description": "Store documents in S3-compatible object storage",
"details": { "details": {
"bucket": getattr(settings, 's3_bucket_name', 'Not set'), "bucket": getattr(settings, "s3_bucket_name", "Not set"),
"region": getattr(settings, 'aws_region', 'Not set'), "region": getattr(settings, "aws_region", "Not set"),
"access_key_id": getattr(settings, 'aws_access_key_id', 'Not set'), "access_key_id": getattr(settings, "aws_access_key_id", "Not set"),
"secret_access_key": mask_sensitive_value(getattr(settings, 'aws_secret_access_key', None)), "secret_access_key": mask_sensitive_value(getattr(settings, "aws_secret_access_key", None)),
"folder_prefix": getattr(settings, 's3_folder_prefix', 'Not set'), "folder_prefix": getattr(settings, "s3_folder_prefix", "Not set"),
"storage_class": getattr(settings, 's3_storage_class', 'Not set'), "storage_class": getattr(settings, "s3_storage_class", "Not set"),
"acl": getattr(settings, 's3_acl', 'Not set') "acl": getattr(settings, "s3_acl", "Not set"),
} },
} }
# Check SFTP configuration # Check SFTP configuration
providers["SFTP Storage"] = { providers["SFTP Storage"] = {
"name": "SFTP Storage", "name": "SFTP Storage",
"icon": "fa-solid fa-lock", "icon": "fa-solid fa-lock",
"configured": bool(getattr(settings, 'sftp_host', None) and "configured": bool(
getattr(settings, 'sftp_username', None) and getattr(settings, "sftp_host", None)
(getattr(settings, 'sftp_password', None) or and getattr(settings, "sftp_username", None)
getattr(settings, 'sftp_private_key', None))), and (getattr(settings, "sftp_password", None) or getattr(settings, "sftp_private_key", None))
),
"enabled": True, "enabled": True,
"description": "Upload files to SFTP server", "description": "Upload files to SFTP server",
"details": { "details": {
"host": getattr(settings, 'sftp_host', 'Not set'), "host": getattr(settings, "sftp_host", "Not set"),
"port": getattr(settings, 'sftp_port', 'Not set'), "port": getattr(settings, "sftp_port", "Not set"),
"username": getattr(settings, 'sftp_username', 'Not set'), "username": getattr(settings, "sftp_username", "Not set"),
"password": mask_sensitive_value(getattr(settings, 'sftp_password', None)), "password": mask_sensitive_value(getattr(settings, "sftp_password", None)),
"private_key": getattr(settings, 'sftp_private_key', 'Not set'), "private_key": getattr(settings, "sftp_private_key", "Not set"),
"private_key_passphrase": mask_sensitive_value(getattr(settings, 'sftp_private_key_passphrase', None)), "private_key_passphrase": mask_sensitive_value(getattr(settings, "sftp_private_key_passphrase", None)),
"folder": getattr(settings, 'sftp_folder', 'Not set') "folder": getattr(settings, "sftp_folder", "Not set"),
} },
} }
# Add Uptime Kuma configuration # Add Uptime Kuma configuration
providers["Uptime Kuma"] = { providers["Uptime Kuma"] = {
"name": "Uptime Kuma", "name": "Uptime Kuma",
"icon": "fa-solid fa-heart-pulse", "icon": "fa-solid fa-heart-pulse",
"configured": bool(getattr(settings, 'uptime_kuma_url', None)), "configured": bool(getattr(settings, "uptime_kuma_url", None)),
"enabled": True, "enabled": True,
"description": "Server monitoring and status page", "description": "Server monitoring and status page",
"details": { "details": {
"url": getattr(settings, 'uptime_kuma_url', 'Not set'), "url": getattr(settings, "uptime_kuma_url", "Not set"),
"ping_interval": getattr(settings, 'uptime_kuma_ping_interval', 'Not set') "ping_interval": getattr(settings, "uptime_kuma_ping_interval", "Not set"),
} },
} }
# Check WebDAV configuration # Check WebDAV configuration
providers["WebDAV"] = { providers["WebDAV"] = {
"name": "WebDAV", "name": "WebDAV",
"icon": "fa-solid fa-globe", "icon": "fa-solid fa-globe",
"configured": bool(getattr(settings, 'webdav_url', None) and "configured": bool(
getattr(settings, 'webdav_username', None) and getattr(settings, "webdav_url", None)
getattr(settings, 'webdav_password', None)), and getattr(settings, "webdav_username", None)
and getattr(settings, "webdav_password", None)
),
"enabled": True, "enabled": True,
"description": "Store documents on WebDAV servers", "description": "Store documents on WebDAV servers",
"details": { "details": {
"url": getattr(settings, 'webdav_url', 'Not set'), "url": getattr(settings, "webdav_url", "Not set"),
"username": getattr(settings, 'webdav_username', 'Not set'), "username": getattr(settings, "webdav_username", "Not set"),
"password": mask_sensitive_value(getattr(settings, 'webdav_password', None)), "password": mask_sensitive_value(getattr(settings, "webdav_password", None)),
"folder": getattr(settings, 'webdav_folder', 'Not set'), "folder": getattr(settings, "webdav_folder", "Not set"),
"verify_ssl": getattr(settings, 'webdav_verify_ssl', 'Not set') "verify_ssl": getattr(settings, "webdav_verify_ssl", "Not set"),
},
} }
}
return providers return providers
+61 -71
View File
@@ -3,31 +3,47 @@ Module for displaying and organizing settings information
""" """
import logging import logging
from app.config import settings from app.config import settings
from app.utils.config_validator.masking import mask_sensitive_value from app.utils.config_validator.masking import mask_sensitive_value
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
def dump_all_settings(): def dump_all_settings():
"""Log all settings values for diagnostic purposes""" """Log all settings values for diagnostic purposes"""
logger.info("--- DUMPING ALL SETTINGS FOR DIAGNOSTIC PURPOSES ---") logger.info("--- DUMPING ALL SETTINGS FOR DIAGNOSTIC PURPOSES ---")
for key in dir(settings): for key in dir(settings):
if not key.startswith('_') and not callable(getattr(settings, key)): if not key.startswith("_") and not callable(getattr(settings, key)):
value = getattr(settings, key) value = getattr(settings, key)
# Mask sensitive values in logs # Mask sensitive values in logs
if key.lower().find('password') >= 0 or key.lower().find('secret') >= 0 or key.lower().find('token') >= 0 or key.lower().find('key') >= 0: if (
key.lower().find("password") >= 0
or key.lower().find("secret") >= 0
or key.lower().find("token") >= 0
or key.lower().find("key") >= 0
):
if value: if value:
if isinstance(value, str) and len(value) > 10: if isinstance(value, str) and len(value) > 10:
visible_start = max(1, len(value) // 3) visible_start = max(1, len(value) // 3)
visible_end = max(1, len(value) // 4) visible_end = max(1, len(value) // 4)
value = f"{value[:visible_start]}{'*' * (len(value) - visible_start - visible_end)}{value[-visible_end:]}" value = (
f"{value[:visible_start]}"
f"{'*' * (len(value) - visible_start - visible_end)}"
f"{value[-visible_end:]}"
)
else: else:
value = f"{value[:2]}{'*' * (len(value) - 4)}{value[-2:]}" if isinstance(value, str) and len(value) > 4 else "****" value = (
f"{value[:2]}{'*' * (len(value) - 4)}{value[-2:]}"
if isinstance(value, str) and len(value) > 4
else "****"
)
# Special handling for notification URLs # Special handling for notification URLs
if key == 'notification_urls' and value: if key == "notification_urls" and value:
try: try:
from app.utils.notification import _mask_sensitive_url from app.utils.notification import _mask_sensitive_url
if isinstance(value, list): if isinstance(value, list):
masked_urls = [_mask_sensitive_url(url) for url in value] masked_urls = [_mask_sensitive_url(url) for url in value]
logger.info(f"{key}: {masked_urls}") logger.info(f"{key}: {masked_urls}")
@@ -40,6 +56,7 @@ def dump_all_settings():
logger.info(f"{key}: {value}") logger.info(f"{key}: {value}")
logger.info("--- END OF SETTINGS DUMP ---") logger.info("--- END OF SETTINGS DUMP ---")
def get_settings_for_display(show_values=False): def get_settings_for_display(show_values=False):
""" """
Group settings into logical categories and check if they are configured. Group settings into logical categories and check if they are configured.
@@ -51,16 +68,8 @@ def get_settings_for_display(show_values=False):
# First include system info with version in result # First include system info with version in result
result = { result = {
"System Info": [ "System Info": [
{ {"name": "App Version", "value": settings.version, "is_configured": True},
"name": "App Version", {"name": "Build Date", "value": settings.build_date, "is_configured": True},
"value": settings.version,
"is_configured": True
},
{
"name": "Build Date",
"value": settings.build_date,
"is_configured": True
}
] ]
} }
@@ -73,7 +82,7 @@ def get_settings_for_display(show_values=False):
"database_url", "database_url",
"redis_url", "redis_url",
"gotenberg_url", "gotenberg_url",
"allow_file_delete" # Added allow_file_delete to Core settings "allow_file_delete", # Added allow_file_delete to Core settings
], ],
"Authentication": [ "Authentication": [
"auth_enabled", "auth_enabled",
@@ -83,7 +92,7 @@ def get_settings_for_display(show_values=False):
"authentik_client_id", "authentik_client_id",
"authentik_client_secret", "authentik_client_secret",
"authentik_config_url", "authentik_config_url",
"oauth_provider_name" "oauth_provider_name",
], ],
"Email": [ "Email": [
"email_host", "email_host",
@@ -92,7 +101,7 @@ def get_settings_for_display(show_values=False):
"email_password", "email_password",
"email_use_tls", "email_use_tls",
"email_sender", "email_sender",
"email_default_recipient" "email_default_recipient",
], ],
"IMAP": [ "IMAP": [
"imap1_host", "imap1_host",
@@ -108,24 +117,11 @@ def get_settings_for_display(show_values=False):
"imap2_password", "imap2_password",
"imap2_ssl", "imap2_ssl",
"imap2_poll_interval_minutes", "imap2_poll_interval_minutes",
"imap2_delete_after_process" "imap2_delete_after_process",
],
"Dropbox": [
"dropbox_app_key",
"dropbox_app_secret",
"dropbox_folder",
"dropbox_refresh_token"
],
"NextCloud": [
"nextcloud_upload_url",
"nextcloud_username",
"nextcloud_password",
"nextcloud_folder"
],
"Paperless": [
"paperless_host",
"paperless_ngx_api_token"
], ],
"Dropbox": ["dropbox_app_key", "dropbox_app_secret", "dropbox_folder", "dropbox_refresh_token"],
"NextCloud": ["nextcloud_upload_url", "nextcloud_username", "nextcloud_password", "nextcloud_folder"],
"Paperless": ["paperless_host", "paperless_ngx_api_token"],
"Google Drive": [ "Google Drive": [
"google_drive_use_oauth", "google_drive_use_oauth",
"google_drive_client_id", "google_drive_client_id",
@@ -133,22 +129,16 @@ def get_settings_for_display(show_values=False):
"google_drive_refresh_token", "google_drive_refresh_token",
"google_drive_credentials_json", "google_drive_credentials_json",
"google_drive_folder_id", "google_drive_folder_id",
"google_drive_delegate_to" "google_drive_delegate_to",
], ],
"OneDrive": [ "OneDrive": [
"onedrive_client_id", "onedrive_client_id",
"onedrive_client_secret", "onedrive_client_secret",
"onedrive_tenant_id", "onedrive_tenant_id",
"onedrive_refresh_token", "onedrive_refresh_token",
"onedrive_folder_path" "onedrive_folder_path",
],
"WebDAV": [
"webdav_url",
"webdav_username",
"webdav_password",
"webdav_folder",
"webdav_verify_ssl"
], ],
"WebDAV": ["webdav_url", "webdav_username", "webdav_password", "webdav_folder", "webdav_verify_ssl"],
"SFTP": [ "SFTP": [
"sftp_host", "sftp_host",
"sftp_port", "sftp_port",
@@ -156,7 +146,7 @@ def get_settings_for_display(show_values=False):
"sftp_password", "sftp_password",
"sftp_folder", "sftp_folder",
"sftp_private_key", "sftp_private_key",
"sftp_private_key_passphrase" "sftp_private_key_passphrase",
], ],
"FTP": [ "FTP": [
"ftp_host", "ftp_host",
@@ -165,7 +155,7 @@ def get_settings_for_display(show_values=False):
"ftp_password", "ftp_password",
"ftp_folder", "ftp_folder",
"ftp_use_tls", "ftp_use_tls",
"ftp_allow_plaintext" "ftp_allow_plaintext",
], ],
"S3/AWS": [ "S3/AWS": [
"aws_access_key_id", "aws_access_key_id",
@@ -174,7 +164,7 @@ def get_settings_for_display(show_values=False):
"s3_bucket_name", "s3_bucket_name",
"s3_folder_prefix", "s3_folder_prefix",
"s3_storage_class", "s3_storage_class",
"s3_acl" "s3_acl",
], ],
"AI Services": [ "AI Services": [
"openai_api_key", "openai_api_key",
@@ -182,28 +172,28 @@ def get_settings_for_display(show_values=False):
"openai_model", "openai_model",
"azure_ai_key", "azure_ai_key",
"azure_endpoint", "azure_endpoint",
"azure_region" "azure_region",
],
"Monitoring": [
"uptime_kuma_url",
"uptime_kuma_ping_interval"
], ],
"Monitoring": ["uptime_kuma_url", "uptime_kuma_ping_interval"],
"Notifications": [ "Notifications": [
"notification_urls", "notification_urls",
"notify_on_task_failure", "notify_on_task_failure",
"notify_on_credential_failure", "notify_on_credential_failure",
"notify_on_startup", "notify_on_startup",
"notify_on_shutdown" "notify_on_shutdown",
] ],
} }
# Handle any settings that don't fit into the predefined categories # Handle any settings that don't fit into the predefined categories
all_settings = set([key for key in dir(settings) all_settings = set(
if not key.startswith('_') and [
not callable(getattr(settings, key)) and key
key not in ["model_computed_fields", "model_config", for key in dir(settings)
"model_extra", "model_fields", if not key.startswith("_")
"model_fields_set"]]) and not callable(getattr(settings, key))
and key not in ["model_computed_fields", "model_config", "model_extra", "model_fields", "model_fields_set"]
]
)
# Ensure 'version' is excluded since we display it separately # Ensure 'version' is excluded since we display it separately
all_settings.discard("version") all_settings.discard("version")
@@ -225,20 +215,24 @@ def get_settings_for_display(show_values=False):
# List of patterns that indicate sensitive values # List of patterns that indicate sensitive values
sensitive_patterns = [ sensitive_patterns = [
'password', 'secret', 'token', 'api_key', 'private_key', "password",
'credentials', 'access_key', 'ai_key' "secret",
"token",
"api_key",
"private_key",
"credentials",
"access_key",
"ai_key",
] ]
# Check if this is a sensitive value that should be masked # Check if this is a sensitive value that should be masked
is_sensitive = any( is_sensitive = any(pattern in key.lower() for pattern in sensitive_patterns)
pattern in key.lower() for pattern in sensitive_patterns
)
# Special handling for "auth" to avoid matching prefixes like "authentik" # Special handling for "auth" to avoid matching prefixes like "authentik"
if not is_sensitive and "auth" in key.lower(): if not is_sensitive and "auth" in key.lower():
# Only mark as sensitive if "auth" is a standalone word or at the end # Only mark as sensitive if "auth" is a standalone word or at the end
# This avoids matching "authentik" as sensitive # This avoids matching "authentik" as sensitive
parts = key.lower().split('_') parts = key.lower().split("_")
is_sensitive = any(part == "auth" for part in parts) or key.lower().endswith("auth") is_sensitive = any(part == "auth" for part in parts) or key.lower().endswith("auth")
# Mask sensitive values regardless of debug mode # Mask sensitive values regardless of debug mode
@@ -253,11 +247,7 @@ def get_settings_for_display(show_values=False):
if is_configured and isinstance(value, str): if is_configured and isinstance(value, str):
is_configured = len(value) > 0 is_configured = len(value) > 0
items.append({ items.append({"name": key, "value": value, "is_configured": is_configured})
"name": key,
"value": value,
"is_configured": is_configured
})
if items: # Only add categories that have items if items: # Only add categories that have items
result[category] = items result[category] = items
+72 -62
View File
@@ -1,24 +1,26 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import logging
import os import os
import socket import socket
import logging
from app.config import settings from app.config import settings
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
def validate_email_config(): def validate_email_config():
"""Validates email configuration settings""" """Validates email configuration settings"""
issues = [] issues = []
# Check for required email settings # Check for required email settings
if not getattr(settings, 'email_host', None): if not getattr(settings, "email_host", None):
issues.append("EMAIL_HOST is not configured") issues.append("EMAIL_HOST is not configured")
if not getattr(settings, 'email_port', None): if not getattr(settings, "email_port", None):
issues.append("EMAIL_PORT is not configured") issues.append("EMAIL_PORT is not configured")
# Test SMTP server connectivity if host is configured # Test SMTP server connectivity if host is configured
if getattr(settings, 'email_host', None) and getattr(settings, 'email_port', None): if getattr(settings, "email_host", None) and getattr(settings, "email_port", None):
try: try:
# Attempt to resolve the hostname # Attempt to resolve the hostname
socket.gethostbyname(settings.email_host) socket.gethostbyname(settings.email_host)
@@ -26,156 +28,168 @@ def validate_email_config():
issues.append(f"Cannot resolve email host: {settings.email_host}") issues.append(f"Cannot resolve email host: {settings.email_host}")
# Check for authentication settings # Check for authentication settings
if not getattr(settings, 'email_username', None): if not getattr(settings, "email_username", None):
issues.append("EMAIL_USERNAME is not configured") issues.append("EMAIL_USERNAME is not configured")
if not getattr(settings, 'email_password', None): if not getattr(settings, "email_password", None):
issues.append("EMAIL_PASSWORD is not configured") issues.append("EMAIL_PASSWORD is not configured")
return issues return issues
def validate_auth_config(): def validate_auth_config():
"""Validates authentication configuration settings""" """Validates authentication configuration settings"""
issues = [] issues = []
# If auth is enabled, check for required settings # If auth is enabled, check for required settings
if getattr(settings, 'auth_enabled', False): if getattr(settings, "auth_enabled", False):
# Check for session secret # Check for session secret
if not getattr(settings, 'session_secret', None): if not getattr(settings, "session_secret", None):
issues.append("SESSION_SECRET is not configured but AUTH_ENABLED is True") issues.append("SESSION_SECRET is not configured but AUTH_ENABLED is True")
elif len(getattr(settings, 'session_secret', '')) < 32: elif len(getattr(settings, "session_secret", "")) < 32:
issues.append("SESSION_SECRET must be at least 32 characters long") issues.append("SESSION_SECRET must be at least 32 characters long")
# Check if using simple authentication or OIDC # Check if using simple authentication or OIDC
using_simple_auth = bool(getattr(settings, 'admin_username', None) and using_simple_auth = bool(
getattr(settings, 'admin_password', None)) getattr(settings, "admin_username", None) and getattr(settings, "admin_password", None)
)
using_oidc = bool(getattr(settings, 'authentik_client_id', None) and using_oidc = bool(
getattr(settings, 'authentik_client_secret', None) and getattr(settings, "authentik_client_id", None)
getattr(settings, 'authentik_config_url', None)) and getattr(settings, "authentik_client_secret", None)
and getattr(settings, "authentik_config_url", None)
)
if not using_simple_auth and not using_oidc: if not using_simple_auth and not using_oidc:
issues.append("Neither simple authentication nor OIDC are properly configured") issues.append("Neither simple authentication nor OIDC are properly configured")
# If using OIDC, check for provider name # If using OIDC, check for provider name
if using_oidc and not getattr(settings, 'oauth_provider_name', None): if using_oidc and not getattr(settings, "oauth_provider_name", None):
issues.append("OAUTH_PROVIDER_NAME is not configured but OIDC is enabled") issues.append("OAUTH_PROVIDER_NAME is not configured but OIDC is enabled")
return issues return issues
def validate_storage_configs(): def validate_storage_configs():
"""Validates configuration for all storage providers""" """Validates configuration for all storage providers"""
issues = {} issues = {}
# Validate Dropbox config # Validate Dropbox config
dropbox_issues = [] dropbox_issues = []
if not (getattr(settings, 'dropbox_app_key', None) and if not (
getattr(settings, 'dropbox_app_secret', None) and getattr(settings, "dropbox_app_key", None)
getattr(settings, 'dropbox_refresh_token', None)): and getattr(settings, "dropbox_app_secret", None)
and getattr(settings, "dropbox_refresh_token", None)
):
dropbox_issues.append("Dropbox credentials are not fully configured") dropbox_issues.append("Dropbox credentials are not fully configured")
issues['dropbox'] = dropbox_issues issues["dropbox"] = dropbox_issues
# Validate Nextcloud config # Validate Nextcloud config
nextcloud_issues = [] nextcloud_issues = []
if not (getattr(settings, 'nextcloud_upload_url', None) and if not (
getattr(settings, 'nextcloud_username', None) and getattr(settings, "nextcloud_upload_url", None)
getattr(settings, 'nextcloud_password', None)): and getattr(settings, "nextcloud_username", None)
and getattr(settings, "nextcloud_password", None)
):
nextcloud_issues.append("Nextcloud credentials are not fully configured") nextcloud_issues.append("Nextcloud credentials are not fully configured")
issues['nextcloud'] = nextcloud_issues issues["nextcloud"] = nextcloud_issues
# Validate SFTP config # Validate SFTP config
sftp_issues = [] sftp_issues = []
if not getattr(settings, 'sftp_host', None): if not getattr(settings, "sftp_host", None):
sftp_issues.append("SFTP_HOST is not configured") sftp_issues.append("SFTP_HOST is not configured")
sftp_key_path = getattr(settings, 'sftp_private_key', None) sftp_key_path = getattr(settings, "sftp_private_key", None)
if sftp_key_path and not os.path.exists(sftp_key_path): if sftp_key_path and not os.path.exists(sftp_key_path):
sftp_issues.append(f"SFTP_KEY_PATH file not found: {sftp_key_path}") sftp_issues.append(f"SFTP_KEY_PATH file not found: {sftp_key_path}")
if not sftp_key_path and not getattr(settings, 'sftp_password', None): if not sftp_key_path and not getattr(settings, "sftp_password", None):
sftp_issues.append("Neither SFTP_KEY_PATH nor SFTP_PASSWORD is configured") sftp_issues.append("Neither SFTP_KEY_PATH nor SFTP_PASSWORD is configured")
issues['sftp'] = sftp_issues issues["sftp"] = sftp_issues
# Validate Email sending # Validate Email sending
email_issues = [] email_issues = []
if not getattr(settings, 'email_host', None): if not getattr(settings, "email_host", None):
email_issues.append("EMAIL_HOST is not configured") email_issues.append("EMAIL_HOST is not configured")
if not getattr(settings, 'email_default_recipient', None): if not getattr(settings, "email_default_recipient", None):
email_issues.append("EMAIL_DEFAULT_RECIPIENT is not configured") email_issues.append("EMAIL_DEFAULT_RECIPIENT is not configured")
issues['email'] = email_issues issues["email"] = email_issues
# Validate S3 # Validate S3
s3_issues = [] s3_issues = []
if not getattr(settings, 's3_bucket_name', None): if not getattr(settings, "s3_bucket_name", None):
s3_issues.append("S3_BUCKET_NAME is not configured") s3_issues.append("S3_BUCKET_NAME is not configured")
if not (getattr(settings, 'aws_access_key_id', None) and if not (getattr(settings, "aws_access_key_id", None) and getattr(settings, "aws_secret_access_key", None)):
getattr(settings, 'aws_secret_access_key', None)):
s3_issues.append("AWS credentials are not configured") s3_issues.append("AWS credentials are not configured")
issues['s3'] = s3_issues issues["s3"] = s3_issues
# Validate FTP # Validate FTP
ftp_issues = [] ftp_issues = []
if not getattr(settings, 'ftp_host', None): if not getattr(settings, "ftp_host", None):
ftp_issues.append("FTP_HOST is not configured") ftp_issues.append("FTP_HOST is not configured")
if not getattr(settings, 'ftp_username', None): if not getattr(settings, "ftp_username", None):
ftp_issues.append("FTP_USERNAME is not configured") ftp_issues.append("FTP_USERNAME is not configured")
if not getattr(settings, 'ftp_password', None): if not getattr(settings, "ftp_password", None):
ftp_issues.append("FTP_PASSWORD is not configured") ftp_issues.append("FTP_PASSWORD is not configured")
issues['ftp'] = ftp_issues issues["ftp"] = ftp_issues
# Validate WebDAV # Validate WebDAV
webdav_issues = [] webdav_issues = []
if not getattr(settings, 'webdav_url', None): if not getattr(settings, "webdav_url", None):
webdav_issues.append("WEBDAV_URL is not configured") webdav_issues.append("WEBDAV_URL is not configured")
if not getattr(settings, 'webdav_username', None): if not getattr(settings, "webdav_username", None):
webdav_issues.append("WEBDAV_USERNAME is not configured") webdav_issues.append("WEBDAV_USERNAME is not configured")
if not getattr(settings, 'webdav_password', None): if not getattr(settings, "webdav_password", None):
webdav_issues.append("WEBDAV_PASSWORD is not configured") webdav_issues.append("WEBDAV_PASSWORD is not configured")
issues['webdav'] = webdav_issues issues["webdav"] = webdav_issues
# Validate Google Drive # Validate Google Drive
gdrive_issues = [] gdrive_issues = []
if not getattr(settings, 'google_drive_credentials_json', None): if not getattr(settings, "google_drive_credentials_json", None):
gdrive_issues.append("GOOGLE_DRIVE_CREDENTIALS_JSON is not configured") gdrive_issues.append("GOOGLE_DRIVE_CREDENTIALS_JSON is not configured")
if not getattr(settings, 'google_drive_folder_id', None): if not getattr(settings, "google_drive_folder_id", None):
gdrive_issues.append("GOOGLE_DRIVE_FOLDER_ID is not configured") gdrive_issues.append("GOOGLE_DRIVE_FOLDER_ID is not configured")
issues['google_drive'] = gdrive_issues issues["google_drive"] = gdrive_issues
# Validate Paperless # Validate Paperless
paperless_issues = [] paperless_issues = []
if not getattr(settings, 'paperless_host', None): if not getattr(settings, "paperless_host", None):
paperless_issues.append("PAPERLESS_HOST is not configured") paperless_issues.append("PAPERLESS_HOST is not configured")
if not getattr(settings, 'paperless_ngx_api_token', None): if not getattr(settings, "paperless_ngx_api_token", None):
paperless_issues.append("PAPERLESS_NGX_API_TOKEN is not configured") paperless_issues.append("PAPERLESS_NGX_API_TOKEN is not configured")
issues['paperless'] = paperless_issues issues["paperless"] = paperless_issues
# Validate OneDrive # Validate OneDrive
onedrive_issues = [] onedrive_issues = []
if not (getattr(settings, 'onedrive_client_id', None) and if not (
getattr(settings, 'onedrive_client_secret', None) and getattr(settings, "onedrive_client_id", None)
getattr(settings, 'onedrive_refresh_token', None)): and getattr(settings, "onedrive_client_secret", None)
and getattr(settings, "onedrive_refresh_token", None)
):
onedrive_issues.append("OneDrive credentials are not fully configured") onedrive_issues.append("OneDrive credentials are not fully configured")
issues['onedrive'] = onedrive_issues issues["onedrive"] = onedrive_issues
# Validate Uptime Kuma # Validate Uptime Kuma
uptime_kuma_issues = [] uptime_kuma_issues = []
if not getattr(settings, 'uptime_kuma_url', None): if not getattr(settings, "uptime_kuma_url", None):
uptime_kuma_issues.append("UPTIME_KUMA_URL is not configured") uptime_kuma_issues.append("UPTIME_KUMA_URL is not configured")
issues['uptime_kuma'] = uptime_kuma_issues issues["uptime_kuma"] = uptime_kuma_issues
return issues return issues
def validate_notification_config(): def validate_notification_config():
"""Check notification configuration""" """Check notification configuration"""
issues = [] issues = []
# Check if any notification URLs are configured # Check if any notification URLs are configured
if not getattr(settings, 'notification_urls', None): if not getattr(settings, "notification_urls", None):
issues.append("No notification URLs configured") issues.append("No notification URLs configured")
else: else:
try: try:
# Try initializing Apprise to validate URLs # Try initializing Apprise to validate URLs
import apprise import apprise
a = apprise.Apprise() a = apprise.Apprise()
for url in settings.notification_urls: for url in settings.notification_urls:
@@ -195,6 +209,7 @@ def validate_notification_config():
return issues return issues
def check_all_configs(): def check_all_configs():
"""Run all configuration validations and log results""" """Run all configuration validations and log results"""
from app.utils.config_validator.settings_display import dump_all_settings from app.utils.config_validator.settings_display import dump_all_settings
@@ -202,7 +217,7 @@ def check_all_configs():
logger.info("Validating application configuration...") logger.info("Validating application configuration...")
# Check if debug is enabled and dump all settings if it is # Check if debug is enabled and dump all settings if it is
if hasattr(settings, 'debug') and settings.debug: if hasattr(settings, "debug") and settings.debug:
dump_all_settings() dump_all_settings()
# Check auth config # Check auth config
@@ -235,9 +250,4 @@ def check_all_configs():
logger.info("Notification configuration OK") logger.info("Notification configuration OK")
# Return all identified issues # Return all identified issues
return { return {"auth": auth_issues, "email": email_issues, "storage": storage_issues, "notification": notification_issues}
'auth': auth_issues,
'email': email_issues,
'storage': storage_issues,
'notification': notification_issues
}
+7 -6
View File
@@ -5,9 +5,9 @@ Uses Fernet symmetric encryption with a key derived from SESSION_SECRET.
This provides encryption at rest for sensitive configuration values. This provides encryption at rest for sensitive configuration values.
""" """
import logging
import base64 import base64
import hashlib import hashlib
import logging
from typing import Optional from typing import Optional
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -33,11 +33,12 @@ def _get_cipher_suite():
if _cipher_suite is None: if _cipher_suite is None:
try: try:
from cryptography.fernet import Fernet from cryptography.fernet import Fernet
from app.config import settings from app.config import settings
# Derive a Fernet-compatible key from SESSION_SECRET # Derive a Fernet-compatible key from SESSION_SECRET
# Fernet requires a 32-byte base64-encoded key # Fernet requires a 32-byte base64-encoded key
secret = settings.session_secret.encode('utf-8') secret = settings.session_secret.encode("utf-8")
# Use SHA256 to get exactly 32 bytes, then base64 encode # Use SHA256 to get exactly 32 bytes, then base64 encode
key_bytes = hashlib.sha256(secret).digest() key_bytes = hashlib.sha256(secret).digest()
@@ -81,9 +82,9 @@ def encrypt_value(plaintext: Optional[str]) -> Optional[str]:
return plaintext return plaintext
try: try:
encrypted_bytes = cipher.encrypt(plaintext.encode('utf-8')) encrypted_bytes = cipher.encrypt(plaintext.encode("utf-8"))
# Prefix with "enc:" to identify encrypted values # Prefix with "enc:" to identify encrypted values
return "enc:" + encrypted_bytes.decode('utf-8') return "enc:" + encrypted_bytes.decode("utf-8")
except Exception as e: except Exception as e:
logger.error(f"Encryption failed: {e}") logger.error(f"Encryption failed: {e}")
# Fall back to plaintext # Fall back to plaintext
@@ -116,9 +117,9 @@ def decrypt_value(ciphertext: Optional[str]) -> Optional[str]:
try: try:
# Remove "enc:" prefix and decrypt # Remove "enc:" prefix and decrypt
encrypted_bytes = ciphertext[4:].encode('utf-8') encrypted_bytes = ciphertext[4:].encode("utf-8")
plaintext_bytes = cipher.decrypt(encrypted_bytes) plaintext_bytes = cipher.decrypt(encrypted_bytes)
return plaintext_bytes.decode('utf-8') return plaintext_bytes.decode("utf-8")
except Exception as e: except Exception as e:
logger.error(f"Decryption failed: {e}") logger.error(f"Decryption failed: {e}")
return "[DECRYPTION FAILED]" return "[DECRYPTION FAILED]"
+1
View File
@@ -1,5 +1,6 @@
import hashlib import hashlib
def hash_file(filepath, chunk_size=65536): def hash_file(filepath, chunk_size=65536):
""" """
Returns the SHA-256 hash of the file at 'filepath'. Returns the SHA-256 hash of the file at 'filepath'.
+14 -18
View File
@@ -1,8 +1,11 @@
""" """
Utility functions for file processing status determination. Utility functions for file processing status determination.
""" """
from typing import Dict, List from typing import Dict, List
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
from app.models import ProcessingLog from app.models import ProcessingLog
@@ -18,9 +21,9 @@ def get_file_processing_status(db: Session, file_id: int) -> Dict:
dict with status, last_step, and has_errors dict with status, last_step, and has_errors
""" """
# Get all logs for this file # Get all logs for this file
logs = db.query(ProcessingLog).filter( logs = (
ProcessingLog.file_id == file_id db.query(ProcessingLog).filter(ProcessingLog.file_id == file_id).order_by(ProcessingLog.timestamp.desc()).all()
).order_by(ProcessingLog.timestamp.desc()).all() )
return _compute_status_from_logs(logs) return _compute_status_from_logs(logs)
@@ -37,9 +40,12 @@ def get_files_processing_status(db: Session, file_ids: List[int]) -> Dict[int, D
dict mapping file_id to status dict dict mapping file_id to status dict
""" """
# Get all logs for these files in one query # Get all logs for these files in one query
logs = db.query(ProcessingLog).filter( logs = (
ProcessingLog.file_id.in_(file_ids) db.query(ProcessingLog)
).order_by(ProcessingLog.file_id, ProcessingLog.timestamp.desc()).all() .filter(ProcessingLog.file_id.in_(file_ids))
.order_by(ProcessingLog.file_id, ProcessingLog.timestamp.desc())
.all()
)
# Group logs by file_id # Group logs by file_id
logs_by_file = {} logs_by_file = {}
@@ -68,12 +74,7 @@ def _compute_status_from_logs(logs: List[ProcessingLog]) -> Dict:
dict with status, last_step, has_errors, and total_steps dict with status, last_step, has_errors, and total_steps
""" """
if not logs: if not logs:
return { return {"status": "pending", "last_step": None, "has_errors": False, "total_steps": 0}
"status": "pending",
"last_step": None,
"has_errors": False,
"total_steps": 0
}
# Check for failures # Check for failures
has_errors = any(log.status == "failure" for log in logs) has_errors = any(log.status == "failure" for log in logs)
@@ -94,9 +95,4 @@ def _compute_status_from_logs(logs: List[ProcessingLog]) -> Dict:
else: else:
status = "pending" status = "pending"
return { return {"status": status, "last_step": latest_log.step_name, "has_errors": has_errors, "total_steps": len(logs)}
"status": status,
"last_step": latest_log.step_name,
"has_errors": has_errors,
"total_steps": len(logs)
}
+13 -10
View File
@@ -1,12 +1,13 @@
import logging
import os import os
import re import re
import uuid import uuid
import logging
from datetime import datetime from datetime import datetime
from pathlib import Path from pathlib import Path
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
def get_unique_filename(original_path, check_exists_func=None): def get_unique_filename(original_path, check_exists_func=None):
""" """
Generates a unique filename by appending a timestamp or counter when a collision occurs. Generates a unique filename by appending a timestamp or counter when a collision occurs.
@@ -67,6 +68,7 @@ def get_unique_filename(original_path, check_exists_func=None):
return new_path return new_path
def sanitize_filename(filename): def sanitize_filename(filename):
""" """
Sanitize a filename to ensure it's valid across different file systems. Sanitize a filename to ensure it's valid across different file systems.
@@ -79,21 +81,22 @@ def sanitize_filename(filename):
""" """
# Replace characters that are problematic in various filesystems # Replace characters that are problematic in various filesystems
# Keep only alphanumeric, dash, underscore, period, and space # Keep only alphanumeric, dash, underscore, period, and space
sanitized = re.sub(r'[^\w\-\. ]', '_', filename) sanitized = re.sub(r"[^\w\-\. ]", "_", filename)
# Replace multiple spaces/underscores with single ones # Replace multiple spaces/underscores with single ones
sanitized = re.sub(r'__+', '_', sanitized) sanitized = re.sub(r"__+", "_", sanitized)
sanitized = re.sub(r' +', ' ', sanitized) sanitized = re.sub(r" +", " ", sanitized)
# Trim leading/trailing spaces and periods which cause issues in Windows # Trim leading/trailing spaces and periods which cause issues in Windows
sanitized = sanitized.strip('. ') sanitized = sanitized.strip(". ")
# Ensure the filename isn't empty after sanitization # Ensure the filename isn't empty after sanitization
if not sanitized or sanitized == '.': if not sanitized or sanitized == ".":
sanitized = f"document_{datetime.now().strftime('%Y%m%d_%H%M%S')}" sanitized = f"document_{datetime.now().strftime('%Y%m%d_%H%M%S')}"
return sanitized return sanitized
def extract_remote_path(file_path, base_dir, remote_base=""): def extract_remote_path(file_path, base_dir, remote_base=""):
""" """
Extract a remote path for a file by preserving its directory structure Extract a remote path for a file by preserving its directory structure
@@ -114,14 +117,14 @@ def extract_remote_path(file_path, base_dir, remote_base=""):
# Skip 'processed' directory if it's in the path # Skip 'processed' directory if it's in the path
path_parts = rel_path.split(os.sep) path_parts = rel_path.split(os.sep)
if 'processed' in path_parts: if "processed" in path_parts:
# Remove 'processed' from the path # Remove 'processed' from the path
path_parts.remove('processed') path_parts.remove("processed")
rel_path = os.path.join(*path_parts) rel_path = os.path.join(*path_parts)
# Combine with remote base path # Combine with remote base path
if remote_base: if remote_base:
if remote_base.startswith('/'): if remote_base.startswith("/"):
# Handle absolute path for services like Dropbox # Handle absolute path for services like Dropbox
remote_path = os.path.join(remote_base[1:], rel_path) remote_path = os.path.join(remote_base[1:], rel_path)
else: else:
@@ -130,6 +133,6 @@ def extract_remote_path(file_path, base_dir, remote_base=""):
remote_path = rel_path remote_path = rel_path
# Convert to forward slashes for compatibility with most cloud services # Convert to forward slashes for compatibility with most cloud services
remote_path = remote_path.replace(os.sep, '/') remote_path = remote_path.replace(os.sep, "/")
return remote_path return remote_path
+1
View File
@@ -1,6 +1,7 @@
from app.database import SessionLocal from app.database import SessionLocal
from app.models import ProcessingLog from app.models import ProcessingLog
def log_task_progress(task_id, step_name, status, message=None, file_id=None): def log_task_progress(task_id, step_name, status, message=None, file_id=None):
""" """
Logs the progress of a Celery task to the database. Logs the progress of a Celery task to the database.
+29 -43
View File
@@ -1,6 +1,7 @@
import apprise
import logging import logging
from typing import List, Optional, Dict, Any, Union from typing import Any, Dict, List, Optional
import apprise
from app.config import settings from app.config import settings
@@ -9,6 +10,7 @@ logger = logging.getLogger(__name__)
# Global Apprise instance # Global Apprise instance
_apprise = None _apprise = None
def init_apprise() -> apprise.Apprise: def init_apprise() -> apprise.Apprise:
"""Initialize the Apprise instance with configured notification services""" """Initialize the Apprise instance with configured notification services"""
global _apprise global _apprise
@@ -29,24 +31,27 @@ def init_apprise() -> apprise.Apprise:
return _apprise return _apprise
def _mask_sensitive_url(url: str) -> str: def _mask_sensitive_url(url: str) -> str:
"""Mask sensitive parts of notification URLs for logging""" """Mask sensitive parts of notification URLs for logging"""
# Simple masking for common URL formats with credentials # Simple masking for common URL formats with credentials
import re import re
# Match patterns like user:pass@host or token in URL parameters # Match patterns like user:pass@host or token in URL parameters
masked = re.sub(r'://([^:]+):([^@]+)@', r'://\1:****@', url) masked = re.sub(r"://([^:]+):([^@]+)@", r"://\1:****@", url)
masked = re.sub(r'(discord://)[^/]+/[^/]+', r'\1webhook_id/****', masked) masked = re.sub(r"(discord://)[^/]+/[^/]+", r"\1webhook_id/****", masked)
masked = re.sub(r'(tgram://)[^/]+/[^/]+', r'\1bot_token/****', masked) masked = re.sub(r"(tgram://)[^/]+/[^/]+", r"\1bot_token/****", masked)
masked = re.sub(r'([?&](token|key|api_key|password|secret)=)([^&]+)', r'\1****', masked) masked = re.sub(r"([?&](token|key|api_key|password|secret)=)([^&]+)", r"\1****", masked)
return masked return masked
def send_notification( def send_notification(
title: str, title: str,
message: str, message: str,
notification_type: str = "info", notification_type: str = "info",
tags: Optional[List[str]] = None, tags: Optional[List[str]] = None,
attachments: Optional[List[str]] = None, attachments: Optional[List[str]] = None,
data: Optional[Dict[str, Any]] = None data: Optional[Dict[str, Any]] = None,
) -> bool: ) -> bool:
""" """
Send a notification through all configured channels Send a notification through all configured channels
@@ -89,12 +94,7 @@ def send_notification(
for server in apprise_obj.servers: # Iterate through the list directly for server in apprise_obj.servers: # Iterate through the list directly
try: try:
service_name = str(server).split("://")[0] if "://" in str(server) else str(server) service_name = str(server).split("://")[0] if "://" in str(server) else str(server)
service_result = server.notify( service_result = server.notify(title=title, body=message, notify_type=notify_type, attach=attachments)
title=title,
body=message,
notify_type=notify_type,
attach=attachments
)
if service_result: if service_result:
successful_services += 1 successful_services += 1
@@ -117,6 +117,7 @@ def send_notification(
logger.exception(f"Error sending notification: {e}") logger.exception(f"Error sending notification: {e}")
return False return False
def notify_celery_failure(task_name: str, task_id: str, exc: Exception, args: list, kwargs: dict) -> bool: def notify_celery_failure(task_name: str, task_id: str, exc: Exception, args: list, kwargs: dict) -> bool:
"""Send a notification about a failed Celery task""" """Send a notification about a failed Celery task"""
if not settings.notify_on_task_failure: if not settings.notify_on_task_failure:
@@ -131,12 +132,10 @@ Arguments: {args}
Keyword arguments: {kwargs} Keyword arguments: {kwargs}
""" """
return send_notification( return send_notification(
title=title, title=title, message=message, notification_type="failure", tags=["celery", "failure", task_name]
message=message,
notification_type="failure",
tags=["celery", "failure", task_name]
) )
def notify_credential_failure(service_name: str, error: str) -> bool: def notify_credential_failure(service_name: str, error: str) -> bool:
"""Send a notification about a credential failure""" """Send a notification about a credential failure"""
if not settings.notify_on_credential_failure: if not settings.notify_on_credential_failure:
@@ -150,39 +149,29 @@ The credentials for {service_name} have failed:
Please check and update the credentials in the system settings. Please check and update the credentials in the system settings.
""" """
return send_notification( return send_notification(
title=title, title=title, message=message, notification_type="warning", tags=["credentials", "warning", service_name]
message=message,
notification_type="warning",
tags=["credentials", "warning", service_name]
) )
def notify_startup() -> bool: def notify_startup() -> bool:
"""Send a notification that the application has started""" """Send a notification that the application has started"""
if not settings.notify_on_startup: if not settings.notify_on_startup:
return False return False
title = f"DocuElevate Started" title = "DocuElevate Started"
message = f"DocuElevate has been started successfully on {settings.external_hostname}" message = f"DocuElevate has been started successfully on {settings.external_hostname}"
return send_notification( return send_notification(title=title, message=message, notification_type="success", tags=["system", "startup"])
title=title,
message=message,
notification_type="success",
tags=["system", "startup"]
)
def notify_shutdown() -> bool: def notify_shutdown() -> bool:
"""Send a notification that the application is shutting down""" """Send a notification that the application is shutting down"""
if not settings.notify_on_shutdown: if not settings.notify_on_shutdown:
return False return False
title = f"DocuElevate Shutting Down" title = "DocuElevate Shutting Down"
message = f"DocuElevate on {settings.external_hostname} is shutting down" message = f"DocuElevate on {settings.external_hostname} is shutting down"
return send_notification( return send_notification(title=title, message=message, notification_type="info", tags=["system", "shutdown"])
title=title,
message=message,
notification_type="info",
tags=["system", "shutdown"]
)
def notify_file_processed(filename: str, file_size: int, metadata: dict, destinations: list) -> bool: def notify_file_processed(filename: str, file_size: int, metadata: dict, destinations: list) -> bool:
"""Send a notification that a file has been successfully processed""" """Send a notification that a file has been successfully processed"""
@@ -194,12 +183,12 @@ def notify_file_processed(filename: str, file_size: int, metadata: dict, destina
size_str = f"{size_mb:.2f} MB" if size_mb >= 1 else f"{file_size / 1024:.2f} KB" size_str = f"{size_mb:.2f} MB" if size_mb >= 1 else f"{file_size / 1024:.2f} KB"
# Extract key metadata fields # Extract key metadata fields
doc_type = metadata.get('document_type', 'Unknown') doc_type = metadata.get("document_type", "Unknown")
tags = metadata.get('tags', []) tags = metadata.get("tags", [])
tags_str = ', '.join(tags) if tags else 'None' tags_str = ", ".join(tags) if tags else "None"
# Format destinations # Format destinations
destinations_str = ', '.join(destinations) if destinations else 'None configured' destinations_str = ", ".join(destinations) if destinations else "None configured"
title = f"File Processed: {filename}" title = f"File Processed: {filename}"
message = f""" message = f"""
@@ -213,8 +202,5 @@ The file has been successfully processed and is being uploaded to all configured
""" """
return send_notification( return send_notification(
title=title, title=title, message=message.strip(), notification_type="success", tags=["document", "processed", "success"]
message=message.strip(),
notification_type="success",
tags=["document", "processed", "success"]
) )
+2 -1
View File
@@ -4,7 +4,8 @@ Shared across multiple OAuth providers to reduce code duplication.
""" """
import logging import logging
from typing import Dict, Any, Optional from typing import Any, Dict, Optional
import requests import requests
from fastapi import HTTPException, status from fastapi import HTTPException, status
+9 -21
View File
@@ -9,8 +9,9 @@ This module provides functionality to:
import logging import logging
from typing import Any, Dict, List, Optional, Tuple from typing import Any, Dict, List, Optional, Tuple
from sqlalchemy.orm import Session
from sqlalchemy.exc import SQLAlchemyError from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy.orm import Session
from app.models import ApplicationSettings from app.models import ApplicationSettings
@@ -67,7 +68,6 @@ SETTING_METADATA = {
"required": True, "required": True,
"restart_required": True, "restart_required": True,
}, },
# Authentication Settings # Authentication Settings
"auth_enabled": { "auth_enabled": {
"category": "Authentication", "category": "Authentication",
@@ -133,7 +133,6 @@ SETTING_METADATA = {
"required": False, "required": False,
"restart_required": True, "restart_required": True,
}, },
# AI Services # AI Services
"openai_api_key": { "openai_api_key": {
"category": "AI Services", "category": "AI Services",
@@ -183,7 +182,6 @@ SETTING_METADATA = {
"required": True, "required": True,
"restart_required": False, "restart_required": False,
}, },
# Storage Providers - Dropbox # Storage Providers - Dropbox
"dropbox_app_key": { "dropbox_app_key": {
"category": "Storage Providers", "category": "Storage Providers",
@@ -217,7 +215,6 @@ SETTING_METADATA = {
"required": False, "required": False,
"restart_required": False, "restart_required": False,
}, },
# Storage Providers - Nextcloud # Storage Providers - Nextcloud
"nextcloud_upload_url": { "nextcloud_upload_url": {
"category": "Storage Providers", "category": "Storage Providers",
@@ -251,7 +248,6 @@ SETTING_METADATA = {
"required": False, "required": False,
"restart_required": False, "restart_required": False,
}, },
# Storage Providers - Paperless-ngx # Storage Providers - Paperless-ngx
"paperless_ngx_api_token": { "paperless_ngx_api_token": {
"category": "Storage Providers", "category": "Storage Providers",
@@ -269,7 +265,6 @@ SETTING_METADATA = {
"required": False, "required": False,
"restart_required": False, "restart_required": False,
}, },
# Storage Providers - Google Drive # Storage Providers - Google Drive
"google_drive_credentials_json": { "google_drive_credentials_json": {
"category": "Storage Providers", "category": "Storage Providers",
@@ -327,7 +322,6 @@ SETTING_METADATA = {
"required": False, "required": False,
"restart_required": False, "restart_required": False,
}, },
# Storage Providers - OneDrive # Storage Providers - OneDrive
"onedrive_client_id": { "onedrive_client_id": {
"category": "Storage Providers", "category": "Storage Providers",
@@ -369,7 +363,6 @@ SETTING_METADATA = {
"required": False, "required": False,
"restart_required": False, "restart_required": False,
}, },
# Storage Providers - WebDAV # Storage Providers - WebDAV
"webdav_url": { "webdav_url": {
"category": "Storage Providers", "category": "Storage Providers",
@@ -411,7 +404,6 @@ SETTING_METADATA = {
"required": False, "required": False,
"restart_required": False, "restart_required": False,
}, },
# Storage Providers - FTP # Storage Providers - FTP
"ftp_host": { "ftp_host": {
"category": "Storage Providers", "category": "Storage Providers",
@@ -469,7 +461,6 @@ SETTING_METADATA = {
"required": False, "required": False,
"restart_required": False, "restart_required": False,
}, },
# Storage Providers - SFTP # Storage Providers - SFTP
"sftp_host": { "sftp_host": {
"category": "Storage Providers", "category": "Storage Providers",
@@ -535,7 +526,6 @@ SETTING_METADATA = {
"required": False, "required": False,
"restart_required": False, "restart_required": False,
}, },
# Storage Providers - AWS S3 # Storage Providers - AWS S3
"aws_access_key_id": { "aws_access_key_id": {
"category": "Storage Providers", "category": "Storage Providers",
@@ -593,7 +583,6 @@ SETTING_METADATA = {
"required": False, "required": False,
"restart_required": False, "restart_required": False,
}, },
# Email Settings # Email Settings
"email_host": { "email_host": {
"category": "Email", "category": "Email",
@@ -651,7 +640,6 @@ SETTING_METADATA = {
"required": False, "required": False,
"restart_required": False, "restart_required": False,
}, },
# IMAP Settings - Account 1 # IMAP Settings - Account 1
"imap1_host": { "imap1_host": {
"category": "IMAP", "category": "IMAP",
@@ -709,7 +697,6 @@ SETTING_METADATA = {
"required": False, "required": False,
"restart_required": False, "restart_required": False,
}, },
# IMAP Settings - Account 2 # IMAP Settings - Account 2
"imap2_host": { "imap2_host": {
"category": "IMAP", "category": "IMAP",
@@ -767,7 +754,6 @@ SETTING_METADATA = {
"required": False, "required": False,
"restart_required": False, "restart_required": False,
}, },
# Monitoring - Uptime Kuma # Monitoring - Uptime Kuma
"uptime_kuma_url": { "uptime_kuma_url": {
"category": "Monitoring", "category": "Monitoring",
@@ -785,7 +771,6 @@ SETTING_METADATA = {
"required": False, "required": False,
"restart_required": False, "restart_required": False,
}, },
# Processing Settings # Processing Settings
"http_request_timeout": { "http_request_timeout": {
"category": "Processing", "category": "Processing",
@@ -811,7 +796,6 @@ SETTING_METADATA = {
"required": False, "required": False,
"restart_required": False, "restart_required": False,
}, },
# Notifications Settings # Notifications Settings
"notification_urls": { "notification_urls": {
"category": "Notifications", "category": "Notifications",
@@ -861,7 +845,6 @@ SETTING_METADATA = {
"required": False, "required": False,
"restart_required": False, "restart_required": False,
}, },
# Feature Flags # Feature Flags
"allow_file_delete": { "allow_file_delete": {
"category": "Feature Flags", "category": "Feature Flags",
@@ -896,6 +879,7 @@ def get_setting_from_db(db: Session, key: str) -> Optional[str]:
metadata = get_setting_metadata(key) metadata = get_setting_metadata(key)
if metadata.get("sensitive", False): if metadata.get("sensitive", False):
from app.utils.encryption import decrypt_value from app.utils.encryption import decrypt_value
return decrypt_value(setting.value) return decrypt_value(setting.value)
return setting.value return setting.value
@@ -968,6 +952,7 @@ def get_all_settings_from_db(db: Session) -> Dict[str, str]:
metadata = get_setting_metadata(setting.key) metadata = get_setting_metadata(setting.key)
if metadata.get("sensitive", False): if metadata.get("sensitive", False):
from app.utils.encryption import decrypt_value from app.utils.encryption import decrypt_value
result[setting.key] = decrypt_value(setting.value) result[setting.key] = decrypt_value(setting.value)
else: else:
result[setting.key] = setting.value result[setting.key] = setting.value
@@ -1013,14 +998,17 @@ def get_setting_metadata(key: str) -> Dict[str, Any]:
Returns: Returns:
Dictionary containing setting metadata Dictionary containing setting metadata
""" """
return SETTING_METADATA.get(key, { return SETTING_METADATA.get(
key,
{
"category": "Other", "category": "Other",
"description": f"Setting: {key}", "description": f"Setting: {key}",
"type": "string", "type": "string",
"sensitive": False, "sensitive": False,
"required": False, "required": False,
"restart_required": False, "restart_required": False,
}) },
)
def get_settings_by_category() -> Dict[str, List[str]]: def get_settings_by_category() -> Dict[str, List[str]]:
+16 -14
View File
@@ -5,7 +5,8 @@ Detects if the system needs initial setup and provides required settings list.
""" """
import logging import logging
from typing import List, Dict, Any from typing import Any, Dict, List
from app.config import settings from app.config import settings
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -27,7 +28,7 @@ def get_required_settings() -> List[Dict[str, Any]]:
"sensitive": False, "sensitive": False,
"default": "sqlite:///./app/database.db", "default": "sqlite:///./app/database.db",
"wizard_step": 1, "wizard_step": 1,
"wizard_category": "Core Infrastructure" "wizard_category": "Core Infrastructure",
}, },
{ {
"key": "redis_url", "key": "redis_url",
@@ -37,7 +38,7 @@ def get_required_settings() -> List[Dict[str, Any]]:
"sensitive": False, "sensitive": False,
"default": "redis://localhost:6379/0", "default": "redis://localhost:6379/0",
"wizard_step": 1, "wizard_step": 1,
"wizard_category": "Core Infrastructure" "wizard_category": "Core Infrastructure",
}, },
{ {
"key": "workdir", "key": "workdir",
@@ -47,7 +48,7 @@ def get_required_settings() -> List[Dict[str, Any]]:
"sensitive": False, "sensitive": False,
"default": "/workdir", "default": "/workdir",
"wizard_step": 1, "wizard_step": 1,
"wizard_category": "Core Infrastructure" "wizard_category": "Core Infrastructure",
}, },
{ {
"key": "gotenberg_url", "key": "gotenberg_url",
@@ -57,7 +58,7 @@ def get_required_settings() -> List[Dict[str, Any]]:
"sensitive": False, "sensitive": False,
"default": "http://gotenberg:3000", "default": "http://gotenberg:3000",
"wizard_step": 1, "wizard_step": 1,
"wizard_category": "Core Infrastructure" "wizard_category": "Core Infrastructure",
}, },
{ {
"key": "session_secret", "key": "session_secret",
@@ -67,7 +68,7 @@ def get_required_settings() -> List[Dict[str, Any]]:
"sensitive": True, "sensitive": True,
"default": None, # Should be generated "default": None, # Should be generated
"wizard_step": 2, "wizard_step": 2,
"wizard_category": "Security" "wizard_category": "Security",
}, },
{ {
"key": "admin_username", "key": "admin_username",
@@ -77,7 +78,7 @@ def get_required_settings() -> List[Dict[str, Any]]:
"sensitive": False, "sensitive": False,
"default": "admin", "default": "admin",
"wizard_step": 2, "wizard_step": 2,
"wizard_category": "Security" "wizard_category": "Security",
}, },
{ {
"key": "admin_password", "key": "admin_password",
@@ -87,7 +88,7 @@ def get_required_settings() -> List[Dict[str, Any]]:
"sensitive": True, "sensitive": True,
"default": None, # Must be set "default": None, # Must be set
"wizard_step": 2, "wizard_step": 2,
"wizard_category": "Security" "wizard_category": "Security",
}, },
{ {
"key": "openai_api_key", "key": "openai_api_key",
@@ -97,7 +98,7 @@ def get_required_settings() -> List[Dict[str, Any]]:
"sensitive": True, "sensitive": True,
"default": None, "default": None,
"wizard_step": 3, "wizard_step": 3,
"wizard_category": "AI Services" "wizard_category": "AI Services",
}, },
{ {
"key": "azure_ai_key", "key": "azure_ai_key",
@@ -107,7 +108,7 @@ def get_required_settings() -> List[Dict[str, Any]]:
"sensitive": True, "sensitive": True,
"default": None, "default": None,
"wizard_step": 3, "wizard_step": 3,
"wizard_category": "AI Services" "wizard_category": "AI Services",
}, },
{ {
"key": "azure_region", "key": "azure_region",
@@ -117,7 +118,7 @@ def get_required_settings() -> List[Dict[str, Any]]:
"sensitive": False, "sensitive": False,
"default": "eastus", "default": "eastus",
"wizard_step": 3, "wizard_step": 3,
"wizard_category": "AI Services" "wizard_category": "AI Services",
}, },
{ {
"key": "azure_endpoint", "key": "azure_endpoint",
@@ -127,7 +128,7 @@ def get_required_settings() -> List[Dict[str, Any]]:
"sensitive": False, "sensitive": False,
"default": None, "default": None,
"wizard_step": 3, "wizard_step": 3,
"wizard_category": "AI Services" "wizard_category": "AI Services",
}, },
] ]
@@ -180,12 +181,13 @@ def get_missing_required_settings() -> List[str]:
# Check if value is missing or is a placeholder # Check if value is missing or is a placeholder
placeholder_values = [ placeholder_values = [
None, "", None,
"",
f"<{key.upper()}>", f"<{key.upper()}>",
"test-key", "test-key",
"your_secure_password", "your_secure_password",
"changeme", "changeme",
"INSECURE_DEFAULT_FOR_DEVELOPMENT_ONLY_DO_NOT_USE_IN_PRODUCTION_MINIMUM_32_CHARS" "INSECURE_DEFAULT_FOR_DEVELOPMENT_ONLY_DO_NOT_USE_IN_PRODUCTION_MINIMUM_32_CHARS",
] ]
if value in placeholder_values: if value in placeholder_values:
+5 -3
View File
@@ -1,16 +1,18 @@
""" """
Aggregated view routers for the application. Aggregated view routers for the application.
""" """
from fastapi import APIRouter from fastapi import APIRouter
from app.views.dropbox import router as dropbox_router
# Import all the view routers # Import all the view routers
from app.views.general import router as general_router from app.views.general import router as general_router
from app.views.status import router as status_router
from app.views.onedrive import router as onedrive_router
from app.views.dropbox import router as dropbox_router
from app.views.google_drive import router as google_drive_router from app.views.google_drive import router as google_drive_router
from app.views.license_routes import router as license_router # Add the license router from app.views.license_routes import router as license_router # Add the license router
from app.views.onedrive import router as onedrive_router
from app.views.settings import router as settings_router from app.views.settings import router as settings_router
from app.views.status import router as status_router
from app.views.wizard import router as wizard_router from app.views.wizard import router as wizard_router
# Create a main router that includes all the view routers # Create a main router that includes all the view routers
+12 -7
View File
@@ -1,15 +1,17 @@
""" """
Base setup for views, containing shared functionality and imports. Base setup for views, containing shared functionality and imports.
""" """
from fastapi import APIRouter, Request, Depends, HTTPException
from fastapi.templating import Jinja2Templates
from pathlib import Path
from sqlalchemy.orm import Session
import logging
from app.auth import require_login import logging
from app.database import SessionLocal from pathlib import Path
from fastapi import APIRouter, Depends, HTTPException, Request # noqa: F401
from fastapi.templating import Jinja2Templates
from sqlalchemy.orm import Session # noqa: F401
from app.auth import require_login # noqa: F401
from app.config import settings from app.config import settings
from app.database import SessionLocal
# Set up Jinja2 templates # Set up Jinja2 templates
templates_dir = Path(__file__).parent.parent.parent / "frontend" / "templates" templates_dir = Path(__file__).parent.parent.parent / "frontend" / "templates"
@@ -22,6 +24,7 @@ templates.env.globals["max"] = max
# Customize Jinja2Templates to include app_version in all templates # Customize Jinja2Templates to include app_version in all templates
original_template_response = templates.TemplateResponse original_template_response = templates.TemplateResponse
def template_response_with_version(*args, **kwargs): def template_response_with_version(*args, **kwargs):
"""Wrapper for TemplateResponse to include version in all templates""" """Wrapper for TemplateResponse to include version in all templates"""
# If context dict is provided, add version to it # If context dict is provided, add version to it
@@ -31,11 +34,13 @@ def template_response_with_version(*args, **kwargs):
kwargs["context"].setdefault("version", settings.version) kwargs["context"].setdefault("version", settings.version)
return original_template_response(*args, **kwargs) return original_template_response(*args, **kwargs)
templates.TemplateResponse = template_response_with_version templates.TemplateResponse = template_response_with_version
# Set up logging # Set up logging
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
def get_db(): def get_db():
""" """
Dependency to get a database session. Dependency to get a database session.
+11 -14
View File
@@ -1,12 +1,14 @@
""" """
Dropbox integration views for setup and OAuth callback. Dropbox integration views for setup and OAuth callback.
""" """
from fastapi import Request from fastapi import Request
from app.views.base import APIRouter, templates, require_login, settings from app.views.base import APIRouter, require_login, settings, templates
router = APIRouter() router = APIRouter()
@router.get("/dropbox-setup") @router.get("/dropbox-setup")
@require_login @require_login
async def dropbox_setup_page(request: Request): async def dropbox_setup_page(request: Request):
@@ -15,9 +17,7 @@ async def dropbox_setup_page(request: Request):
Shows configuration status and setup instructions. Shows configuration status and setup instructions.
""" """
# Check Dropbox configuration # Check Dropbox configuration
is_configured = bool(settings.dropbox_app_key and is_configured = bool(settings.dropbox_app_key and settings.dropbox_app_secret and settings.dropbox_refresh_token)
settings.dropbox_app_secret and
settings.dropbox_refresh_token)
return templates.TemplateResponse( return templates.TemplateResponse(
"dropbox.html", "dropbox.html",
@@ -27,10 +27,11 @@ async def dropbox_setup_page(request: Request):
"app_key_value": settings.dropbox_app_key or "", "app_key_value": settings.dropbox_app_key or "",
"app_secret_value": settings.dropbox_app_secret if settings.dropbox_app_secret else "", "app_secret_value": settings.dropbox_app_secret if settings.dropbox_app_secret else "",
"refresh_token_value": settings.dropbox_refresh_token if settings.dropbox_refresh_token else "", "refresh_token_value": settings.dropbox_refresh_token if settings.dropbox_refresh_token else "",
"folder_path": settings.dropbox_folder or "/Documents/Uploads" # Default folder path "folder_path": settings.dropbox_folder or "/Documents/Uploads", # Default folder path
} },
) )
@router.get("/dropbox-callback") @router.get("/dropbox-callback")
@require_login @require_login
async def dropbox_callback(request: Request, code: str = None, error: str = None): async def dropbox_callback(request: Request, code: str = None, error: str = None):
@@ -39,15 +40,11 @@ async def dropbox_callback(request: Request, code: str = None, error: str = None
Automatically exchanges the code for a token and saves it to the configuration. Automatically exchanges the code for a token and saves it to the configuration.
""" """
if error: if error:
return templates.TemplateResponse( return templates.TemplateResponse("dropbox_callback_error.html", {"request": request, "error": error})
"dropbox_callback_error.html",
{"request": request, "error": error}
)
if not code: if not code:
return templates.TemplateResponse( return templates.TemplateResponse(
"dropbox_callback_error.html", "dropbox_callback_error.html", {"request": request, "error": "No authorization code received from Dropbox"}
{"request": request, "error": "No authorization code received from Dropbox"}
) )
# Display the processing page with automatic token exchange # Display the processing page with automatic token exchange
@@ -60,6 +57,6 @@ async def dropbox_callback(request: Request, code: str = None, error: str = None
"code": code, "code": code,
"app_key_value": "", # The callback will prioritize sessionStorage values "app_key_value": "", # The callback will prioritize sessionStorage values
"app_secret_value": "", # The callback will prioritize sessionStorage values "app_secret_value": "", # The callback will prioritize sessionStorage values
"folder_path": "" # The callback will prioritize sessionStorage values "folder_path": "", # The callback will prioritize sessionStorage values
} },
) )
+9 -6
View File
@@ -2,13 +2,14 @@
File management views for displaying and managing files. File management views for displaying and managing files.
""" """
from fastapi import Request, Depends, Query
from sqlalchemy.orm import Session
from typing import Optional from typing import Optional
from app.views.base import APIRouter, templates, require_login, get_db, logger from fastapi import Depends, Query, Request
from app.utils.file_status import get_files_processing_status from sqlalchemy.orm import Session
from app.config import settings from app.config import settings
from app.utils.file_status import get_files_processing_status
from app.views.base import APIRouter, get_db, logger, require_login, templates
router = APIRouter() router = APIRouter()
@@ -31,8 +32,9 @@ def files_page(
""" """
try: try:
# Import the model here to avoid circular imports # Import the model here to avoid circular imports
from sqlalchemy import asc, desc, or_
from app.models import FileRecord, ProcessingLog from app.models import FileRecord, ProcessingLog
from sqlalchemy import desc, asc, or_
# Start with base query # Start with base query
query = db.query(FileRecord) query = db.query(FileRecord)
@@ -159,9 +161,10 @@ def file_detail_page(request: Request, file_id: int, db: Session = Depends(get_d
Return the file detail page showing processing history and file information Return the file detail page showing processing history and file information
""" """
try: try:
from app.models import FileRecord, ProcessingLog
import os import os
from app.models import FileRecord, ProcessingLog
# Find the file record # Find the file record
file_record = db.query(FileRecord).filter(FileRecord.id == file_id).first() file_record = db.query(FileRecord).filter(FileRecord.id == file_id).first()
+26 -18
View File
@@ -1,17 +1,20 @@
""" """
General routes for the application homepage and basic pages. General routes for the application homepage and basic pages.
""" """
from fastapi import Request, HTTPException, Depends
from fastapi.responses import FileResponse, RedirectResponse
from pathlib import Path
from datetime import date from datetime import date
from pathlib import Path
from fastapi import Depends, HTTPException, Request
from fastapi.responses import FileResponse, RedirectResponse
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
from app.views.base import APIRouter, templates, require_login, get_db, logger
from app.utils.config_validator import get_provider_status, validate_storage_configs from app.utils.config_validator import get_provider_status, validate_storage_configs
from app.views.base import APIRouter, get_db, logger, require_login, templates
router = APIRouter() router = APIRouter()
@router.get("/", include_in_schema=False) @router.get("/", include_in_schema=False)
async def serve_index(request: Request, db: Session = Depends(get_db)): async def serve_index(request: Request, db: Session = Depends(get_db)):
""" """
@@ -20,8 +23,8 @@ async def serve_index(request: Request, db: Session = Depends(get_db)):
If the system requires initial setup, redirect to the setup wizard. If the system requires initial setup, redirect to the setup wizard.
""" """
# Check if setup wizard is needed # Check if setup wizard is needed
from app.utils.setup_wizard import is_setup_required
from app.utils.settings_service import get_setting_from_db from app.utils.settings_service import get_setting_from_db
from app.utils.setup_wizard import is_setup_required
# Check if setup was explicitly skipped # Check if setup was explicitly skipped
setup_skipped = get_setting_from_db(db, "_setup_wizard_skipped") setup_skipped = get_setting_from_db(db, "_setup_wizard_skipped")
@@ -37,20 +40,23 @@ async def serve_index(request: Request, db: Session = Depends(get_db)):
providers = get_provider_status() providers = get_provider_status()
# Count configured providers # Count configured providers
configured_providers = sum(1 for provider in providers.values() if provider['configured']) configured_providers = sum(1 for provider in providers.values() if provider["configured"])
# Count different types of storage targets # Count different types of storage targets
storage_issues = validate_storage_configs() storage_issues = validate_storage_configs()
configured_storage_targets = sum(1 for provider, issues in storage_issues.items() configured_storage_targets = sum(
if not issues and provider in ['dropbox', 'nextcloud', 'sftp', 1
's3', 'ftp', 'webdav', for provider, issues in storage_issues.items()
'google_drive', 'onedrive']) if not issues
and provider in ["dropbox", "nextcloud", "sftp", "s3", "ftp", "webdav", "google_drive", "onedrive"]
)
# Query the actual file count from the database # Query the actual file count from the database
processed_files = 0 processed_files = 0
try: try:
# Import the model here to avoid circular imports # Import the model here to avoid circular imports
from app.models import FileRecord from app.models import FileRecord
processed_files = db.query(FileRecord).count() processed_files = db.query(FileRecord).count()
except Exception as e: except Exception as e:
# Log error but continue (don't break the page if DB query fails) # Log error but continue (don't break the page if DB query fails)
@@ -60,16 +66,18 @@ async def serve_index(request: Request, db: Session = Depends(get_db)):
stats = { stats = {
"processed_files": processed_files, "processed_files": processed_files,
"active_integrations": configured_providers, "active_integrations": configured_providers,
"storage_targets": configured_storage_targets "storage_targets": configured_storage_targets,
} }
return templates.TemplateResponse("index.html", {"request": request, "stats": stats}) return templates.TemplateResponse("index.html", {"request": request, "stats": stats})
@router.get("/about", include_in_schema=False) @router.get("/about", include_in_schema=False)
async def serve_about(request: Request): async def serve_about(request: Request):
"""Serve the about page.""" """Serve the about page."""
return templates.TemplateResponse("about.html", {"request": request}) return templates.TemplateResponse("about.html", {"request": request})
@router.get("/privacy", include_in_schema=False) @router.get("/privacy", include_in_schema=False)
async def serve_privacy(request: Request): async def serve_privacy(request: Request):
"""Serve the privacy policy page.""" """Serve the privacy policy page."""
@@ -77,17 +85,20 @@ async def serve_privacy(request: Request):
current_date = date.today().strftime("%B %d, %Y") current_date = date.today().strftime("%B %d, %Y")
return templates.TemplateResponse("privacy.html", {"request": request, "current_date": current_date}) return templates.TemplateResponse("privacy.html", {"request": request, "current_date": current_date})
@router.get("/imprint", include_in_schema=False) @router.get("/imprint", include_in_schema=False)
async def serve_imprint(request: Request): async def serve_imprint(request: Request):
"""Serve the imprint/impressum page.""" """Serve the imprint/impressum page."""
return templates.TemplateResponse("imprint.html", {"request": request}) return templates.TemplateResponse("imprint.html", {"request": request})
@router.get("/upload", include_in_schema=False) @router.get("/upload", include_in_schema=False)
@require_login @require_login
async def serve_upload(request: Request): async def serve_upload(request: Request):
"""Serve the upload page.""" """Serve the upload page."""
return templates.TemplateResponse("upload.html", {"request": request}) return templates.TemplateResponse("upload.html", {"request": request})
@router.get("/favicon.ico", include_in_schema=False) @router.get("/favicon.ico", include_in_schema=False)
def favicon(): def favicon():
"""Serve the favicon.""" """Serve the favicon."""
@@ -97,6 +108,7 @@ def favicon():
raise HTTPException(status_code=404, detail="Favicon not found") raise HTTPException(status_code=404, detail="Favicon not found")
return FileResponse(favicon_path) return FileResponse(favicon_path)
@router.get("/license", include_in_schema=False) @router.get("/license", include_in_schema=False)
async def serve_license(request: Request): async def serve_license(request: Request):
"""Serve the license page.""" """Serve the license page."""
@@ -130,13 +142,8 @@ The full license text could not be located on this system.
Please visit http://www.apache.org/licenses/LICENSE-2.0 for the complete license text. Please visit http://www.apache.org/licenses/LICENSE-2.0 for the complete license text.
""" """
return templates.TemplateResponse( return templates.TemplateResponse("license.html", {"request": request, "license_text": license_text})
"license.html",
{
"request": request,
"license_text": license_text
}
)
@router.get("/cookies", include_in_schema=False) @router.get("/cookies", include_in_schema=False)
async def serve_cookies(request: Request): async def serve_cookies(request: Request):
@@ -144,6 +151,7 @@ async def serve_cookies(request: Request):
current_date = date.today().strftime("%B %d, %Y") current_date = date.today().strftime("%B %d, %Y")
return templates.TemplateResponse("cookies.html", {"request": request, "current_date": current_date}) return templates.TemplateResponse("cookies.html", {"request": request, "current_date": current_date})
@router.get("/terms", include_in_schema=False) @router.get("/terms", include_in_schema=False)
async def serve_terms(request: Request): async def serve_terms(request: Request):
"""Serve the terms of service page.""" """Serve the terms of service page."""
+20 -31
View File
@@ -1,14 +1,17 @@
""" """
Google Drive integration views for setup and OAuth callback. Google Drive integration views for setup and OAuth callback.
""" """
from fastapi import Request
from fastapi.responses import RedirectResponse
import urllib.parse import urllib.parse
from app.views.base import APIRouter, templates, require_login, settings from fastapi import Request
from fastapi.responses import RedirectResponse
from app.views.base import APIRouter, require_login, settings, templates
router = APIRouter() router = APIRouter()
@router.get("/google-drive-setup") @router.get("/google-drive-setup")
@require_login @require_login
async def google_drive_setup_page(request: Request): async def google_drive_setup_page(request: Request):
@@ -17,12 +20,12 @@ async def google_drive_setup_page(request: Request):
Shows configuration status and setup instructions. Shows configuration status and setup instructions.
""" """
# Check if using OAuth # Check if using OAuth
use_oauth = getattr(settings, 'google_drive_use_oauth', False) use_oauth = getattr(settings, "google_drive_use_oauth", False)
# Check Google Drive OAuth configuration # Check Google Drive OAuth configuration
oauth_configured = bool(settings.google_drive_client_id and oauth_configured = bool(
settings.google_drive_client_secret and settings.google_drive_client_id and settings.google_drive_client_secret and settings.google_drive_refresh_token
settings.google_drive_refresh_token) )
# Check Google Drive service account configuration # Check Google Drive service account configuration
sa_configured = bool(settings.google_drive_credentials_json) sa_configured = bool(settings.google_drive_credentials_json)
@@ -51,10 +54,11 @@ async def google_drive_setup_page(request: Request):
"refresh_token": bool(settings.google_drive_refresh_token), "refresh_token": bool(settings.google_drive_refresh_token),
"refresh_token_value": settings.google_drive_refresh_token or "", "refresh_token_value": settings.google_drive_refresh_token or "",
"folder_id": settings.google_drive_folder_id or "", "folder_id": settings.google_drive_folder_id or "",
"has_credentials_json": bool(settings.google_drive_credentials_json) "has_credentials_json": bool(settings.google_drive_credentials_json),
} },
) )
@router.get("/google-drive-callback") @router.get("/google-drive-callback")
@require_login @require_login
async def google_drive_callback(request: Request, code: str = None, error: str = None, state: str = None): async def google_drive_callback(request: Request, code: str = None, error: str = None, state: str = None):
@@ -63,34 +67,21 @@ async def google_drive_callback(request: Request, code: str = None, error: str =
Now automatically exchanges the code for a token and saves it to the configuration. Now automatically exchanges the code for a token and saves it to the configuration.
""" """
if error: if error:
return templates.TemplateResponse( return templates.TemplateResponse("google_drive_callback_error.html", {"request": request, "error": error})
"google_drive_callback_error.html",
{"request": request, "error": error}
)
if not code: if not code:
return templates.TemplateResponse( return templates.TemplateResponse(
"google_drive_callback_error.html", "google_drive_callback_error.html",
{"request": request, "error": "No authorization code received from Google"} {"request": request, "error": "No authorization code received from Google"},
) )
# Display the processing page with automatic token exchange # Display the processing page with automatic token exchange
return templates.TemplateResponse( return templates.TemplateResponse("google_drive_callback.html", {"request": request, "code": code, "state": state})
"google_drive_callback.html",
{
"request": request,
"code": code,
"state": state
}
)
@router.get("/google-drive-auth-start") @router.get("/google-drive-auth-start")
@require_login @require_login
async def google_drive_auth_start( async def google_drive_auth_start(request: Request, client_id: str, redirect_uri: str = None):
request: Request,
client_id: str,
redirect_uri: str = None
):
""" """
Start the Google Drive OAuth flow by redirecting to Google's authorization page. Start the Google Drive OAuth flow by redirecting to Google's authorization page.
""" """
@@ -99,11 +90,9 @@ async def google_drive_auth_start(
# Create the authorization URL with required scopes # Create the authorization URL with required scopes
# Use only drive.file scope to minimize required permissions # Use only drive.file scope to minimize required permissions
scopes = [ scopes = ["https://www.googleapis.com/auth/drive.file"] # Access to files created or opened by the app
"https://www.googleapis.com/auth/drive.file" # Access to files created or opened by the app
]
scope_str = urllib.parse.quote(' '.join(scopes)) scope_str = urllib.parse.quote(" ".join(scopes))
auth_url = ( auth_url = (
f"https://accounts.google.com/o/oauth2/auth" f"https://accounts.google.com/o/oauth2/auth"
+6 -4
View File
@@ -1,12 +1,13 @@
from fastapi import APIRouter, HTTPException, Request
from fastapi.responses import PlainTextResponse, HTMLResponse
from pathlib import Path from pathlib import Path
import os
from app.views.base import templates, require_login from fastapi import APIRouter, HTTPException, Request
from fastapi.responses import HTMLResponse, PlainTextResponse
from app.views.base import templates
router = APIRouter() router = APIRouter()
@router.get("/licenses/lgpl.txt", response_class=PlainTextResponse) @router.get("/licenses/lgpl.txt", response_class=PlainTextResponse)
async def get_lgpl_license(): async def get_lgpl_license():
""" """
@@ -19,6 +20,7 @@ async def get_lgpl_license():
with open(license_path, "r") as f: with open(license_path, "r") as f:
return f.read() return f.read()
@router.get("/attribution", response_class=HTMLResponse, include_in_schema=False) @router.get("/attribution", response_class=HTMLResponse, include_in_schema=False)
async def serve_attribution(request: Request): async def serve_attribution(request: Request):
""" """
+13 -13
View File
@@ -1,12 +1,14 @@
""" """
OneDrive integration views for setup and OAuth callback. OneDrive integration views for setup and OAuth callback.
""" """
from fastapi import Request from fastapi import Request
from app.views.base import APIRouter, templates, require_login, settings from app.views.base import APIRouter, require_login, settings, templates
router = APIRouter() router = APIRouter()
@router.get("/onedrive-setup") @router.get("/onedrive-setup")
@require_login @require_login
async def onedrive_setup_page(request: Request): async def onedrive_setup_page(request: Request):
@@ -15,9 +17,9 @@ async def onedrive_setup_page(request: Request):
Shows configuration status and setup instructions. Shows configuration status and setup instructions.
""" """
# Check OneDrive configuration # Check OneDrive configuration
is_configured = bool(settings.onedrive_client_id and is_configured = bool(
settings.onedrive_client_secret and settings.onedrive_client_id and settings.onedrive_client_secret and settings.onedrive_refresh_token
settings.onedrive_refresh_token) )
# Get configuration values to display status (hide sensitive values) # Get configuration values to display status (hide sensitive values)
return templates.TemplateResponse( return templates.TemplateResponse(
@@ -32,10 +34,11 @@ async def onedrive_setup_page(request: Request):
"tenant_id": settings.onedrive_tenant_id, "tenant_id": settings.onedrive_tenant_id,
"refresh_token": bool(settings.onedrive_refresh_token), "refresh_token": bool(settings.onedrive_refresh_token),
"refresh_token_value": settings.onedrive_refresh_token if settings.onedrive_refresh_token else "", "refresh_token_value": settings.onedrive_refresh_token if settings.onedrive_refresh_token else "",
"folder_path": settings.onedrive_folder_path or "Documents/Uploads" # Default folder path "folder_path": settings.onedrive_folder_path or "Documents/Uploads", # Default folder path
} },
) )
@router.get("/onedrive-callback") @router.get("/onedrive-callback")
@require_login @require_login
async def onedrive_callback(request: Request, code: str = None, error: str = None): async def onedrive_callback(request: Request, code: str = None, error: str = None):
@@ -44,15 +47,12 @@ async def onedrive_callback(request: Request, code: str = None, error: str = Non
Now automatically exchanges the code for a token and saves it to the configuration. Now automatically exchanges the code for a token and saves it to the configuration.
""" """
if error: if error:
return templates.TemplateResponse( return templates.TemplateResponse("onedrive_callback_error.html", {"request": request, "error": error})
"onedrive_callback_error.html",
{"request": request, "error": error}
)
if not code: if not code:
return templates.TemplateResponse( return templates.TemplateResponse(
"onedrive_callback_error.html", "onedrive_callback_error.html",
{"request": request, "error": "No authorization code received from Microsoft"} {"request": request, "error": "No authorization code received from Microsoft"},
) )
# Display the processing page with automatic token exchange # Display the processing page with automatic token exchange
@@ -63,6 +63,6 @@ async def onedrive_callback(request: Request, code: str = None, error: str = Non
"code": code, "code": code,
"client_id_value": settings.onedrive_client_id or "", "client_id_value": settings.onedrive_client_id or "",
"client_secret_value": settings.onedrive_client_secret or "", "client_secret_value": settings.onedrive_client_secret or "",
"tenant_id": settings.onedrive_tenant_id or "common" "tenant_id": settings.onedrive_tenant_id or "common",
} },
) )
+17 -19
View File
@@ -2,17 +2,18 @@
Settings management views for the application. Settings management views for the application.
""" """
import os
import logging
import inspect import inspect
import logging
import os
from functools import wraps from functools import wraps
from fastapi import Request, Depends, HTTPException, status
from fastapi import Depends, HTTPException, Request, status
from fastapi.responses import RedirectResponse from fastapi.responses import RedirectResponse
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
from app.views.base import APIRouter, templates, require_login, settings, get_db
from app.utils.settings_service import get_settings_by_category, get_setting_metadata, SETTING_METADATA
from app.utils.config_validator.masking import mask_sensitive_value from app.utils.config_validator.masking import mask_sensitive_value
from app.utils.settings_service import get_setting_metadata, get_settings_by_category
from app.views.base import APIRouter, get_db, require_login, settings, templates
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
router = APIRouter() router = APIRouter()
@@ -26,11 +27,12 @@ def require_admin_access(func):
If not, redirects to the home page. Works with both sync and async functions, If not, redirects to the home page. Works with both sync and async functions,
though FastAPI route handlers should always be async. though FastAPI route handlers should always be async.
""" """
@wraps(func) @wraps(func)
async def wrapper(request: Request, *args, **kwargs): async def wrapper(request: Request, *args, **kwargs):
user = request.session.get("user") user = request.session.get("user")
if not user or not user.get("is_admin"): if not user or not user.get("is_admin"):
logger.warning(f"Non-admin user attempted to access admin-only route") logger.warning("Non-admin user attempted to access admin-only route")
return RedirectResponse(url="/", status_code=status.HTTP_302_FOUND) return RedirectResponse(url="/", status_code=status.HTTP_302_FOUND)
# FastAPI route handlers are async, but we support sync for flexibility # FastAPI route handlers are async, but we support sync for flexibility
@@ -38,6 +40,7 @@ def require_admin_access(func):
return await func(request, *args, **kwargs) return await func(request, *args, **kwargs)
else: else:
return func(request, *args, **kwargs) return func(request, *args, **kwargs)
return wrapper return wrapper
@@ -55,6 +58,7 @@ async def settings_page(request: Request, db: Session = Depends(get_db)):
try: try:
# Get settings from database # Get settings from database
from app.utils.settings_service import get_all_settings_from_db from app.utils.settings_service import get_all_settings_from_db
db_settings = get_all_settings_from_db(db) db_settings = get_all_settings_from_db(db)
# Get settings organized by category # Get settings organized by category
@@ -93,26 +97,20 @@ async def settings_page(request: Request, db: Session = Depends(get_db)):
if metadata.get("sensitive") and value: if metadata.get("sensitive") and value:
display_value = mask_sensitive_value(value) display_value = mask_sensitive_value(value)
settings_data[category].append({ settings_data[category].append(
{
"key": key, "key": key,
"display_value": display_value if display_value is not None else "", "display_value": display_value if display_value is not None else "",
"metadata": metadata, "metadata": metadata,
"source": source, "source": source,
"source_label": source_label, "source_label": source_label,
"source_color": source_color "source_color": source_color,
}) }
)
return templates.TemplateResponse( return templates.TemplateResponse(
"settings.html", "settings.html", {"request": request, "settings_data": settings_data, "app_version": settings.version}
{
"request": request,
"settings_data": settings_data,
"app_version": settings.version
}
) )
except Exception as e: except Exception as e:
logger.error(f"Error loading settings page: {e}") logger.error(f"Error loading settings page: {e}")
raise HTTPException( raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Failed to load settings page")
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to load settings page"
)
+31 -29
View File
@@ -1,17 +1,19 @@
""" """
Status and configuration views for the application. Status and configuration views for the application.
""" """
from fastapi import Request
from fastapi.responses import JSONResponse
from datetime import datetime
import os
import logging
from app.views.base import APIRouter, templates, require_login, settings import logging
import os
from datetime import datetime
from fastapi import Request
from app.views.base import APIRouter, require_login, settings, templates
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
router = APIRouter() router = APIRouter()
@router.get("/status") @router.get("/status")
@require_login @require_login
async def status_dashboard(request: Request): async def status_dashboard(request: Request):
@@ -24,53 +26,53 @@ async def status_dashboard(request: Request):
providers = get_provider_status() providers = get_provider_status()
# Get build date from settings # Get build date from settings
build_date = getattr(settings, 'build_date', 'Unknown') build_date = getattr(settings, "build_date", "Unknown")
# Try to get container information # Try to get container information
container_info = {} container_info = {}
try: try:
# Check for Docker environment # Check for Docker environment
if os.path.exists('/.dockerenv'): if os.path.exists("/.dockerenv"):
# We're inside a Docker container # We're inside a Docker container
container_info['is_docker'] = True container_info["is_docker"] = True
# Try to get container ID # Try to get container ID
try: try:
with open('/proc/self/cgroup', 'r') as f: with open("/proc/self/cgroup", "r") as f:
for line in f: for line in f:
if 'docker' in line: if "docker" in line:
container_id = line.split('/')[-1].strip() container_id = line.split("/")[-1].strip()
container_info['id'] = container_id[:12] # Short ID format container_info["id"] = container_id[:12] # Short ID format
break break
except Exception: except Exception:
container_info['id'] = 'Unknown' container_info["id"] = "Unknown"
# Get Git commit SHA from settings # Get Git commit SHA from settings
try: try:
git_sha = settings.git_sha git_sha = settings.git_sha
container_info['git_sha'] = git_sha[:7] if git_sha and git_sha != 'unknown' else 'Unknown' container_info["git_sha"] = git_sha[:7] if git_sha and git_sha != "unknown" else "Unknown"
except Exception: except Exception:
container_info['git_sha'] = 'Unknown' container_info["git_sha"] = "Unknown"
# Try to get runtime information # Try to get runtime information
try: try:
container_info['runtime_info'] = settings.runtime_info container_info["runtime_info"] = settings.runtime_info
except Exception: except Exception:
pass pass
else: else:
container_info['is_docker'] = False container_info["is_docker"] = False
# If not in Docker, get Git info from settings # If not in Docker, get Git info from settings
try: try:
git_sha = settings.git_sha git_sha = settings.git_sha
container_info['git_sha'] = git_sha[:7] if git_sha and git_sha != 'unknown' else 'Unknown' container_info["git_sha"] = git_sha[:7] if git_sha and git_sha != "unknown" else "Unknown"
except Exception: except Exception:
container_info['git_sha'] = 'Unknown' container_info["git_sha"] = "Unknown"
except Exception: except Exception:
container_info = {'is_docker': False, 'id': 'Unknown', 'git_sha': 'Unknown'} container_info = {"is_docker": False, "id": "Unknown", "git_sha": "Unknown"}
# Get notification URLs for the notification box # Get notification URLs for the notification box
notification_urls = getattr(settings, 'notification_urls', []) notification_urls = getattr(settings, "notification_urls", [])
return templates.TemplateResponse( return templates.TemplateResponse(
"status_dashboard.html", "status_dashboard.html",
@@ -79,15 +81,14 @@ async def status_dashboard(request: Request):
"providers": providers, "providers": providers,
"app_version": settings.version, "app_version": settings.version,
"build_date": build_date, "build_date": build_date,
"debug_enabled": getattr(settings, 'debug', False), "debug_enabled": getattr(settings, "debug", False),
"last_check": datetime.now().strftime("%Y-%m-%d %H:%M:%S"), "last_check": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"container_info": container_info, "container_info": container_info,
"settings": { "settings": {"notification_urls": notification_urls},
"notification_urls": notification_urls },
}
}
) )
@router.get("/env") @router.get("/env")
@require_login @require_login
async def env_debug(request: Request): async def env_debug(request: Request):
@@ -100,6 +101,7 @@ async def env_debug(request: Request):
# Get settings data # Get settings data
from app.utils.config_validator import get_settings_for_display from app.utils.config_validator import get_settings_for_display
settings_data = get_settings_for_display(show_values=debug_enabled) settings_data = get_settings_for_display(show_values=debug_enabled)
return templates.TemplateResponse( return templates.TemplateResponse(
@@ -108,6 +110,6 @@ async def env_debug(request: Request):
"request": request, "request": request,
"settings": settings_data, "settings": settings_data,
"debug_enabled": debug_enabled, "debug_enabled": debug_enabled,
"app_version": settings.version "app_version": settings.version,
} },
) )
+7 -16
View File
@@ -2,21 +2,16 @@
Setup wizard views for initial system configuration. Setup wizard views for initial system configuration.
""" """
import os
import logging import logging
import secrets import secrets
from fastapi import Request, Depends, Form
from fastapi import Depends, Form, Request
from fastapi.responses import RedirectResponse from fastapi.responses import RedirectResponse
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
from app.views.base import APIRouter, templates, get_db
from app.utils.setup_wizard import (
is_setup_required,
get_required_settings,
get_wizard_steps,
get_missing_required_settings
)
from app.utils.settings_service import save_setting_to_db from app.utils.settings_service import save_setting_to_db
from app.utils.setup_wizard import get_wizard_steps
from app.views.base import APIRouter, get_db, templates
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
router = APIRouter() router = APIRouter()
@@ -54,17 +49,13 @@ async def setup_wizard(request: Request, step: int = 1):
"max_step": max_step, "max_step": max_step,
"settings": current_settings, "settings": current_settings,
"step_category": step_category, "step_category": step_category,
"progress_percent": int((step / max_step) * 100) "progress_percent": int((step / max_step) * 100),
} },
) )
@router.post("/setup") @router.post("/setup")
async def setup_wizard_save( async def setup_wizard_save(request: Request, step: int = Form(...), db: Session = Depends(get_db)):
request: Request,
step: int = Form(...),
db: Session = Depends(get_db)
):
""" """
Save settings from the current wizard step. Save settings from the current wizard step.
""" """