File size: 5,766 Bytes
69bded7
 
 
 
 
4d15d01
69bded7
 
 
 
 
 
 
 
 
01b26cd
69bded7
 
 
 
 
4d15d01
69bded7
 
 
 
 
 
 
4d15d01
69bded7
 
 
 
 
 
 
 
 
01b26cd
69bded7
 
 
4d15d01
69bded7
4d15d01
01b26cd
 
4d15d01
01b26cd
 
 
 
 
 
 
 
69bded7
01b26cd
69bded7
 
 
01b26cd
4d15d01
01b26cd
4d15d01
01b26cd
69bded7
01b26cd
 
 
69bded7
 
 
 
 
 
01b26cd
4d15d01
 
01b26cd
69bded7
 
 
4d15d01
 
01b26cd
4d15d01
01b26cd
 
4d15d01
 
 
01b26cd
4d15d01
 
 
 
01b26cd
4d15d01
01b26cd
69bded7
 
 
4d15d01
 
 
01b26cd
 
 
4d15d01
 
 
 
01b26cd
4d15d01
 
 
 
 
 
 
 
 
 
01b26cd
4d15d01
 
 
69bded7
 
 
4d15d01
 
 
 
 
 
 
 
 
 
 
 
 
 
01b26cd
69bded7
 
 
4d15d01
 
01b26cd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import os
import json
import requests
import shutil
import time
import traceback
from typing import List, Optional
from fastapi import FastAPI, HTTPException, UploadFile, File, Form, Body
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse, JSONResponse
from pydantic import BaseModel
from passlib.context import CryptContext
from datetime import datetime

# --- KONFIGURASI ---
app = FastAPI(title="BlackSpaces Brain v1.3 (Bulletproof)")

JSONBIN_ID = "6962ebfe43b1c97be927b07d"
JSONBIN_KEY = "$2a$10$khoKkhYEAUCG.O0xHDHiY.Ei88KlSV1l3olI2pxc86mUSsTaNpJx6"
JSONBIN_URL = f"https://api.jsonbin.io/v3/b/{JSONBIN_ID}"

pwd_context = CryptContext(schemes=["sha256_crypt"], deprecated="auto")

GALLERY_DIR = "gallery_storage"
if not os.path.exists(GALLERY_DIR):
    os.makedirs(GALLERY_DIR)

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

class UserAuth(BaseModel):
    username: str
    password: str

# --- HELPER FUNCTIONS ---
def get_users_db():
    headers = {"X-Master-Key": JSONBIN_KEY}
    try:
        response = requests.get(JSONBIN_URL + "/latest", headers=headers, timeout=10)
        if response.status_code == 200:
            data = response.json()
            # Handle berbagai struktur JSONBin
            users = []
            if "record" in data:
                users = data["record"].get("users", [])
            else:
                users = data.get("users", [])
            
            # Filter user yang rusak (tidak punya username)
            valid_users = [u for u in users if isinstance(u, dict) and "username" in u]
            return valid_users
        return []
    except Exception as e:
        print(f"DB Error: {e}")
        return []

def update_users_db(users_list):
    headers = {"X-Master-Key": JSONBIN_KEY, "Content-Type": "application/json"}
    try:
        requests.put(JSONBIN_URL, headers=headers, json={"users": users_list}, timeout=10)
    except Exception as e:
        print(f"Save Error: {e}")

def verify_password(plain, hashed):
    try: return pwd_context.verify(plain, hashed)
    except: return False

def get_password_hash(password):
    return pwd_context.hash(password)

# --- ENDPOINTS ---
@app.get("/")
def home(): return {"status": "online", "system": "v1.3"}

@app.get("/ping")
def ping(): return {"status": "alive"}

@app.post("/auth/signup")
def signup(user: UserAuth):
    try:
        users = get_users_db()
        # Cek duplikat dengan aman
        for u in users:
            if u.get('username', '').lower() == user.username.lower():
                return JSONResponse(status_code=400, content={"status": "error", "detail": "Username taken"})
        
        new_user = {
            "username": user.username,
            "password": get_password_hash(user.password),
            "joined_at": str(datetime.now())
        }
        users.append(new_user)
        update_users_db(users)
        return {"status": "success", "message": "Registered"}
    except Exception as e:
        return JSONResponse(status_code=500, content={"status": "error", "detail": str(e)})

@app.post("/auth/login")
def login(user: UserAuth):
    try:
        users = get_users_db()
        for u in users:
            if u.get('username', '').lower() == user.username.lower():
                if verify_password(user.password, u.get('password', '')):
                    return {"status": "success", "username": u['username'], "token": "ok"}
                else:
                    return JSONResponse(status_code=401, content={"status": "error", "detail": "Wrong Password"})
        return JSONResponse(status_code=404, content={"status": "error", "detail": "User not found"})
    except Exception as e:
        return JSONResponse(status_code=500, content={"status": "error", "detail": str(e)})

@app.post("/gallery/save")
async def save_to_gallery(username: str = Form(...), prompt: str = Form(...), meta: str = Form(...), image: UploadFile = File(...)):
    try:
        user_dir = os.path.join(GALLERY_DIR, username)
        if not os.path.exists(user_dir): os.makedirs(user_dir)
        timestamp = int(time.time())
        path_img = os.path.join(user_dir, f"{timestamp}.jpg")
        path_meta = os.path.join(user_dir, f"{timestamp}.json")
        with open(path_img, "wb") as buffer: shutil.copyfileobj(image.file, buffer)
        with open(path_meta, "w") as f: json.dump({"prompt": prompt, "details": meta, "date": str(datetime.now())}, f)
        return {"status": "success", "file_id": timestamp}
    except Exception as e:
        return JSONResponse(status_code=500, content={"status": "error", "detail": str(e)})

@app.get("/gallery/{username}")
def get_user_gallery(username: str):
    try:
        user_dir = os.path.join(GALLERY_DIR, username)
        if not os.path.exists(user_dir): return {"images": []}
        images = []
        for file in os.listdir(user_dir):
            if file.endswith(".jpg"):
                img_id = file.split(".")[0]
                meta_path = os.path.join(user_dir, f"{img_id}.json")
                meta_data = {}
                if os.path.exists(meta_path):
                    with open(meta_path, "r") as f: meta_data = json.load(f)
                images.append({"id": img_id, "url": f"/gallery/view/{username}/{file}", "meta": meta_data})
        images.sort(key=lambda x: x['id'], reverse=True)
        return {"images": images}
    except: return {"images": []}

@app.get("/gallery/view/{username}/{filename}")
def view_image(username: str, filename: str):
    path = os.path.join(GALLERY_DIR, username, filename)
    if os.path.exists(path): return FileResponse(path)
    raise HTTPException(status_code=404)