cryogenic22 commited on
Commit
c559c19
·
verified ·
1 Parent(s): 4f0ef41

Update storage/storage_manager.py

Browse files
Files changed (1) hide show
  1. storage/storage_manager.py +168 -20
storage/storage_manager.py CHANGED
@@ -5,36 +5,184 @@ from pathlib import Path
5
  from datetime import datetime
6
  import shutil
7
  import streamlit as st
8
- import base64
9
 
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")
 
5
  from datetime import datetime
6
  import shutil
7
  import streamlit as st
 
8
 
9
  class UserStorageManager:
10
  def __init__(self, storage_paths):
11
  self.paths = storage_paths
 
 
 
12
  self.ensure_directories()
13
 
14
+ def ensure_directories(self):
15
+ """Ensure all required directories exist"""
16
+ for path in self.paths.values():
17
+ path.mkdir(parents=True, exist_ok=True)
18
+
19
+ def save_chat(self, chat_data, images=None, filename=None):
20
+ """Save chat with associated images"""
21
+ try:
22
+ timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
23
+ chat_id = f"chat_{timestamp}"
24
+
25
+ if filename:
26
+ chat_id = filename.replace('.json', '')
27
+
28
+ # Save chat data
29
+ chat_file = self.paths["chats"] / f"{chat_id}.json"
30
+
31
+ # Add metadata
32
+ chat_metadata = {
33
+ 'id': chat_id,
34
+ 'timestamp': timestamp,
35
+ 'data': chat_data
36
+ }
37
+
38
+ with open(chat_file, "w") as f:
39
+ json.dump(chat_metadata, f)
40
+
41
+ # Save associated images
42
+ if images:
43
+ image_dir = self.paths["images"] / chat_id
44
+ image_dir.mkdir(exist_ok=True)
45
+
46
+ if isinstance(images, list):
47
+ for idx, img_data in enumerate(images):
48
+ img_file = image_dir / f"image_{idx}.png"
49
+ with open(img_file, "wb") as f:
50
+ f.write(img_data)
51
+ else:
52
+ img_file = image_dir / "image_0.png"
53
+ with open(img_file, "wb") as f:
54
+ f.write(images)
55
+
56
+ # Update chat data with image references
57
+ chat_metadata['image_paths'] = [str(p) for p in image_dir.glob("*.png")]
58
+ with open(chat_file, "w") as f:
59
+ json.dump(chat_metadata, f)
60
+
61
+ return chat_id
62
+
63
+ except Exception as e:
64
+ st.error(f"Error saving chat: {str(e)}")
65
+ return None
66
+
67
+ def get_all_chats(self):
68
+ """Get all user's chats with metadata"""
69
+ try:
70
+ chats = []
71
+ for chat_file in sorted(self.paths["chats"].glob("*.json"), reverse=True):
72
+ with open(chat_file, "r") as f:
73
+ chat_data = json.load(f)
74
+ # Load associated images if they exist
75
+ chat_id = chat_data.get('id', chat_file.stem)
76
+ image_dir = self.paths["images"] / chat_id
77
+ if image_dir.exists():
78
+ images = []
79
+ for img_file in sorted(image_dir.glob("*.png")):
80
+ with open(img_file, "rb") as f:
81
+ images.append(f.read())
82
+ chat_data['images'] = images
83
+ chats.append(chat_data)
84
+ return chats
85
+ except Exception as e:
86
+ st.error(f"Error loading chats: {str(e)}")
87
+ return []
88
+
89
+ def get_chat(self, chat_id):
90
+ """Get specific chat with images"""
91
+ try:
92
+ chat_file = self.paths["chats"] / f"{chat_id}.json"
93
+ if not chat_file.exists():
94
+ return None
95
+
96
+ with open(chat_file, "r") as f:
97
+ chat_data = json.load(f)
98
+
99
+ # Load associated images
100
+ image_dir = self.paths["images"] / chat_id
101
+ if image_dir.exists():
102
+ images = []
103
+ for img_file in sorted(image_dir.glob("*.png")):
104
+ with open(img_file, "rb") as f:
105
+ images.append(f.read())
106
+ chat_data['images'] = images
107
+
108
+ return chat_data
109
+ except Exception as e:
110
+ st.error(f"Error loading chat: {str(e)}")
111
+ return None
112
+
113
+ def save_context(self, context_data):
114
+ """Save conversation context"""
115
+ try:
116
+ context_file = self.paths["context"] / "context.json"
117
+ with open(context_file, "w") as f:
118
+ json.dump(context_data, f)
119
+ except Exception as e:
120
+ st.error(f"Error saving context: {str(e)}")
121
+
122
+ def get_context(self):
123
+ """Get saved conversation context"""
124
+ try:
125
+ context_file = self.paths["context"] / "context.json"
126
+ if context_file.exists():
127
+ with open(context_file, "r") as f:
128
+ return json.load(f)
129
+ return None
130
+ except Exception as e:
131
+ st.error(f"Error loading context: {str(e)}")
132
+ return None
133
+
134
+ def clear_context(self):
135
+ """Clear conversation context"""
136
+ try:
137
+ context_file = self.paths["context"] / "context.json"
138
+ if context_file.exists():
139
+ context_file.unlink()
140
+ except Exception as e:
141
+ st.error(f"Error clearing context: {str(e)}")
142
+
143
+ def delete_chat(self, chat_id):
144
+ """Delete a chat and its associated images"""
145
  try:
146
+ # Delete chat file
147
+ chat_file = self.paths["chats"] / f"{chat_id}.json"
148
+ if chat_file.exists():
149
+ chat_file.unlink()
150
+
151
+ # Delete associated images
152
+ image_dir = self.paths["images"] / chat_id
153
+ if image_dir.exists():
154
+ shutil.rmtree(image_dir)
155
+
156
  return True
157
  except Exception as e:
158
+ st.error(f"Error deleting chat: {str(e)}")
159
  return False
160
 
161
+ def get_storage_stats(self):
162
+ """Get storage usage statistics"""
163
  try:
164
+ stats = {
165
+ 'total_chats': 0,
166
+ 'total_images': 0,
167
+ 'storage_used': 0 # in bytes
168
+ }
169
+
170
+ # Count chats
171
+ stats['total_chats'] = len(list(self.paths["chats"].glob("*.json")))
172
+
173
+ # Count images
174
+ for image_dir in self.paths["images"].glob("*"):
175
+ if image_dir.is_dir():
176
+ stats['total_images'] += len(list(image_dir.glob("*.png")))
177
+
178
+ # Calculate storage used
179
+ for path_type, path in self.paths.items():
180
+ if path.exists():
181
+ for file_path in path.rglob("*"):
182
+ if file_path.is_file():
183
+ stats['storage_used'] += file_path.stat().st_size
184
+
185
+ return stats
186
  except Exception as e:
187
+ st.error(f"Error getting storage stats: {str(e)}")
188
+ return None