Spaces:
Sleeping
Sleeping
Update user_management.py
Browse files- user_management.py +70 -0
user_management.py
CHANGED
|
@@ -320,6 +320,76 @@ class HFUserManager:
|
|
| 320 |
|
| 321 |
return user.get("password_hash") == password_hash
|
| 322 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 323 |
|
| 324 |
# ============================================
|
| 325 |
# CONVENIENCE FUNCTIONS
|
|
|
|
| 320 |
|
| 321 |
return user.get("password_hash") == password_hash
|
| 322 |
|
| 323 |
+
# ============================================
|
| 324 |
+
# USER STORAGE FILE HELPERS
|
| 325 |
+
# ============================================
|
| 326 |
+
def _path_in_repo_for_user(self, user_id: str, filename: str) -> str:
|
| 327 |
+
return f"users/{user_id}/{filename}"
|
| 328 |
+
|
| 329 |
+
def save_user_json(self, user_id: str, filename: str, data: dict, commit_message: str = None) -> bool:
|
| 330 |
+
"""
|
| 331 |
+
Save a JSON file into the user's storage folder in the HF repo.
|
| 332 |
+
"""
|
| 333 |
+
if not self.enabled:
|
| 334 |
+
print("⚠️ HF User Manager disabled, cannot save user file")
|
| 335 |
+
return False
|
| 336 |
+
|
| 337 |
+
if commit_message is None:
|
| 338 |
+
commit_message = f"Update {user_id}/{filename}"
|
| 339 |
+
|
| 340 |
+
try:
|
| 341 |
+
# Create temp file
|
| 342 |
+
import tempfile
|
| 343 |
+
with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as tmp:
|
| 344 |
+
json.dump(data, tmp, indent=2, ensure_ascii=False)
|
| 345 |
+
tmp_path = tmp.name
|
| 346 |
+
|
| 347 |
+
path_in_repo = self._path_in_repo_for_user(user_id, filename)
|
| 348 |
+
|
| 349 |
+
try:
|
| 350 |
+
self.api.upload_file(
|
| 351 |
+
path_or_fileobj=tmp_path,
|
| 352 |
+
path_in_repo=path_in_repo,
|
| 353 |
+
repo_id=self.hf_repo,
|
| 354 |
+
token=self.hf_token,
|
| 355 |
+
repo_type='model',
|
| 356 |
+
commit_message=commit_message
|
| 357 |
+
)
|
| 358 |
+
print(f"✅ Saved {path_in_repo} to HF repo")
|
| 359 |
+
return True
|
| 360 |
+
finally:
|
| 361 |
+
if os.path.exists(tmp_path):
|
| 362 |
+
os.remove(tmp_path)
|
| 363 |
+
|
| 364 |
+
except Exception as e:
|
| 365 |
+
print(f"❌ Failed to save user file to HF: {e}")
|
| 366 |
+
return False
|
| 367 |
+
|
| 368 |
+
def load_user_json(self, user_id: str, filename: str) -> Optional[dict]:
|
| 369 |
+
"""
|
| 370 |
+
Load a JSON file from the user's storage folder in the HF repo.
|
| 371 |
+
Returns the parsed JSON dict or None if not found / error.
|
| 372 |
+
"""
|
| 373 |
+
if not self.enabled:
|
| 374 |
+
return None
|
| 375 |
+
|
| 376 |
+
path_in_repo = self._path_in_repo_for_user(user_id, filename)
|
| 377 |
+
try:
|
| 378 |
+
downloaded_path = hf_hub_download(
|
| 379 |
+
repo_id=self.hf_repo,
|
| 380 |
+
filename=path_in_repo,
|
| 381 |
+
token=self.hf_token,
|
| 382 |
+
repo_type='model',
|
| 383 |
+
local_dir_use_symlinks=False
|
| 384 |
+
)
|
| 385 |
+
|
| 386 |
+
with open(downloaded_path, 'r', encoding='utf-8') as f:
|
| 387 |
+
return json.load(f)
|
| 388 |
+
|
| 389 |
+
except Exception as e:
|
| 390 |
+
# Not found or error
|
| 391 |
+
return None
|
| 392 |
+
|
| 393 |
|
| 394 |
# ============================================
|
| 395 |
# CONVENIENCE FUNCTIONS
|