Spaces:
Sleeping
Sleeping
feat: 修改目录结构
Browse files- .gitignore +1 -0
- api.py +62 -0
- main.py +4 -61
.gitignore
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
__pycache__
|
api.py
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, File, UploadFile, HTTPException, Request, Depends, APIRouter
|
2 |
+
from fastapi.responses import StreamingResponse
|
3 |
+
import requests
|
4 |
+
import os
|
5 |
+
import json
|
6 |
+
import base64
|
7 |
+
from io import BytesIO
|
8 |
+
from datetime import datetime
|
9 |
+
import hashlib
|
10 |
+
import time
|
11 |
+
|
12 |
+
|
13 |
+
async def verify_internal_token(request: Request):
|
14 |
+
auth_key = request.query_params.get("auth_key")
|
15 |
+
if not auth_key:
|
16 |
+
raise HTTPException(status_code=403, detail="Forbidden")
|
17 |
+
timestamp = int(time.time())
|
18 |
+
timestampStr,rand,uid,hashStr = auth_key.split('-', 3)
|
19 |
+
path = request.url.path
|
20 |
+
if timestamp > int(timestampStr):
|
21 |
+
raise HTTPException(status_code=403, detail="Forbidden")
|
22 |
+
md5hash = hashlib.md5(f"{path}-{timestampStr}-{rand}-{uid}-{os.environ.get('AUTH_KEY')}".encode())
|
23 |
+
md5 = md5hash.hexdigest()
|
24 |
+
if md5 != hashStr:
|
25 |
+
raise HTTPException(status_code=403, detail="Forbidden")
|
26 |
+
|
27 |
+
|
28 |
+
router = APIRouter()
|
29 |
+
|
30 |
+
UPLOAD_DIRECTORY = "/tmp/uploaded_files"
|
31 |
+
|
32 |
+
# 如果目录不存在,则创建
|
33 |
+
if not os.path.exists(UPLOAD_DIRECTORY):
|
34 |
+
os.makedirs(UPLOAD_DIRECTORY)
|
35 |
+
|
36 |
+
|
37 |
+
def download_file(url: str):
|
38 |
+
with requests.get(url, stream=True) as response:
|
39 |
+
for chunk in response.iter_content(chunk_size=1024): # 每次读取 1KB
|
40 |
+
if chunk:
|
41 |
+
yield chunk
|
42 |
+
|
43 |
+
|
44 |
+
@router.get("/")
|
45 |
+
def read_root():
|
46 |
+
return {"Hello": "World!"}
|
47 |
+
|
48 |
+
@router.post("/upload")
|
49 |
+
async def upload_file(file: UploadFile = File(...)):
|
50 |
+
# 定义文件存储路径和名称
|
51 |
+
file_location = os.path.join(UPLOAD_DIRECTORY, file.filename)
|
52 |
+
|
53 |
+
# 将上传的文件写入本地文件系统
|
54 |
+
with open(file_location, "wb") as buffer:
|
55 |
+
buffer.write(await file.read())
|
56 |
+
|
57 |
+
return {"info": f"file '{file.filename}' saved at '{file_location}'"}
|
58 |
+
|
59 |
+
|
60 |
+
@router.get("/download")
|
61 |
+
def download():
|
62 |
+
return StreamingResponse(download_file("https://huggingface.co/wsj1995/stable-diffusion-models/resolve/main/3Guofeng3_v34.safetensors?download=true"), media_type="application/octet-stream")
|
main.py
CHANGED
@@ -1,65 +1,8 @@
|
|
1 |
-
from fastapi import FastAPI
|
2 |
-
|
3 |
-
import requests
|
4 |
-
import os
|
5 |
-
import json
|
6 |
-
import base64
|
7 |
-
from io import BytesIO
|
8 |
-
from datetime import datetime
|
9 |
-
import hashlib
|
10 |
-
import time
|
11 |
|
12 |
|
13 |
-
|
14 |
-
auth_key = request.query_params.get("auth_key")
|
15 |
-
if not auth_key:
|
16 |
-
raise HTTPException(status_code=403, detail="Forbidden")
|
17 |
-
timestamp = int(time.time())
|
18 |
-
timestampStr,rand,uid,hashStr = auth_key.split('-', 3)
|
19 |
-
path = request.url.path
|
20 |
-
if timestamp > int(timestampStr):
|
21 |
-
raise HTTPException(status_code=403, detail="Forbidden")
|
22 |
-
md5hash = hashlib.md5(f"{path}-{timestampStr}-{rand}-{uid}-{os.environ.get('AUTH_KEY')}".encode())
|
23 |
-
md5 = md5hash.hexdigest()
|
24 |
-
if md5 != hashStr:
|
25 |
-
raise HTTPException(status_code=403, detail="Forbidden")
|
26 |
-
|
27 |
|
|
|
28 |
|
29 |
-
app = FastAPI(dependencies=[Depends(verify_internal_token)])
|
30 |
-
|
31 |
-
|
32 |
-
UPLOAD_DIRECTORY = "/tmp/uploaded_files"
|
33 |
-
|
34 |
-
# 如果目录不存在,则创建
|
35 |
-
if not os.path.exists(UPLOAD_DIRECTORY):
|
36 |
-
os.makedirs(UPLOAD_DIRECTORY)
|
37 |
-
|
38 |
-
|
39 |
-
def download_file(url: str):
|
40 |
-
with requests.get(url, stream=True) as response:
|
41 |
-
for chunk in response.iter_content(chunk_size=1024): # 每次读取 1KB
|
42 |
-
if chunk:
|
43 |
-
yield chunk
|
44 |
-
# HF_API_TOKEN = "your_huggingface_api_token" # 替换为您的 Hugging Face API Token
|
45 |
-
# HfFolder.save_token(HF_API_TOKEN)
|
46 |
-
|
47 |
-
@app.get("/")
|
48 |
-
def read_root():
|
49 |
-
return {"Hello": "World!"}
|
50 |
-
|
51 |
-
@app.post("/upload")
|
52 |
-
async def upload_file(file: UploadFile = File(...)):
|
53 |
-
# 定义文件存储路径和名称
|
54 |
-
file_location = os.path.join(UPLOAD_DIRECTORY, file.filename)
|
55 |
-
|
56 |
-
# 将上传的文件写入本地文件系统
|
57 |
-
with open(file_location, "wb") as buffer:
|
58 |
-
buffer.write(await file.read())
|
59 |
-
|
60 |
-
return {"info": f"file '{file.filename}' saved at '{file_location}'"}
|
61 |
-
|
62 |
-
|
63 |
-
@app.get("/download")
|
64 |
-
def download():
|
65 |
-
return StreamingResponse(download_file("https://huggingface.co/wsj1995/stable-diffusion-models/resolve/main/3Guofeng3_v34.safetensors?download=true"), media_type="application/octet-stream")
|
|
|
1 |
+
from fastapi import FastAPI
|
2 |
+
import api
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
|
5 |
+
app = FastAPI(dependencies=[Depends(api.verify_internal_token)])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
+
app.include_router(api.router)
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|