Spaces:
Sleeping
Sleeping
Update server.py
Browse files
server.py
CHANGED
|
@@ -789,71 +789,71 @@ 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/assets"
|
| 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 |
-
|
| 828 |
-
|
| 829 |
-
|
| 830 |
-
|
| 831 |
-
|
| 832 |
-
|
| 833 |
-
|
| 834 |
-
|
| 835 |
-
|
| 836 |
-
|
| 837 |
-
|
| 838 |
-
|
| 839 |
-
logger.info(f"Contents of assets dir: {list(base_dir.rglob('*'))}")
|
| 840 |
|
| 841 |
-
|
| 842 |
-
|
| 843 |
-
|
|
|
|
| 844 |
|
| 845 |
-
|
| 846 |
-
|
| 847 |
-
|
| 848 |
-
|
| 849 |
-
|
| 850 |
-
|
| 851 |
-
|
| 852 |
-
|
| 853 |
-
|
| 854 |
-
|
| 855 |
-
|
| 856 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 857 |
|
| 858 |
|
| 859 |
# ----------------------------- 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 |
|
| 833 |
+
else:
|
| 834 |
+
filename = secure_filename(filename)
|
| 835 |
+
filepath = UPLOADS_DIR / Path(filename).stem / filename
|
| 836 |
+
idx_file = filepath.parent / "rag_dv" / "index.html"
|
| 837 |
|
| 838 |
+
if not idx_file.exists():
|
| 839 |
+
raise HTTPException(status_code=404, detail="Document index not found.")
|
| 840 |
+
|
| 841 |
+
logger.info(f"Loading document: {filename}")
|
| 842 |
+
|
| 843 |
+
# Get the story_id (filename without extension and UUID prefix)
|
| 844 |
+
story_id = Path(filename).stem
|
| 845 |
+
|
| 846 |
+
# Construct base URL for assets
|
| 847 |
+
base_url = str(request.base_url).rstrip('/') + f"/load/assets"
|
| 848 |
+
|
| 849 |
+
with open(idx_file, "r", encoding="utf-8") as f:
|
| 850 |
+
content = f.read()
|
| 851 |
+
|
| 852 |
+
# Rewrite URLs to use absolute paths
|
| 853 |
+
content = _rewrite_html_urls(content, base_url, story_id)
|
| 854 |
+
|
| 855 |
+
logger.info(f"Document loaded with base URL: {base_url}")
|
| 856 |
+
return HTMLResponse(content)
|
| 857 |
|
| 858 |
|
| 859 |
# ----------------------------- Error handling ------------------------------ #
|