|
@@ -0,0 +1,93 @@
|
|
|
+from fastapi import APIRouter,FastAPI, Depends, HTTPException, status
|
|
|
+from pydantic import BaseModel
|
|
|
+from supabase import create_client, Client
|
|
|
+from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
|
|
|
+from fastapi import UploadFile, File, Form
|
|
|
+from jose import JWTError, jwt
|
|
|
+from passlib.context import CryptContext
|
|
|
+from datetime import datetime, timedelta
|
|
|
+from dotenv import load_dotenv
|
|
|
+import os
|
|
|
+import shutil
|
|
|
+import requests
|
|
|
+from typing import Optional
|
|
|
+from fastapi.responses import JSONResponse
|
|
|
+from jose import jwt
|
|
|
+from google.oauth2 import id_token
|
|
|
+from google.auth.transport import requests
|
|
|
+from google.oauth2.credentials import Credentials
|
|
|
+from google_auth_oauthlib.flow import InstalledAppFlow
|
|
|
+from email.mime.multipart import MIMEMultipart
|
|
|
+from email.mime.text import MIMEText
|
|
|
+from google.auth.transport.requests import Request
|
|
|
+import string
|
|
|
+import random
|
|
|
+import supabase
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+SUPABASE_URL = "http://172.105.241.163:8000/"
|
|
|
+SUPABASE_KEY = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyAgCiAgICAicm9sZSI6ICJzZXJ2aWNlX3JvbGUiLAogICAgImlzcyI6ICJzdXBhYmFzZS1kZW1vIiwKICAgICJpYXQiOiAxNjQxNzY5MjAwLAogICAgImV4cCI6IDE3OTk1MzU2MDAKfQ.DaYlNEoUrrEn2Ig7tqibS-PHK5vgusbcbo7X36XVt4Q"
|
|
|
+SECRET_KEY = "EdmSaasUserSecretKey"
|
|
|
+USER_TABLE="EDM_SAAS_USER"
|
|
|
+ALGORITHM = "HS256"
|
|
|
+
|
|
|
+supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
|
|
|
+router = APIRouter()
|
|
|
+
|
|
|
+class User(BaseModel):
|
|
|
+ username: str
|
|
|
+ password: str
|
|
|
+
|
|
|
+class UserInDB(BaseModel):
|
|
|
+ username: str
|
|
|
+ hashed_password: str
|
|
|
+ token :Optional[str]
|
|
|
+ detect_info : Optional[str]
|
|
|
+ point : Optional[int]
|
|
|
+ recharge : Optional[int]
|
|
|
+
|
|
|
+pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
|
|
+oauth2_scheme = OAuth2PasswordBearer(tokenUrl="auth/token")
|
|
|
+
|
|
|
+async def get_user(username: str):
|
|
|
+ user_table: str = USER_TABLE
|
|
|
+ response = supabase.table(user_table).select('*').eq('username', username).execute()
|
|
|
+ users = response.data
|
|
|
+ if users:
|
|
|
+ user_dict = users[0]
|
|
|
+ return UserInDB(**user_dict)
|
|
|
+ return None
|
|
|
+
|
|
|
+@router.post("/update-detect-info")
|
|
|
+async def update_detect_info(
|
|
|
+ token: str = Depends(oauth2_scheme),
|
|
|
+ title: str=Form(...),
|
|
|
+ anchor: str=Form(...),
|
|
|
+ style: str=Form(...),
|
|
|
+ lang: str=Form(...),
|
|
|
+ upload_file: UploadFile=File(),
|
|
|
+ ):
|
|
|
+ # 驗證身分
|
|
|
+ credentials_exception = HTTPException(
|
|
|
+ status_code=status.HTTP_401_UNAUTHORIZED,
|
|
|
+ detail="Could not validate credentials",
|
|
|
+ headers={"WWW-Authenticate": "Bearer"},
|
|
|
+ )
|
|
|
+ try:
|
|
|
+ payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
|
|
|
+ username: str = payload.get("sub")
|
|
|
+ if username is None:
|
|
|
+ raise credentials_exception
|
|
|
+ except JWTError:
|
|
|
+ raise credentials_exception
|
|
|
+
|
|
|
+ user = await get_user(username)
|
|
|
+ if user is None:
|
|
|
+ raise credentials_exception
|
|
|
+
|
|
|
+ video_data = {"title":title, "anchor":anchor, "lang_id":lang}
|
|
|
+ print(video_data)
|
|
|
+
|
|
|
+
|