hagras commited on
Commit
b98029f
·
verified ·
1 Parent(s): 97c15e2

Update server.py

Browse files
Files changed (1) hide show
  1. server.py +59 -102
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
- 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"
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/images/{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" / "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
- # Log the request for debugging
866
- logger.info(f"Asset request: {asset_path}")
867
- logger.info(f"Uploaded file: {request.session.get('uploaded_file')}")
 
 
 
 
 
 
 
 
 
 
 
 
 
868
 
869
- uploaded_file = request.session["uploaded_file"]
870
- story_id = Path(uploaded_file).stem # This is our story_id
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
- # Set appropriate content type based on file extension
888
- from mimetypes import guess_type
889
- content_type, _ = guess_type(str(file_path))
890
-
891
- return FileResponse(
892
- file_path,
893
- media_type=content_type,
894
- headers={
895
- "Cache-Control": "public, max-age=3600", # Cache for 1 hour
896
- "X-Story-ID": story_id # Custom header for debugging
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 ------------------------------ #