Commit Β·
d550d56
1
Parent(s): babd950
Add Drive-backed project storage and asset upload
Browse files- gateway.py +46 -0
- requirements.txt +2 -0
gateway.py
CHANGED
|
@@ -1498,3 +1498,49 @@ if __name__ == "__main__":
|
|
| 1498 |
Groq : {"β
" if GROQ_KEY else "β set GROQ_API_KEY"}
|
| 1499 |
""")
|
| 1500 |
uvicorn.run(app, host="0.0.0.0", port=PORT)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1498 |
Groq : {"β
" if GROQ_KEY else "β set GROQ_API_KEY"}
|
| 1499 |
""")
|
| 1500 |
uvicorn.run(app, host="0.0.0.0", port=PORT)
|
| 1501 |
+
|
| 1502 |
+
# ββ Drive-backed storage (added for builder-app / admin-app) ββ
|
| 1503 |
+
from fastapi import UploadFile, File
|
| 1504 |
+
from googleapiclient.discovery import build
|
| 1505 |
+
from googleapiclient.http import MediaIoBaseUpload
|
| 1506 |
+
from google.oauth2 import service_account
|
| 1507 |
+
import io, json, os
|
| 1508 |
+
|
| 1509 |
+
DRIVE_FOLDER_ID = os.environ["DRIVE_FOLDER_ID"]
|
| 1510 |
+
_creds = service_account.Credentials.from_service_account_info(
|
| 1511 |
+
json.loads(os.environ["GOOGLE_SERVICE_ACCOUNT_JSON"]),
|
| 1512 |
+
scopes=["https://www.googleapis.com/auth/drive"]
|
| 1513 |
+
)
|
| 1514 |
+
drive = build("drive", "v3", credentials=_creds)
|
| 1515 |
+
|
| 1516 |
+
@app.post("/v1/projects/{page_id}")
|
| 1517 |
+
async def save_project(page_id: str, body: dict):
|
| 1518 |
+
fname = f"{page_id}.json"
|
| 1519 |
+
media = MediaIoBaseUpload(io.BytesIO(json.dumps(body).encode()), mimetype="application/json")
|
| 1520 |
+
existing = drive.files().list(q=f"name='{fname}' and '{DRIVE_FOLDER_ID}' in parents").execute()
|
| 1521 |
+
if existing["files"]:
|
| 1522 |
+
drive.files().update(fileId=existing["files"][0]["id"], media_body=media).execute()
|
| 1523 |
+
else:
|
| 1524 |
+
drive.files().create(body={"name": fname, "parents": [DRIVE_FOLDER_ID]}, media_body=media).execute()
|
| 1525 |
+
return {"status": "ok"}
|
| 1526 |
+
|
| 1527 |
+
@app.get("/v1/projects/{page_id}")
|
| 1528 |
+
async def load_project(page_id: str):
|
| 1529 |
+
fname = f"{page_id}.json"
|
| 1530 |
+
res = drive.files().list(q=f"name='{fname}' and '{DRIVE_FOLDER_ID}' in parents").execute()
|
| 1531 |
+
if not res["files"]:
|
| 1532 |
+
return {"data": {}}
|
| 1533 |
+
content = drive.files().get_media(fileId=res["files"][0]["id"]).execute()
|
| 1534 |
+
return {"data": json.loads(content)}
|
| 1535 |
+
|
| 1536 |
+
@app.post("/v1/assets/upload")
|
| 1537 |
+
async def upload_asset(files: list[UploadFile] = File(...)):
|
| 1538 |
+
urls = []
|
| 1539 |
+
for f in files:
|
| 1540 |
+
media = MediaIoBaseUpload(io.BytesIO(await f.read()), mimetype=f.content_type)
|
| 1541 |
+
created = drive.files().create(
|
| 1542 |
+
body={"name": f.filename, "parents": [DRIVE_FOLDER_ID]}, media_body=media, fields="id"
|
| 1543 |
+
).execute()
|
| 1544 |
+
drive.permissions().create(fileId=created["id"], body={"role": "reader", "type": "anyone"}).execute()
|
| 1545 |
+
urls.append(f"https://drive.google.com/uc?id={created['id']}")
|
| 1546 |
+
return {"data": [{"src": u} for u in urls]}
|
requirements.txt
CHANGED
|
@@ -5,3 +5,5 @@ beautifulsoup4
|
|
| 5 |
python-dotenv
|
| 6 |
colorthief
|
| 7 |
pillow
|
|
|
|
|
|
|
|
|
| 5 |
python-dotenv
|
| 6 |
colorthief
|
| 7 |
pillow
|
| 8 |
+
google-api-python-client
|
| 9 |
+
google-auth
|