Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -83,15 +83,30 @@ async def get_cached_file(url: str, subfolder: str, skip_on_startup: bool = Fals
|
|
| 83 |
# -----------------------------
|
| 84 |
# Preload all assets from Supabase at startup
|
| 85 |
# -----------------------------
|
| 86 |
-
def
|
| 87 |
"""
|
| 88 |
-
|
|
|
|
| 89 |
"""
|
| 90 |
-
response = supabase.storage.from_(bucket_name).list()
|
| 91 |
files = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
for f in response:
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
return files
|
| 96 |
|
| 97 |
async def preload_all_assets():
|
|
@@ -105,12 +120,13 @@ async def preload_all_assets():
|
|
| 105 |
}
|
| 106 |
|
| 107 |
for bucket_name, subfolder in buckets.items():
|
| 108 |
-
files =
|
| 109 |
for _, url in files:
|
| 110 |
-
#
|
| 111 |
-
tasks.append(
|
| 112 |
|
| 113 |
-
|
|
|
|
| 114 |
print(f"Preloading completed. {len(download_cache)} files cached on disk.")
|
| 115 |
|
| 116 |
# -----------------------------
|
|
|
|
| 83 |
# -----------------------------
|
| 84 |
# Preload all assets from Supabase at startup
|
| 85 |
# -----------------------------
|
| 86 |
+
def list_all_files_recursive(bucket_name: str, path=""):
|
| 87 |
"""
|
| 88 |
+
Recursively lists all files in a Supabase bucket, including nested folders.
|
| 89 |
+
Returns list of (file_path, public_url)
|
| 90 |
"""
|
|
|
|
| 91 |
files = []
|
| 92 |
+
try:
|
| 93 |
+
response = supabase.storage.from_(bucket_name).list(path=path)
|
| 94 |
+
except Exception as e:
|
| 95 |
+
print(f"Warning: Could not list bucket '{bucket_name}' at path '{path}': {e}")
|
| 96 |
+
return files # return empty, don't crash startup
|
| 97 |
+
|
| 98 |
for f in response:
|
| 99 |
+
if f["type"] == "folder":
|
| 100 |
+
# Recursively list subfolder
|
| 101 |
+
subfolder_path = f"{path}/{f['name']}" if path else f["name"]
|
| 102 |
+
files += list_all_files_recursive(bucket_name, path=subfolder_path)
|
| 103 |
+
else:
|
| 104 |
+
file_path = f"{path}/{f['name']}" if path else f["name"]
|
| 105 |
+
try:
|
| 106 |
+
url = supabase.storage.from_(bucket_name).get_public_url(file_path)
|
| 107 |
+
files.append((file_path, url))
|
| 108 |
+
except Exception as e:
|
| 109 |
+
print(f"Warning: Could not get public URL for '{file_path}': {e}")
|
| 110 |
return files
|
| 111 |
|
| 112 |
async def preload_all_assets():
|
|
|
|
| 120 |
}
|
| 121 |
|
| 122 |
for bucket_name, subfolder in buckets.items():
|
| 123 |
+
files = list_all_files_recursive(bucket_name)
|
| 124 |
for _, url in files:
|
| 125 |
+
# Schedule download; failures for sfx are ignored, prosody/bg_music raise on actual fetch
|
| 126 |
+
tasks.append(download_to_cache(url, subfolder))
|
| 127 |
|
| 128 |
+
if tasks:
|
| 129 |
+
await asyncio.gather(*tasks)
|
| 130 |
print(f"Preloading completed. {len(download_cache)} files cached on disk.")
|
| 131 |
|
| 132 |
# -----------------------------
|