LogicGoInfotechSpaces commited on
Commit
d359c5d
·
verified ·
1 Parent(s): a8018cc

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +92 -0
app.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from fastapi import FastAPI, Header, HTTPException
3
+ from fastapi.middleware.cors import CORSMiddleware
4
+ import boto3
5
+
6
+ # -----------------------------
7
+ # CONFIG
8
+ # -----------------------------
9
+ DO_SPACES_REGION = "blr1"
10
+ DO_SPACES_BUCKET = "milestone"
11
+ DO_ACCESS_KEY = os.getenv("DO_SPACES_KEY")
12
+ DO_SECRET_KEY = os.getenv("DO_SPACES_SECRET")
13
+
14
+ BASE_URL = f"https://{DO_SPACES_BUCKET}.{DO_SPACES_REGION}.digitaloceanspaces.com"
15
+ FOLDER_PREFIX = "bikini-theme/target/"
16
+
17
+ AUTH_TOKEN = "logicgo_admin@123"
18
+
19
+ # -----------------------------
20
+ # FASTAPI INIT
21
+ # -----------------------------
22
+ app = FastAPI()
23
+
24
+ app.add_middleware(
25
+ CORSMiddleware,
26
+ allow_origins=["*"],
27
+ allow_credentials=True,
28
+ allow_methods=["*"],
29
+ allow_headers=["*"],
30
+ )
31
+
32
+ # -----------------------------
33
+ # SPACES CLIENT
34
+ # -----------------------------
35
+ def get_spaces_client():
36
+ endpoint = f"{DO_SPACES_REGION}.digitaloceanspaces.com"
37
+ return boto3.client(
38
+ "s3",
39
+ region_name=DO_SPACES_REGION,
40
+ endpoint_url=f"https://{endpoint}",
41
+ aws_access_key_id=DO_ACCESS_KEY,
42
+ aws_secret_access_key=DO_SECRET_KEY,
43
+ )
44
+
45
+ # -----------------------------
46
+ # AUTH MIDDLEWARE
47
+ # -----------------------------
48
+ def check_auth(authorization: str = Header(None)):
49
+ if not authorization:
50
+ raise HTTPException(status_code=401, detail="Missing Authorization header")
51
+
52
+ token = authorization.replace("Bearer ", "").strip()
53
+ if token != AUTH_TOKEN:
54
+ raise HTTPException(status_code=403, detail="Invalid authorization token")
55
+
56
+
57
+ # -----------------------------
58
+ # PROTECTED ROUTE
59
+ # -----------------------------
60
+ @app.get("/bikini-theme")
61
+ def list_bikini_theme_images(authorization: str = Header(None)):
62
+ check_auth(authorization)
63
+
64
+ client = get_spaces_client()
65
+
66
+ # List objects
67
+ resp = client.list_objects_v2(
68
+ Bucket=DO_SPACES_BUCKET,
69
+ Prefix=FOLDER_PREFIX
70
+ )
71
+
72
+ contents = resp.get("Contents", [])
73
+
74
+ image_files = [
75
+ obj["Key"]
76
+ for obj in contents
77
+ if obj["Key"].lower().endswith((".png", ".jpg", ".jpeg", ".webp"))
78
+ ]
79
+
80
+ images = {}
81
+ for file_key in image_files:
82
+ file_name = file_key.split("/")[-1]
83
+ name_without_ext = file_name.rsplit(".", 1)[0]
84
+
85
+ images[name_without_ext] = f"{BASE_URL}/{file_key}"
86
+
87
+ return {"images": images}
88
+
89
+
90
+ @app.get("/")
91
+ def home():
92
+ return {"message": "FastAPI DO Spaces bikini-theme API running on HuggingFace"}