Imrao commited on
Commit
5112cdf
·
1 Parent(s): acd48d9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -21
app.py CHANGED
@@ -429,31 +429,60 @@ async def generate_i3s(request: GenerateI3SRequest):
429
  raise HTTPException(status_code=500, detail=str(e))
430
 
431
  @app.middleware("http")
432
- async def i3s_json_middleware(request: Request, call_next):
433
- # Check for ArcGIS-style query param f=pjson or f=json
434
- if request.query_params.get("f") in ["json", "pjson"]:
435
- # Allow normal processing first? No, static files don't handle query params usually
436
- # We check if the path corresponds to a directory with 3dSceneLayer.json
437
 
438
- # Construct local path from URL path
439
- # URL path: /layers/<uuid>/SceneLayer
440
- # Local path: .../static/layers/<uuid>/SceneLayer
 
 
 
441
 
442
- path = request.url.path
443
- if path.startswith("/layers"):
444
- rel_path = path[len("/layers"):] # e.g. /<uuid>/SceneLayer
445
- if rel_path.startswith("/"): rel_path = rel_path[1:]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
446
 
447
- local_path = os.path.join(LAYERS_DIR, rel_path.replace("/", os.sep))
 
 
 
 
 
 
 
 
 
 
448
 
449
- # If request is for a directory (or valid path that is a dir)
450
- if os.path.exists(local_path) and os.path.isdir(local_path):
451
- json_path = os.path.join(local_path, "3dSceneLayer.json")
452
- if os.path.exists(json_path):
453
- logger.info(f"Intercepting f=pjson for {path}, serving 3dSceneLayer.json")
454
- from fastapi.responses import FileResponse
455
- return FileResponse(json_path, media_type="application/json")
456
-
457
  response = await call_next(request)
458
  return response
459
 
 
429
  raise HTTPException(status_code=500, detail=str(e))
430
 
431
  @app.middleware("http")
432
+ async def i3s_smart_middleware(request: Request, call_next):
433
+ # Intercept requests to /layers/
434
+ if request.url.path.startswith("/layers"):
435
+ path = request.url.path
 
436
 
437
+ # Determine local path
438
+ # URL: /layers/<uuid>/...
439
+ # Local: .../static/layers/<uuid>/...
440
+ rel_path = path[len("/layers"):]
441
+ if rel_path.startswith("/"): rel_path = rel_path[1:]
442
+ local_path = os.path.join(LAYERS_DIR, rel_path.replace("/", os.sep))
443
 
444
+ # Logic to map REST-style I3S requests to File System
445
+ target_file = None
446
+
447
+ # 1. Check if exact file exists
448
+ if os.path.isfile(local_path):
449
+ # Let default static handler (or call_next) handle it?
450
+ # But call_next might be the static mount.
451
+ # We can just let it proceed.
452
+ pass
453
+ else:
454
+ # 2. Handle I3S Conventions
455
+
456
+ # Case A: Layer Root (e.g. .../layers/0r or .../layers/0r/)
457
+ # If directory exists, check for 3dSceneLayer.json
458
+ if os.path.isdir(local_path):
459
+ possible_json = os.path.join(local_path, "3dSceneLayer.json")
460
+ if os.path.isfile(possible_json):
461
+ target_file = possible_json
462
+
463
+ # Case B: Nodes (e.g. .../nodes/root or .../nodes/15)
464
+ # Expects: .../nodes/root/3dNodeIndexDocument.json
465
+ elif "/nodes/" in path:
466
+ # If local_path is a directory, look for 3dNodeIndexDocument.json
467
+ # But os.path.isdir might fail if the url doesn't have trailing slash?
468
+ # Actually, 'nodes/root' might be a folder on disk.
469
+ if os.path.isdir(local_path):
470
+ possible_doc = os.path.join(local_path, "3dNodeIndexDocument.json")
471
+ if os.path.isfile(possible_doc):
472
+ target_file = possible_doc
473
 
474
+ # Case C: NodePages (e.g. .../nodepages/0)
475
+ # Expects: .../nodepages/0.json
476
+ elif "/nodepages/" in path:
477
+ # Check if adding .json helps
478
+ possible_json_page = local_path + ".json"
479
+ if os.path.isfile(possible_json_page):
480
+ target_file = possible_json_page
481
+
482
+ if target_file:
483
+ logger.info(f"Serving I3S Resource: {path} -> {target_file}")
484
+ return FileResponse(target_file, media_type="application/json")
485
 
 
 
 
 
 
 
 
 
486
  response = await call_next(request)
487
  return response
488