hagras commited on
Commit
0bd6316
·
verified ·
1 Parent(s): fb45bed

Update server.py

Browse files
Files changed (1) hide show
  1. server.py +60 -60
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
- filename = secure_filename(filename)
793
- filepath = UPLOADS_DIR / Path(filename).stem / filename
794
- idx_file = filepath.parent / "rag_dv" / "index.html"
795
-
796
- if not idx_file.exists():
797
- raise HTTPException(status_code=404, detail="Document index not found.")
798
-
799
- logger.info(f"Loading document: {filename}")
800
-
801
- # Get the story_id (filename without extension and UUID prefix)
802
- story_id = Path(filename).stem
803
-
804
- # Construct base URL for assets
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
- # Rewrite URLs to use absolute paths
811
- content = _rewrite_html_urls(content, base_url, story_id)
812
 
813
- logger.info(f"Document loaded with base URL: {base_url}")
814
- return HTMLResponse(content)
815
-
816
-
817
- @backend.get("/load/assets/{asset_path:path}")
818
- def load_assets(request: Request, asset_path: str):
819
- if "uploaded_file" not in request.session:
820
- logger.error("No active session for asset request")
821
- raise HTTPException(status_code=404, detail="No active session")
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"
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
  # ----------------------------- 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 ------------------------------ #