MariaKaiser commited on
Commit
713775d
·
verified ·
1 Parent(s): 3687c1d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -20
app.py CHANGED
@@ -83,31 +83,29 @@ 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 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():
113
  print("Starting Supabase asset preloading...")
 
83
  # -----------------------------
84
  # Preload all assets from Supabase at startup
85
  # -----------------------------
86
+ def list_all_files_recursive(bucket_name: str, path="") -> list[tuple[str, str]]:
87
  """
88
+ Returns a list of (file_name, public_url) for all files in the bucket, including nested folders.
89
+ Folders themselves are ignored.
90
  """
91
+ files_list = []
92
+ response = supabase.storage.from_(bucket_name).list(path=path)
 
 
 
 
93
 
94
  for f in response:
95
+ # Safely get type, default to 'file' if missing
96
+ item_type = f.get("type", "file")
97
+ item_name = f["name"]
98
+ full_path = f"{path}/{item_name}" if path else item_name
99
+
100
+ if item_type == "folder":
101
+ # Recursively get files in this folder
102
+ files_list.extend(list_all_files_recursive(bucket_name, full_path))
103
  else:
104
+ # Only add actual files
105
+ url = supabase.storage.from_(bucket_name).get_public_url(full_path)
106
+ files_list.append((full_path, url))
107
+
108
+ return files_list
 
 
109
 
110
  async def preload_all_assets():
111
  print("Starting Supabase asset preloading...")