cryogenic22 commited on
Commit
2e29cfe
·
verified ·
1 Parent(s): 7ac74fa

Update storage/storage_manager.py

Browse files
Files changed (1) hide show
  1. storage/storage_manager.py +24 -81
storage/storage_manager.py CHANGED
@@ -10,88 +10,31 @@ import base64
10
  class UserStorageManager:
11
  def __init__(self, storage_paths):
12
  self.paths = storage_paths
 
 
 
13
  self.ensure_directories()
14
 
 
 
 
 
 
 
 
 
 
 
 
15
  def ensure_directories(self):
16
  """Ensure all required directories exist"""
17
- for path in self.paths.values():
18
- path.mkdir(parents=True, exist_ok=True)
19
-
20
- def save_chat(self, chat_data, images=None):
21
- """Save chat with associated images"""
22
- timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
23
- chat_id = f"chat_{timestamp}"
24
-
25
- # Save chat data
26
- chat_file = self.paths["chats"] / f"{chat_id}.json"
27
- with open(chat_file, "w") as f:
28
- json.dump(chat_data, f)
29
-
30
- # Save associated images
31
- if images:
32
- image_dir = self.paths["images"] / chat_id
33
- image_dir.mkdir(exist_ok=True)
34
-
35
- for idx, img_data in enumerate(images):
36
- img_file = image_dir / f"image_{idx}.png"
37
- with open(img_file, "wb") as f:
38
- f.write(img_data)
39
-
40
- # Update chat data with image references
41
- chat_data["image_paths"] = [str(p) for p in image_dir.glob("*.png")]
42
- with open(chat_file, "w") as f:
43
- json.dump(chat_data, f)
44
-
45
- return chat_id
46
-
47
- def get_all_chats(self):
48
- """Get all user's chats with metadata"""
49
- chats = []
50
- for chat_file in self.paths["chats"].glob("*.json"):
51
- with open(chat_file, "r") as f:
52
- chat_data = json.load(f)
53
- chat_data["id"] = chat_file.stem
54
- chats.append(chat_data)
55
-
56
- # Sort by timestamp
57
- return sorted(chats, key=lambda x: x.get("timestamp", ""), reverse=True)
58
-
59
- def get_chat(self, chat_id):
60
- """Get specific chat with images"""
61
- chat_file = self.paths["chats"] / f"{chat_id}.json"
62
- if not chat_file.exists():
63
- return None
64
-
65
- with open(chat_file, "r") as f:
66
- chat_data = json.load(f)
67
-
68
- # Load associated images
69
- image_dir = self.paths["images"] / chat_id
70
- if image_dir.exists():
71
- images = []
72
- for img_file in sorted(image_dir.glob("*.png")):
73
- with open(img_file, "rb") as f:
74
- images.append(f.read())
75
- chat_data["images"] = images
76
-
77
- return chat_data
78
-
79
- def save_context(self, context_data):
80
- """Save conversation context"""
81
- context_file = self.paths["context"] / "context.json"
82
- with open(context_file, "w") as f:
83
- json.dump(context_data, f)
84
-
85
- def get_context(self):
86
- """Get saved conversation context"""
87
- context_file = self.paths["context"] / "context.json"
88
- if context_file.exists():
89
- with open(context_file, "r") as f:
90
- return json.load(f)
91
- return None
92
-
93
- def clear_context(self):
94
- """Clear conversation context"""
95
- context_file = self.paths["context"] / "context.json"
96
- if context_file.exists():
97
- context_file.unlink()
 
10
  class UserStorageManager:
11
  def __init__(self, storage_paths):
12
  self.paths = storage_paths
13
+ # Validate storage access
14
+ if not self._validate_storage():
15
+ raise RuntimeError("Cannot access persistent storage. Please ensure /data directory is available and writable.")
16
  self.ensure_directories()
17
 
18
+ def _validate_storage(self):
19
+ """Validate that we can access and write to storage"""
20
+ try:
21
+ test_file = Path("/data/storage_test")
22
+ test_file.touch()
23
+ test_file.unlink()
24
+ return True
25
+ except Exception as e:
26
+ st.error(f"Storage validation failed: {str(e)}")
27
+ return False
28
+
29
  def ensure_directories(self):
30
  """Ensure all required directories exist"""
31
+ try:
32
+ for path in self.paths.values():
33
+ path.mkdir(parents=True, exist_ok=True)
34
+ # Test write access
35
+ test_file = path / ".write_test"
36
+ test_file.touch()
37
+ test_file.unlink()
38
+ except Exception as e:
39
+ st.error(f"Failed to create or access storage directories: {str(e)}")
40
+ raise RuntimeError("Storage directories are not accessible")