Ali2206 commited on
Commit
c378250
·
1 Parent(s): 11102bd

Fix PermissionError with robust directory creation fallback

Browse files
Files changed (2) hide show
  1. api/routes/messaging.py +19 -8
  2. api/routes/patients.py +21 -9
api/routes/messaging.py CHANGED
@@ -795,14 +795,25 @@ async def upload_file(
795
  )
796
 
797
  # Create uploads directory if it doesn't exist
798
- try:
799
- upload_dir = Path("uploads")
800
- upload_dir.mkdir(exist_ok=True)
801
- except PermissionError:
802
- # In containerized environments, use temp directory
803
- import tempfile
804
- upload_dir = Path(tempfile.gettempdir()) / "uploads"
805
- upload_dir.mkdir(exist_ok=True)
 
 
 
 
 
 
 
 
 
 
 
806
 
807
  # Create category subdirectory
808
  category_dir = upload_dir / file_category
 
795
  )
796
 
797
  # Create uploads directory if it doesn't exist
798
+ def get_upload_dir():
799
+ default_dir = Path("uploads")
800
+ try:
801
+ default_dir.mkdir(exist_ok=True)
802
+ return default_dir
803
+ except (PermissionError, OSError):
804
+ # In containerized environments, use temp directory
805
+ import tempfile
806
+ temp_dir = Path(tempfile.gettempdir()) / "uploads"
807
+ try:
808
+ temp_dir.mkdir(exist_ok=True)
809
+ return temp_dir
810
+ except (PermissionError, OSError):
811
+ # Last resort: use current directory
812
+ current_dir = Path.cwd() / "uploads"
813
+ current_dir.mkdir(exist_ok=True)
814
+ return current_dir
815
+
816
+ upload_dir = get_upload_dir()
817
 
818
  # Create category subdirectory
819
  category_dir = upload_dir / file_category
api/routes/patients.py CHANGED
@@ -34,15 +34,27 @@ router = APIRouter()
34
 
35
  # Configuration
36
  BASE_DIR = Path(__file__).resolve().parent.parent.parent
37
- SYNTHEA_DATA_DIR = BASE_DIR / "output" / "fhir"
38
- try:
39
- os.makedirs(SYNTHEA_DATA_DIR, exist_ok=True)
40
- except PermissionError:
41
- # In containerized environments, we might not have write permissions
42
- # Use a temporary directory instead
43
- import tempfile
44
- SYNTHEA_DATA_DIR = Path(tempfile.gettempdir()) / "fhir"
45
- os.makedirs(SYNTHEA_DATA_DIR, exist_ok=True)
 
 
 
 
 
 
 
 
 
 
 
 
46
 
47
  # Pydantic models for update validation
48
  class ConditionUpdate(BaseModel):
 
34
 
35
  # Configuration
36
  BASE_DIR = Path(__file__).resolve().parent.parent.parent
37
+
38
+ # Try to create the default directory, fallback to temp if permission denied
39
+ def get_synthea_data_dir():
40
+ default_dir = BASE_DIR / "output" / "fhir"
41
+ try:
42
+ os.makedirs(default_dir, exist_ok=True)
43
+ return default_dir
44
+ except (PermissionError, OSError):
45
+ # In containerized environments, use temp directory
46
+ import tempfile
47
+ temp_dir = Path(tempfile.gettempdir()) / "fhir"
48
+ try:
49
+ os.makedirs(temp_dir, exist_ok=True)
50
+ return temp_dir
51
+ except (PermissionError, OSError):
52
+ # Last resort: use current directory
53
+ current_dir = Path.cwd() / "fhir"
54
+ os.makedirs(current_dir, exist_ok=True)
55
+ return current_dir
56
+
57
+ SYNTHEA_DATA_DIR = get_synthea_data_dir()
58
 
59
  # Pydantic models for update validation
60
  class ConditionUpdate(BaseModel):