Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -75,13 +75,61 @@ async def lifespan(app: FastAPI):
|
|
| 75 |
|
| 76 |
app = FastAPI(lifespan=lifespan)
|
| 77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
app.add_middleware(
|
| 79 |
CORSMiddleware,
|
| 80 |
-
allow_origins=
|
|
|
|
| 81 |
allow_methods=["*"],
|
| 82 |
allow_headers=["*"],
|
| 83 |
)
|
| 84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
# --- MODELS ---
|
| 86 |
class ItemPayload(BaseModel):
|
| 87 |
name: str
|
|
|
|
| 75 |
|
| 76 |
app = FastAPI(lifespan=lifespan)
|
| 77 |
|
| 78 |
+
# --- CORS ---
|
| 79 |
+
# Replace ["*"] with your actual external site URL to prevent strangers from using your API
|
| 80 |
+
ALLOWED_ORIGINS = [
|
| 81 |
+
"http://localhost:3000", # For your local testing
|
| 82 |
+
"https://test.1ink.us", # <--- REPLACE THIS with your actual site
|
| 83 |
+
"https://go.1ink.us", # <--- REPLACE THIS with your actual site
|
| 84 |
+
"https://noahcohn.com", # <--- REPLACE THIS with your actual site
|
| 85 |
+
]
|
| 86 |
+
|
| 87 |
app.add_middleware(
|
| 88 |
CORSMiddleware,
|
| 89 |
+
allow_origins=ALLOWED_ORIGINS, # Uses the list above
|
| 90 |
+
allow_credentials=True,
|
| 91 |
allow_methods=["*"],
|
| 92 |
allow_headers=["*"],
|
| 93 |
)
|
| 94 |
|
| 95 |
+
|
| 96 |
+
# --- DIRECT STORAGE LISTING ---
|
| 97 |
+
@app.get("/api/storage/files")
|
| 98 |
+
async def list_gcs_folder(folder: str = Query(..., description="Folder name, e.g., 'songs' or 'samples'")):
|
| 99 |
+
"""
|
| 100 |
+
Directly lists files in a GCS folder (ignoring the JSON index).
|
| 101 |
+
Useful for seeing what is actually on the disk.
|
| 102 |
+
"""
|
| 103 |
+
# 1. Get the correct prefix from your STORAGE_MAP, or use the folder name directly
|
| 104 |
+
# This handles cases where user types "song" but folder is "songs/"
|
| 105 |
+
config = STORAGE_MAP.get(folder)
|
| 106 |
+
prefix = config["folder"] if config else f"{folder}/"
|
| 107 |
+
|
| 108 |
+
try:
|
| 109 |
+
# 2. Run GCS List Blobs in a thread (to keep server fast)
|
| 110 |
+
def _fetch_blobs():
|
| 111 |
+
# 'delimiter' makes it behave like a folder (doesn't show sub-sub-files)
|
| 112 |
+
blobs = bucket.list_blobs(prefix=prefix, delimiter="/")
|
| 113 |
+
|
| 114 |
+
file_list = []
|
| 115 |
+
for blob in blobs:
|
| 116 |
+
# Remove the folder prefix (e.g. "songs/beat1.json" -> "beat1.json")
|
| 117 |
+
name = blob.name.replace(prefix, "")
|
| 118 |
+
if name and name != "":
|
| 119 |
+
file_list.append({
|
| 120 |
+
"filename": name,
|
| 121 |
+
"size": blob.size,
|
| 122 |
+
"updated": blob.updated.isoformat() if blob.updated else None,
|
| 123 |
+
"url": blob.public_url if blob.public_url else None
|
| 124 |
+
})
|
| 125 |
+
return file_list
|
| 126 |
+
|
| 127 |
+
files = await run_io(_fetch_blobs)
|
| 128 |
+
return {"folder": prefix, "count": len(files), "files": files}
|
| 129 |
+
|
| 130 |
+
except Exception as e:
|
| 131 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 132 |
+
|
| 133 |
# --- MODELS ---
|
| 134 |
class ItemPayload(BaseModel):
|
| 135 |
name: str
|