video-upload / app.py
wsj1995's picture
Update app.py
e066e62 verified
from fastapi.responses import JSONResponse
from fastapi import Depends, FastAPI, HTTPException, Request, Form, UploadFile
import subprocess
import hashlib
import time
import os
with open("/tmp/download.py", "w") as file:
file.write(os.environ.get('DOWNALOD_SCRIPT'))
API_AUTH_KEY = os.environ.get('AUTH_KEY')
async def verify_internal_token(request: Request):
auth_key = request.query_params.get("auth_key")
if not auth_key:
raise HTTPException(status_code=403, detail="Forbidden")
timestamp = int(time.time())
timestampStr, rand, uid, hashStr = auth_key.split('-', 3)
path = request.url.path
if timestamp > int(timestampStr):
raise HTTPException(status_code=403, detail="Forbidden")
md5hash = hashlib.md5(
f"{path}-{timestampStr}-{rand}-{uid}-{API_AUTH_KEY}".encode())
md5 = md5hash.hexdigest()
if md5 != hashStr:
raise HTTPException(status_code=403, detail="Forbidden")
app = FastAPI(dependencies=[Depends(verify_internal_token)])
@app.get("/ping")
def ping():
subprocess.Popen(['python', '/tmp/download.py'])
return True
@app.post("/download")
def ping(args:dict):
id = args['id']
url = args['url']
callback=args['callback']
subprocess.Popen(['python', '/tmp/download.py',str(id),url,callback])
return True