RC1
This commit is contained in:
40
register.py
40
register.py
@ -7,11 +7,46 @@ from database import get_db
|
||||
import uuid
|
||||
import smtplib
|
||||
from email.message import EmailMessage
|
||||
from pathlib import Path
|
||||
import os
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
MAGIC_FROM = "noreply@dicta2stream.net"
|
||||
MAGIC_DOMAIN = "https://dicta2stream.net"
|
||||
DATA_ROOT = Path("./data")
|
||||
|
||||
def initialize_user_directory(username: str):
|
||||
"""Initialize user directory with a silent stream.opus file"""
|
||||
try:
|
||||
user_dir = DATA_ROOT / username
|
||||
default_stream_path = DATA_ROOT / "stream.opus"
|
||||
|
||||
print(f"[DEBUG] Initializing user directory: {user_dir.absolute()}")
|
||||
|
||||
# Create the directory if it doesn't exist
|
||||
user_dir.mkdir(parents=True, exist_ok=True)
|
||||
print(f"[DEBUG] Directory created or already exists: {user_dir.exists()}")
|
||||
|
||||
# Create stream.opus by copying the default stream.opus file
|
||||
user_stream_path = user_dir / "stream.opus"
|
||||
print(f"[DEBUG] Creating stream.opus at: {user_stream_path.absolute()}")
|
||||
|
||||
if not user_stream_path.exists():
|
||||
if default_stream_path.exists():
|
||||
import shutil
|
||||
shutil.copy2(default_stream_path, user_stream_path)
|
||||
print(f"[DEBUG] Copied default stream.opus to {user_stream_path}")
|
||||
else:
|
||||
print(f"[ERROR] Default stream.opus not found at {default_stream_path}")
|
||||
# Fallback: create an empty file to prevent errors
|
||||
with open(user_stream_path, 'wb') as f:
|
||||
f.write(b'')
|
||||
|
||||
return True
|
||||
except Exception as e:
|
||||
print(f"Error initializing user directory for {username}: {str(e)}")
|
||||
return False
|
||||
|
||||
@router.post("/register")
|
||||
def register(request: Request, email: str = Form(...), user: str = Form(...), db: Session = Depends(get_db)):
|
||||
@ -40,8 +75,13 @@ def register(request: Request, email: str = Form(...), user: str = Form(...), db
|
||||
# Register new user
|
||||
db.add(User(email=email, username=user, token=token, confirmed=False, ip=request.client.host))
|
||||
db.add(UserQuota(uid=user))
|
||||
|
||||
try:
|
||||
# First commit the user to the database
|
||||
db.commit()
|
||||
|
||||
# Only after successful commit, initialize the user directory
|
||||
initialize_user_directory(user)
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
if isinstance(e, IntegrityError):
|
||||
|
Reference in New Issue
Block a user