Add random background getter
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
a213ca0c50
commit
670ddda93a
|
@ -3,6 +3,7 @@ anyio==3.7.1
|
||||||
beautifulsoup4==4.12.2
|
beautifulsoup4==4.12.2
|
||||||
certifi==2023.7.22
|
certifi==2023.7.22
|
||||||
charset-normalizer==3.2.0
|
charset-normalizer==3.2.0
|
||||||
|
chevron==0.14.0
|
||||||
click==8.1.6
|
click==8.1.6
|
||||||
envyaml==1.10.211231
|
envyaml==1.10.211231
|
||||||
exceptiongroup==1.1.2
|
exceptiongroup==1.1.2
|
||||||
|
@ -16,6 +17,7 @@ isodate==0.6.1
|
||||||
jstyleson==0.0.2
|
jstyleson==0.0.2
|
||||||
lxml==4.9.3
|
lxml==4.9.3
|
||||||
mf2py==1.1.3
|
mf2py==1.1.3
|
||||||
|
minio==7.1.17
|
||||||
pydantic==2.1.1
|
pydantic==2.1.1
|
||||||
pydantic_core==2.4.0
|
pydantic_core==2.4.0
|
||||||
pyparsing==3.1.1
|
pyparsing==3.1.1
|
||||||
|
|
|
@ -1 +1,8 @@
|
||||||
|
images:
|
||||||
|
# This component will serve files from a static bucket, randomly
|
||||||
|
# You should set the bucket to public, we will return the url
|
||||||
|
access_key: ${ACCESS_KEY}
|
||||||
|
secret_key: ${SECRET_KEY}
|
||||||
|
endpoint: s3.clortox.com
|
||||||
|
bucket: static-assets
|
||||||
|
secure: True
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
from datetime import datetime, date
|
from datetime import datetime, date
|
||||||
from fastapi import FastAPI, HTTPException
|
from fastapi import FastAPI, HTTPException
|
||||||
|
|
||||||
from routers import recipe
|
from routers import recipe, images
|
||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
|
|
||||||
app.include_router(recipe.router)
|
app.include_router(recipe.router)
|
||||||
|
app.include_router(images.router)
|
||||||
|
|
|
@ -0,0 +1,62 @@
|
||||||
|
from fastapi import APIRouter, Depends, HTTPException, Header
|
||||||
|
|
||||||
|
from typing import List, Optional
|
||||||
|
from pydantic import BaseModel
|
||||||
|
import logging
|
||||||
|
from config import getConfig
|
||||||
|
|
||||||
|
from minio import Minio
|
||||||
|
import random
|
||||||
|
import tempfile
|
||||||
|
import traceback
|
||||||
|
|
||||||
|
router = APIRouter(
|
||||||
|
prefix="/images",
|
||||||
|
tags=["Images"],
|
||||||
|
responses={404: {"description": "Not found"}}
|
||||||
|
)
|
||||||
|
|
||||||
|
@router.get("/background/random")
|
||||||
|
async def background(accept: str = Header(None)):
|
||||||
|
"""
|
||||||
|
Will obtain a random image from the configured S3 bucket.
|
||||||
|
"""
|
||||||
|
endpoint = getConfig()["images.endpoint"]
|
||||||
|
access_key = getConfig()["images.access_key"]
|
||||||
|
secret_key = getConfig()["images.secret_key"]
|
||||||
|
bucket = getConfig()["images.bucket"]
|
||||||
|
secure = getConfig()["images.secure"]
|
||||||
|
|
||||||
|
try:
|
||||||
|
client = Minio(
|
||||||
|
endpoint,
|
||||||
|
access_key=access_key,
|
||||||
|
secret_key=secret_key,
|
||||||
|
secure=secure
|
||||||
|
)
|
||||||
|
|
||||||
|
objects = client.list_objects(bucket)
|
||||||
|
object_names = [obj.object_name for obj in objects]
|
||||||
|
if not object_names:
|
||||||
|
raise HTTPException(status_code=500, detail="No data in bucket!")
|
||||||
|
|
||||||
|
random_object_name = random.choice(object_names)
|
||||||
|
print(random_object_name)
|
||||||
|
scheme = "https" if secure else "http"
|
||||||
|
object_url = f"{scheme}://{endpoint}/{bucket}/{random_object_name}"
|
||||||
|
|
||||||
|
|
||||||
|
if accept and "image/" in accept:
|
||||||
|
with tempfile.NamedTemporaryFile(detel=True) as temp_file:
|
||||||
|
client.fget_object(bucket, random_object_name, temp_file)
|
||||||
|
return FileResponse(temp_file.name, headers={"Content-Disposition": f"attachment; filename={random_object_name}"})
|
||||||
|
|
||||||
|
return object_url
|
||||||
|
|
||||||
|
|
||||||
|
except Exception as err:
|
||||||
|
traceback.print_exc()
|
||||||
|
raise HTTPException(status_code=500, detail=f"Unknown error. Check logs {err}")
|
||||||
|
print(f"Error occurued processing background {err}")
|
||||||
|
|
||||||
|
# TODO get a sync up background
|
Loading…
Reference in New Issue