Spaces:
Runtime error
Runtime error
nifty-coder commited on
Commit ·
f0c9842
1
Parent(s): dd40bee
R2 check
Browse files- __pycache__/main.cpython-314.pyc +0 -0
- main.py +37 -0
__pycache__/main.cpython-314.pyc
CHANGED
|
Binary files a/__pycache__/main.cpython-314.pyc and b/__pycache__/main.cpython-314.pyc differ
|
|
|
main.py
CHANGED
|
@@ -134,6 +134,8 @@ R2_ACCOUNT_ID = os.environ.get('R2_ACCOUNT_ID')
|
|
| 134 |
|
| 135 |
s3_client = None
|
| 136 |
R2_ENABLED = False
|
|
|
|
|
|
|
| 137 |
|
| 138 |
if all([R2_ENDPOINT, R2_ACCESS_KEY_ID, R2_SECRET_ACCESS_KEY, R2_BUCKET_NAME]):
|
| 139 |
try:
|
|
@@ -152,6 +154,37 @@ if all([R2_ENDPOINT, R2_ACCESS_KEY_ID, R2_SECRET_ACCESS_KEY, R2_BUCKET_NAME]):
|
|
| 152 |
else:
|
| 153 |
logger.warning("R2 credentials not found. File storage will use local temp directory.")
|
| 154 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
# --- reCAPTCHA Verification ---
|
| 156 |
RECAPTCHA_SECRET_KEY = os.environ.get('RECAPTCHA_SECRET_KEY')
|
| 157 |
|
|
@@ -385,6 +418,10 @@ async def process_upload(
|
|
| 385 |
# Verify Firebase token and get UID
|
| 386 |
uid = verify_firebase_token(authorization) if authorization else None
|
| 387 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 388 |
# Generate unique song ID
|
| 389 |
song_id = str(uuid.uuid4())
|
| 390 |
|
|
|
|
| 134 |
|
| 135 |
s3_client = None
|
| 136 |
R2_ENABLED = False
|
| 137 |
+
# 10 GB limit
|
| 138 |
+
MAX_STORAGE_BYTES = 10 * (1000 ** 3)
|
| 139 |
|
| 140 |
if all([R2_ENDPOINT, R2_ACCESS_KEY_ID, R2_SECRET_ACCESS_KEY, R2_BUCKET_NAME]):
|
| 141 |
try:
|
|
|
|
| 154 |
else:
|
| 155 |
logger.warning("R2 credentials not found. File storage will use local temp directory.")
|
| 156 |
|
| 157 |
+
def get_r2_usage_bytes() -> int:
|
| 158 |
+
"""Calculate the total storage usage of the R2 bucket in bytes."""
|
| 159 |
+
if not R2_ENABLED:
|
| 160 |
+
return 0
|
| 161 |
+
|
| 162 |
+
total_size = 0
|
| 163 |
+
try:
|
| 164 |
+
paginator = s3_client.get_paginator('list_objects_v2')
|
| 165 |
+
for page in paginator.paginate(Bucket=R2_BUCKET_NAME):
|
| 166 |
+
if 'Contents' in page:
|
| 167 |
+
for obj in page['Contents']:
|
| 168 |
+
total_size += obj.get('Size', 0)
|
| 169 |
+
return total_size
|
| 170 |
+
except Exception as e:
|
| 171 |
+
logger.error(f"Error calculating R2 usage: {e}")
|
| 172 |
+
return 0
|
| 173 |
+
|
| 174 |
+
def check_storage_limit():
|
| 175 |
+
"""Check if the R2 storage limit has been reached."""
|
| 176 |
+
if not R2_ENABLED:
|
| 177 |
+
return
|
| 178 |
+
|
| 179 |
+
usage = get_r2_usage_bytes()
|
| 180 |
+
logger.info(f"Current R2 storage usage: {usage / (1024*1024*1024):.2f} GB / {MAX_STORAGE_BYTES / (1024*1024*1024):.2f} GB")
|
| 181 |
+
|
| 182 |
+
if usage >= MAX_STORAGE_BYTES:
|
| 183 |
+
raise HTTPException(
|
| 184 |
+
status_code=507,
|
| 185 |
+
detail="Storage limit reached. Please try again tomorrow."
|
| 186 |
+
)
|
| 187 |
+
|
| 188 |
# --- reCAPTCHA Verification ---
|
| 189 |
RECAPTCHA_SECRET_KEY = os.environ.get('RECAPTCHA_SECRET_KEY')
|
| 190 |
|
|
|
|
| 418 |
# Verify Firebase token and get UID
|
| 419 |
uid = verify_firebase_token(authorization) if authorization else None
|
| 420 |
|
| 421 |
+
# Enforce R2 storage limit only if user is authenticated and R2 is enabled
|
| 422 |
+
if R2_ENABLED and uid:
|
| 423 |
+
check_storage_limit()
|
| 424 |
+
|
| 425 |
# Generate unique song ID
|
| 426 |
song_id = str(uuid.uuid4())
|
| 427 |
|