rename file -> url and add base_url
Build and publish bnuy api / build (push) Successful in 13m9s

This commit is contained in:
2026-05-12 23:47:23 +02:00
parent ec841012dd
commit 957ad5ba38
+3 -3
View File
@@ -1,5 +1,5 @@
import aiohttp
from fastapi import FastAPI, HTTPException
from fastapi import FastAPI, HTTPException, Request
from fastapi.responses import FileResponse
import asyncmy
import asyncio
@@ -72,7 +72,7 @@ async def root():
return {"message": "yes the api works, maybe i will create a small landing page later here"}
@app.get("/random")
async def get_random_bnuy():
async def get_random_bnuy(request: Request):
async with app.state.pool.acquire() as conn:
async with conn.cursor() as cursor:
await cursor.execute("SELECT filename, subreddit, url FROM images ORDER BY RAND() LIMIT 1;")
@@ -80,7 +80,7 @@ async def get_random_bnuy():
if result:
filepath = os.path.join("data/images", result[0])
if os.path.exists(filepath):
return {"file": f"/images/{result[0]}", "source": f"https://www.reddit.com/r/{result[1]}/", "original_url": result[2]}
return {"url": f"{request.base_url}/images/{result[0]}", "source": f"https://www.reddit.com/r/{result[1]}/", "original_url": result[2]}
else:
raise HTTPException(status_code=404, detail="Image file not found")
else: