Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -6,17 +6,27 @@ import json
|
|
6 |
import base64
|
7 |
from io import BytesIO
|
8 |
from datetime import datetime
|
9 |
-
import
|
|
|
10 |
|
11 |
-
# 配置日志记录器
|
12 |
-
logging.basicConfig(
|
13 |
-
level=logging.INFO, # 设置日志级别为INFO
|
14 |
-
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", # 定义日志格式
|
15 |
-
)
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
|
22 |
UPLOAD_DIRECTORY = "/tmp/uploaded_files"
|
@@ -40,13 +50,11 @@ def read_root():
|
|
40 |
|
41 |
@app.post("/upload")
|
42 |
async def upload_file(file: UploadFile = File(...)):
|
43 |
-
logger.info(f'收到请求 {file.filename}')
|
44 |
# 定义文件存储路径和名称
|
45 |
file_location = os.path.join(UPLOAD_DIRECTORY, file.filename)
|
46 |
|
47 |
# 将上传的文件写入本地文件系统
|
48 |
with open(file_location, "wb") as buffer:
|
49 |
-
logger.info(f'写入本地文件 {file.filename}')
|
50 |
buffer.write(await file.read())
|
51 |
|
52 |
return {"info": f"file '{file.filename}' saved at '{file_location}'"}
|
|
|
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.env.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"
|
|
|
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}'"}
|