-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollaborative_manager.py
More file actions
291 lines (250 loc) · 9.96 KB
/
collaborative_manager.py
File metadata and controls
291 lines (250 loc) · 9.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
"""
Collaborative Data Manager
Handles persistence for collaborative canvas and scratchpad sessions
"""
import os
import json
import time
import base64
from datetime import datetime
class CollaborativeManager:
def __init__(self, base_dir):
self.base_dir = base_dir
self.collab_dir = os.path.join(base_dir, ".collaborative")
self.canvas_dir = os.path.join(self.collab_dir, "canvases")
self.scratchpad_dir = os.path.join(self.collab_dir, "scratchpads")
# Create directories if they don't exist
os.makedirs(self.canvas_dir, exist_ok=True)
os.makedirs(self.scratchpad_dir, exist_ok=True)
# In-memory session tracking
self.active_sessions = {
"canvas": {},
"scratchpad": {}
}
# ===== Canvas Methods =====
def save_canvas(self, canvas_id, data):
"""
Save canvas state to disk
Args:
canvas_id: Unique identifier for the canvas
data: Dictionary containing canvas state
- strokes: List of stroke objects
- metadata: Canvas metadata (size, created, modified)
- image_data: Base64 encoded PNG (optional)
Returns:
dict: Success status and file path
"""
try:
canvas_file = os.path.join(self.canvas_dir, f"{canvas_id}.json")
# Add timestamp
data["last_modified"] = datetime.now().isoformat()
# Save JSON data
with open(canvas_file, 'w', encoding='utf-8') as f:
json.dump(data, f, indent=2)
# Save PNG if image_data provided
if "image_data" in data and data["image_data"]:
png_file = os.path.join(self.canvas_dir, f"{canvas_id}.png")
# Remove data URL prefix if present
img_data = data["image_data"]
if "," in img_data:
img_data = img_data.split(",")[1]
with open(png_file, 'wb') as f:
f.write(base64.b64decode(img_data))
return {
"success": True,
"path": canvas_file,
"timestamp": data["last_modified"]
}
except Exception as e:
return {
"success": False,
"error": str(e)
}
def load_canvas(self, canvas_id):
"""
Load canvas state from disk
Args:
canvas_id: Unique identifier for the canvas
Returns:
dict: Canvas data or None if not found
"""
try:
canvas_file = os.path.join(self.canvas_dir, f"{canvas_id}.json")
if not os.path.exists(canvas_file):
return None
with open(canvas_file, 'r', encoding='utf-8') as f:
data = json.load(f)
# Check if PNG exists
png_file = os.path.join(self.canvas_dir, f"{canvas_id}.png")
if os.path.exists(png_file):
data["has_image"] = True
data["image_path"] = png_file
return data
except Exception as e:
print(f"Error loading canvas {canvas_id}: {e}")
return None
def list_canvases(self):
"""
List all available canvas sessions
Returns:
list: List of canvas metadata
"""
canvases = []
try:
for filename in os.listdir(self.canvas_dir):
if filename.endswith('.json'):
canvas_id = filename[:-5] # Remove .json
data = self.load_canvas(canvas_id)
if data:
canvases.append({
"id": canvas_id,
"last_modified": data.get("last_modified", ""),
"stroke_count": len(data.get("strokes", [])),
"has_image": data.get("has_image", False)
})
except Exception as e:
print(f"Error listing canvases: {e}")
return sorted(canvases, key=lambda x: x["last_modified"], reverse=True)
def delete_canvas(self, canvas_id):
"""Delete a canvas and its associated files"""
try:
canvas_file = os.path.join(self.canvas_dir, f"{canvas_id}.json")
png_file = os.path.join(self.canvas_dir, f"{canvas_id}.png")
if os.path.exists(canvas_file):
os.remove(canvas_file)
if os.path.exists(png_file):
os.remove(png_file)
return {"success": True}
except Exception as e:
return {"success": False, "error": str(e)}
# ===== Scratchpad Methods =====
def save_scratchpad(self, doc_id, content, metadata=None):
"""
Save scratchpad content to disk
Args:
doc_id: Unique identifier for the document
content: Text content (string)
metadata: Optional metadata dict
Returns:
dict: Success status and file path
"""
try:
doc_file = os.path.join(self.scratchpad_dir, f"{doc_id}.md")
# Save content
with open(doc_file, 'w', encoding='utf-8') as f:
f.write(content)
# Save metadata separately
if metadata:
meta_file = os.path.join(self.scratchpad_dir, f"{doc_id}.meta.json")
metadata["last_modified"] = datetime.now().isoformat()
with open(meta_file, 'w', encoding='utf-8') as f:
json.dump(metadata, f, indent=2)
return {
"success": True,
"path": doc_file,
"timestamp": datetime.now().isoformat()
}
except Exception as e:
return {
"success": False,
"error": str(e)
}
def load_scratchpad(self, doc_id):
"""
Load scratchpad content from disk
Args:
doc_id: Unique identifier for the document
Returns:
dict: Document data with content and metadata
"""
try:
doc_file = os.path.join(self.scratchpad_dir, f"{doc_id}.md")
if not os.path.exists(doc_file):
return None
# Load content
with open(doc_file, 'r', encoding='utf-8') as f:
content = f.read()
# Load metadata if exists
meta_file = os.path.join(self.scratchpad_dir, f"{doc_id}.meta.json")
metadata = {}
if os.path.exists(meta_file):
with open(meta_file, 'r', encoding='utf-8') as f:
metadata = json.load(f)
return {
"content": content,
"metadata": metadata,
"last_modified": metadata.get("last_modified", "")
}
except Exception as e:
print(f"Error loading scratchpad {doc_id}: {e}")
return None
def list_scratchpads(self):
"""
List all available scratchpad documents
Returns:
list: List of document metadata
"""
docs = []
try:
for filename in os.listdir(self.scratchpad_dir):
if filename.endswith('.md'):
doc_id = filename[:-3] # Remove .md
data = self.load_scratchpad(doc_id)
if data:
docs.append({
"id": doc_id,
"last_modified": data.get("last_modified", ""),
"content_length": len(data.get("content", "")),
"metadata": data.get("metadata", {})
})
except Exception as e:
print(f"Error listing scratchpads: {e}")
return sorted(docs, key=lambda x: x["last_modified"], reverse=True)
def delete_scratchpad(self, doc_id):
"""Delete a scratchpad document and its metadata"""
try:
doc_file = os.path.join(self.scratchpad_dir, f"{doc_id}.md")
meta_file = os.path.join(self.scratchpad_dir, f"{doc_id}.meta.json")
if os.path.exists(doc_file):
os.remove(doc_file)
if os.path.exists(meta_file):
os.remove(meta_file)
return {"success": True}
except Exception as e:
return {"success": False, "error": str(e)}
# ===== Session Management =====
def get_active_sessions(self):
"""Get all active collaborative sessions"""
return {
"canvases": self.list_canvases(),
"scratchpads": self.list_scratchpads()
}
def create_new_session(self, session_type, name=None):
"""
Create a new collaborative session
Args:
session_type: 'canvas' or 'scratchpad'
name: Optional custom name
Returns:
str: New session ID
"""
timestamp = int(time.time() * 1000)
if name:
session_id = f"{name}_{timestamp}"
else:
session_id = f"{session_type}_{timestamp}"
# Initialize empty session
if session_type == "canvas":
self.save_canvas(session_id, {
"strokes": [],
"metadata": {
"created": datetime.now().isoformat(),
"name": name or f"Canvas {timestamp}"
}
})
elif session_type == "scratchpad":
self.save_scratchpad(session_id, "", {
"created": datetime.now().isoformat(),
"name": name or f"Scratchpad {timestamp}"
})
return session_id