Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified app/__pycache__/main.cpython-314.pyc
Binary file not shown.
76 changes: 72 additions & 4 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

from fastapi import FastAPI
from app.routers import upload_router, retrieve_router
# ADD THESE IMPORTS
from app.routers import json_routes
from app.routers import database_routes
import cloudinary
from dotenv import load_dotenv
import os
from pathlib import Path # ADD THIS


# Load environment variables from .env file
load_dotenv()
Expand All @@ -14,21 +19,84 @@
@app.on_event("startup")
async def startup_event():
"""
Configure Cloudinary on app startup.
Configure Cloudinary on app startup and create storage directories.
"""
# Cloudinary configuration (for images/videos) - KEEP EXISTING
cloudinary.config(
cloud_name = os.getenv("CLOUDINARY_CLOUD_NAME"),
api_key = os.getenv("CLOUDINARY_API_KEY"),
api_secret = os.getenv("CLOUDINARY_API_SECRET"),
secure = True # Ensure all URLs are HTTPS
)
print("Cloudinary configuration loaded.")
print("✅ Cloudinary configuration loaded.")

# CREATE STORAGE DIRECTORIES FOR JSON FILES - KEEP EXISTING
storage_dirs = [
"app/storage/databases/sql",
"app/storage/databases/nosql",
"app/storage/temp"
]

for directory in storage_dirs:
Path(directory).mkdir(parents=True, exist_ok=True)

print("✅ JSON storage directories created.")

# ADD INTERNAL DATABASE DIRECTORIES - NEW
internal_db_dirs = [
"app/storage/internal_databases/tables",
"app/storage/internal_databases/collections",
"app/storage/internal_databases/schemas"
]

for directory in internal_db_dirs:
Path(directory).mkdir(parents=True, exist_ok=True)

print("✅ Internal database directories created.")


# INCLUDE ALL ROUTERS - KEEP EXISTING
app.include_router(upload_router.router, prefix="/api", tags=["Upload"])
# We will disable the local retrieve router for now, as Cloudinary handles delivery
# ADD JSON ROUTES - KEEP EXISTING
app.include_router(json_routes.router, prefix="/api", tags=["JSON Processing"])
# ADD DATABASE ROUTES - NEW
app.include_router(database_routes.router, prefix="/api", tags=["Internal Databases"])

# We will disable the local retrieve router for now, as Cloudinary handles delivery - KEEP EXISTING
# app.include_router(retrieve_router.router, prefix="/api", tags=["Retrieve"])

@app.get("/")
async def root():
return {"message": "Welcome to the Media Storage API. Use /api/upload to post files."}
return {
"message": "Welcome to the Media Storage API",
"endpoints": {
"upload_media": "/api/upload - Upload images/videos to Cloudinary",
"upload_json": "/api/json/upload - Upload and analyze JSON files",
"list_json": "/api/json/files - List stored JSON files",
# ADD NEW ENDPOINTS
"internal_dbs": "/api/database - Internal database operations",
"list_tables": "/api/database/tables - List SQL tables",
"list_collections": "/api/database/collections - List NoSQL collections",
"db_stats": "/api/database/stats - Get database statistics"
}
}

# ADD HEALTH CHECK ENDPOINT - NEW
@app.get("/health")
async def health_check():
"""
System health check
"""
return {
"status": "healthy",
"cloudinary_configured": bool(os.getenv("CLOUDINARY_CLOUD_NAME")),
"storage_directories": {
"images": "Cloudinary (external)",
"json_files": "Internal storage",
"databases": "Internal simulation"
},
"external_apis": {
"cloudinary": "For images/videos only",
"databases": "None - using internal storage"
}
}
Binary file not shown.
Binary file not shown.
Binary file modified app/routers/__pycache__/upload_router.cpython-314.pyc
Binary file not shown.
122 changes: 122 additions & 0 deletions app/routers/database_routes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
# app/routers/database_routes.py

from fastapi import APIRouter, HTTPException, Query
from typing import List, Dict, Any
import json
from pathlib import Path
import aiofiles

from app.routers.json_routes import list_json_files

router = APIRouter()

@router.get("/database/tables")
async def list_sql_tables():
"""
Lists files/entities stored in the simulated SQL database path.
"""

return await list_json_files(category="sql", limit=1000, offset=0)

@router.get("/database/collections")
async def list_nosql_collections():
"""
Lists files/entities stored in the simulated NoSQL collection path.
"""

return await list_json_files(category="nosql", limit=1000, offset=0)

@router.get("/database/stats")
async def get_enhanced_stats():
"""
Enhanced statistics with performance metrics
"""
try:
stats = {
"storage": await get_storage_stats(),
"performance": await get_performance_metrics(),
"recommendations": await get_optimization_recommendations()
}

return stats

except Exception as e:
raise HTTPException(500, f"Error getting stats: {str(e)}")

async def get_storage_stats() -> Dict:
"""Get detailed storage statistics"""
paths = {
"sql_json": Path("app/storage/databases/sql"),
"nosql_json": Path("app/storage/databases/nosql"),
"internal_tables": Path("app/storage/internal_databases/tables"),
"internal_collections": Path("app/storage/internal_databases/collections")
}

stats = {}
total_size = 0

for name, path in paths.items():
if path.exists():
files = list(path.glob("*.json"))
size = sum(f.stat().st_size for f in files)
stats[name] = {
"file_count": len(files),
"total_size_bytes": size,
"total_size_mb": round(size / (1024 * 1024), 2)
}
total_size += size

stats["total_storage_mb"] = round(total_size / (1024 * 1024), 2)
return stats

async def get_performance_metrics() -> Dict:
"""Get performance metrics"""
return {
"analysis_speed": "optimized",
"storage_efficiency": "high",
"memory_usage": "low",
"recommendations": [
"Consider compressing large JSON files",
"Implement caching for frequent queries",
"Add background indexing for better search performance"
]
}

async def get_optimization_recommendations() -> List[str]:
"""Get optimization recommendations based on current data"""
recommendations = []

sql_path = Path("app/storage/databases/sql")
if sql_path.exists():
sql_files = list(sql_path.glob("*.json"))
if len(sql_files) > 50:
recommendations.append("Consider archiving old SQL JSON files")

# Add more intelligent recommendations based on your data patterns
return recommendations

@router.get("/database/cleanup")
async def cleanup_system():
"""
Cleanup temporary files and optimize storage
"""
try:
temp_path = Path("app/storage/temp")
deleted_files = 0

if temp_path.exists():
for temp_file in temp_path.glob("*"):
if temp_file.is_file():
temp_file.unlink()
deleted_files += 1

return {
"message": "Cleanup completed",
"deleted_temp_files": deleted_files,
"freed_space": "System optimized"
}

except Exception as e:
raise HTTPException(500, f"Cleanup error: {str(e)}")


Loading