77 lines
2.2 KiB
Plaintext
77 lines
2.2 KiB
Plaintext
from PDAPI import *
|
|
import os
|
|
import json
|
|
import time
|
|
|
|
RESOURCE_ROOT = os.path.join(os.path.abspath(os.path.join(os.getcwd(), "resource")))
|
|
TEMP_ROOT = os.path.join(RESOURCE_ROOT, ".tmp")
|
|
|
|
|
|
def _format_size(size):
|
|
if size < 1024:
|
|
return f"{size} B"
|
|
elif size < 1024 * 1024:
|
|
return f"{size / 1024:.1f} KB"
|
|
elif size < 1024 * 1024 * 1024:
|
|
return f"{size / (1024 * 1024):.1f} MB"
|
|
else:
|
|
return f"{size / (1024 * 1024 * 1024):.1f} GB"
|
|
|
|
|
|
__metadata__ = {
|
|
"methods": ["POST"],
|
|
"blueprint": "GERP",
|
|
"permission": 3.0,
|
|
"attributes": [],
|
|
"limit": "30 per minute",
|
|
}
|
|
|
|
def HandleRequest(_CleanedData):
|
|
current_user = UGM.GetELTUser()
|
|
if not current_user:
|
|
return {"code": 401, "msg": "未登录"}, 401
|
|
|
|
data = flask.request.get_json(silent=True) or {}
|
|
file_name = data.get("fileName", "").strip()
|
|
file_size = data.get("fileSize", 0)
|
|
mime_type = data.get("mimeType", "").strip()
|
|
total_chunks = data.get("totalChunks", 0)
|
|
|
|
if not file_name:
|
|
return {"code": 1, "msg": "文件名不能为空"}, 400
|
|
if file_size <= 0:
|
|
return {"code": 1, "msg": "文件大小无效"}, 400
|
|
if total_chunks <= 0:
|
|
return {"code": 1, "msg": "分片数无效"}, 400
|
|
if file_size > 1 * 1024 * 1024 * 1024:
|
|
return {"code": 1, "msg": "文件大小超过1GB限制"}, 400
|
|
|
|
upload_id = NewBUUID()
|
|
upload_id_str = UUIDObj(upload_id).UpperStr
|
|
|
|
temp_dir = os.path.join(TEMP_ROOT, upload_id_str)
|
|
os.makedirs(temp_dir, exist_ok=True)
|
|
|
|
meta = {
|
|
"fileName": file_name,
|
|
"fileSize": file_size,
|
|
"mimeType": mime_type or "application/octet-stream",
|
|
"totalChunks": total_chunks,
|
|
"uploaderUUID": UUIDObj(current_user.UserUUID).UpperStr,
|
|
"createTime": time.time(),
|
|
"receivedChunks": [],
|
|
}
|
|
|
|
meta_path = os.path.join(temp_dir, "meta.json")
|
|
with open(meta_path, "w", encoding="utf-8") as f:
|
|
json.dump(meta, f, ensure_ascii=False)
|
|
|
|
return {
|
|
"code": 0,
|
|
"msg": "初始化成功",
|
|
"data": {
|
|
"uploadId": upload_id_str,
|
|
"chunkSize": 5 * 1024 * 1024,
|
|
}
|
|
}, 200
|