Spaces:
Running
Running
Update server.py
Browse files
server.py
CHANGED
|
@@ -789,113 +789,70 @@ def refresh_session_ajax(request: Request):
|
|
| 789 |
|
| 790 |
@backend.get("/load/{filename:path}")
|
| 791 |
def load(request: Request, filename: str):
|
| 792 |
-
|
| 793 |
-
|
| 794 |
-
|
| 795 |
-
|
| 796 |
-
|
| 797 |
-
|
| 798 |
-
|
| 799 |
-
|
| 800 |
-
|
| 801 |
-
|
| 802 |
-
|
| 803 |
-
|
| 804 |
-
|
| 805 |
-
base_url = str(request.base_url).rstrip('/') + f"/load"
|
| 806 |
-
|
| 807 |
-
with open(idx_file, "r", encoding="utf-8") as f:
|
| 808 |
-
content = f.read()
|
| 809 |
|
| 810 |
-
#
|
| 811 |
-
|
| 812 |
|
| 813 |
-
logger.info(f"
|
| 814 |
-
|
| 815 |
-
|
| 816 |
-
|
| 817 |
-
|
| 818 |
-
|
| 819 |
-
|
| 820 |
-
|
| 821 |
-
|
| 822 |
-
|
| 823 |
-
# Log the request for debugging
|
| 824 |
-
logger.info(f"Asset request: {asset_path}")
|
| 825 |
-
logger.info(f"Uploaded file: {request.session.get('uploaded_file')}")
|
| 826 |
-
|
| 827 |
-
uploaded_file = request.session["uploaded_file"]
|
| 828 |
-
story_id = Path(uploaded_file).stem # This is our story_id
|
| 829 |
-
base_dir = UPLOADS_DIR / story_id / "rag_dv" / "assets" / "images"
|
| 830 |
-
|
| 831 |
-
# Construct the full file path
|
| 832 |
-
file_path = base_dir / asset_path
|
| 833 |
-
|
| 834 |
-
logger.info(f"Story ID: {story_id}")
|
| 835 |
-
logger.info(f"Looking for asset at: {file_path}")
|
| 836 |
-
logger.info(f"Base directory exists: {base_dir.exists()}")
|
| 837 |
-
|
| 838 |
-
if base_dir.exists():
|
| 839 |
-
logger.info(f"Contents of assets dir: {list(base_dir.rglob('*'))}")
|
| 840 |
-
|
| 841 |
-
if not file_path.exists() or not file_path.is_file():
|
| 842 |
-
logger.error(f"Asset not found: {file_path}")
|
| 843 |
-
raise HTTPException(status_code=404, detail=f"Asset not found: {asset_path}")
|
| 844 |
-
|
| 845 |
-
# Set appropriate content type based on file extension
|
| 846 |
-
from mimetypes import guess_type
|
| 847 |
-
content_type, _ = guess_type(str(file_path))
|
| 848 |
-
|
| 849 |
-
return FileResponse(
|
| 850 |
-
file_path,
|
| 851 |
-
media_type=content_type,
|
| 852 |
-
headers={
|
| 853 |
-
"Cache-Control": "public, max-age=3600", # Cache for 1 hour
|
| 854 |
-
"X-Story-ID": story_id # Custom header for debugging
|
| 855 |
-
}
|
| 856 |
-
)
|
| 857 |
-
|
| 858 |
-
|
| 859 |
-
@backend.get("/load/assets/{asset_path:path}")
|
| 860 |
-
def load_assets(request: Request, asset_path: str):
|
| 861 |
-
if "uploaded_file" not in request.session:
|
| 862 |
-
logger.error("No active session for asset request")
|
| 863 |
-
raise HTTPException(status_code=404, detail="No active session")
|
| 864 |
|
| 865 |
-
|
| 866 |
-
|
| 867 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 868 |
|
| 869 |
-
|
| 870 |
-
|
| 871 |
-
base_dir = UPLOADS_DIR / story_id / "rag_dv" / "assets"
|
| 872 |
-
|
| 873 |
-
# Construct the full file path
|
| 874 |
-
file_path = base_dir / asset_path
|
| 875 |
-
|
| 876 |
-
logger.info(f"Story ID: {story_id}")
|
| 877 |
-
logger.info(f"Looking for asset at: {file_path}")
|
| 878 |
-
logger.info(f"Base directory exists: {base_dir.exists()}")
|
| 879 |
-
|
| 880 |
-
if base_dir.exists():
|
| 881 |
-
logger.info(f"Contents of assets dir: {list(base_dir.rglob('*'))}")
|
| 882 |
-
|
| 883 |
-
if not file_path.exists() or not file_path.is_file():
|
| 884 |
-
logger.error(f"Asset not found: {file_path}")
|
| 885 |
-
raise HTTPException(status_code=404, detail=f"Asset not found: {asset_path}")
|
| 886 |
|
| 887 |
-
|
| 888 |
-
|
| 889 |
-
|
| 890 |
-
|
| 891 |
-
|
| 892 |
-
|
| 893 |
-
|
| 894 |
-
|
| 895 |
-
|
| 896 |
-
|
| 897 |
-
|
| 898 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 899 |
|
| 900 |
|
| 901 |
# ----------------------------- Error handling ------------------------------ #
|
|
|
|
| 789 |
|
| 790 |
@backend.get("/load/{filename:path}")
|
| 791 |
def load(request: Request, filename: str):
|
| 792 |
+
if filename.startswith("assets"):
|
| 793 |
+
asset_path = filename.replace("assets/", "")
|
| 794 |
+
if "uploaded_file" not in request.session:
|
| 795 |
+
logger.error("No active session for asset request")
|
| 796 |
+
raise HTTPException(status_code=404, detail="No active session")
|
| 797 |
+
|
| 798 |
+
# Log the request for debugging
|
| 799 |
+
logger.info(f"Asset request: {asset_path}")
|
| 800 |
+
logger.info(f"Uploaded file: {request.session.get('uploaded_file')}")
|
| 801 |
+
|
| 802 |
+
uploaded_file = request.session["uploaded_file"]
|
| 803 |
+
story_id = Path(uploaded_file).stem # This is our story_id
|
| 804 |
+
base_dir = UPLOADS_DIR / story_id / "rag_dv" / "assets"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 805 |
|
| 806 |
+
# Construct the full file path
|
| 807 |
+
file_path = base_dir / asset_path
|
| 808 |
|
| 809 |
+
logger.info(f"Story ID: {story_id}")
|
| 810 |
+
logger.info(f"Looking for asset at: {file_path}")
|
| 811 |
+
logger.info(f"Base directory exists: {base_dir.exists()}")
|
| 812 |
+
|
| 813 |
+
if base_dir.exists():
|
| 814 |
+
logger.info(f"Contents of assets dir: {list(base_dir.rglob('*'))}")
|
| 815 |
+
|
| 816 |
+
if not file_path.exists() or not file_path.is_file():
|
| 817 |
+
logger.error(f"Asset not found: {file_path}")
|
| 818 |
+
raise HTTPException(status_code=404, detail=f"Asset not found: {asset_path}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 819 |
|
| 820 |
+
# Set appropriate content type based on file extension
|
| 821 |
+
from mimetypes import guess_type
|
| 822 |
+
content_type, _ = guess_type(str(file_path))
|
| 823 |
+
|
| 824 |
+
return FileResponse(
|
| 825 |
+
file_path,
|
| 826 |
+
media_type=content_type,
|
| 827 |
+
headers={
|
| 828 |
+
"Cache-Control": "public, max-age=3600", # Cache for 1 hour
|
| 829 |
+
"X-Story-ID": story_id # Custom header for debugging
|
| 830 |
+
}
|
| 831 |
+
)
|
| 832 |
+
else:
|
| 833 |
+
filename = secure_filename(filename)
|
| 834 |
+
filepath = UPLOADS_DIR / Path(filename).stem / filename
|
| 835 |
+
idx_file = filepath.parent / "rag_dv" / "index.html"
|
| 836 |
|
| 837 |
+
if not idx_file.exists():
|
| 838 |
+
raise HTTPException(status_code=404, detail="Document index not found.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 839 |
|
| 840 |
+
logger.info(f"Loading document: {filename}")
|
| 841 |
+
|
| 842 |
+
# Get the story_id (filename without extension and UUID prefix)
|
| 843 |
+
story_id = Path(filename).stem
|
| 844 |
+
|
| 845 |
+
# Construct base URL for assets
|
| 846 |
+
base_url = str(request.base_url).rstrip('/') + f"/load"
|
| 847 |
+
|
| 848 |
+
with open(idx_file, "r", encoding="utf-8") as f:
|
| 849 |
+
content = f.read()
|
| 850 |
+
|
| 851 |
+
# Rewrite URLs to use absolute paths
|
| 852 |
+
content = _rewrite_html_urls(content, base_url, story_id)
|
| 853 |
+
|
| 854 |
+
logger.info(f"Document loaded with base URL: {base_url}")
|
| 855 |
+
return HTMLResponse(content)
|
| 856 |
|
| 857 |
|
| 858 |
# ----------------------------- Error handling ------------------------------ #
|