feat: зеркало кода ВМ — deploy/ из contractor
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
"""Upload service — accept file, store to DB, parse."""
|
||||
import uuid, base64, json, io, cgi, os
|
||||
from db import documents, contracts, supplements
|
||||
from .parse import parse_file
|
||||
|
||||
MAX_FILE_SIZE = 100 * 1024 * 1024 # 100 MB
|
||||
|
||||
|
||||
def handle_upload(rfile, content_type, content_length):
|
||||
"""Parse multipart upload, store in DB, return result dict."""
|
||||
# Validate content_length
|
||||
try:
|
||||
cl = int(content_length)
|
||||
except (TypeError, ValueError):
|
||||
return {"ok": False, "error": "invalid content-length"}
|
||||
if cl <= 0:
|
||||
return {"ok": False, "error": "empty request"}
|
||||
if cl > MAX_FILE_SIZE:
|
||||
return {"ok": False, "error": f"file too large (max {MAX_FILE_SIZE // 1024 // 1024}MB)"}
|
||||
|
||||
body = rfile.read(cl)
|
||||
|
||||
environ = {
|
||||
"REQUEST_METHOD": "POST",
|
||||
"CONTENT_TYPE": content_type,
|
||||
"CONTENT_LENGTH": str(content_length),
|
||||
}
|
||||
fs = cgi.FieldStorage(fp=io.BytesIO(body), environ=environ, keep_blank_values=True)
|
||||
|
||||
filename = None
|
||||
file_data = None
|
||||
contract_id = ""
|
||||
|
||||
if "files" in fs:
|
||||
item = fs["files"]
|
||||
if isinstance(item, list):
|
||||
item = item[0]
|
||||
filename = os.path.basename(item.filename) if item.filename else None
|
||||
if filename and (".." in filename or "/" in filename or "\\" in filename):
|
||||
return {"ok": False, "error": "invalid filename"}
|
||||
file_data = item.file.read() if hasattr(item, "file") else item.value
|
||||
if isinstance(file_data, str):
|
||||
file_data = file_data.encode("utf-8")
|
||||
|
||||
if "contract_id" in fs:
|
||||
contract_id = fs.getfirst("contract_id", "")
|
||||
# Validate UUID
|
||||
if contract_id:
|
||||
try:
|
||||
uuid.UUID(contract_id)
|
||||
except (ValueError, AttributeError):
|
||||
contract_id = ""
|
||||
|
||||
batch_id = fs.getfirst("batch_id", None) if "batch_id" in fs else None
|
||||
# Validate UUID
|
||||
if batch_id:
|
||||
try:
|
||||
uuid.UUID(batch_id)
|
||||
except (ValueError, AttributeError):
|
||||
batch_id = None
|
||||
|
||||
# zip_source — имя родительского ZIP-архива (для визуальной группировки)
|
||||
zip_source = None
|
||||
if "zip_source" in fs:
|
||||
raw_zip = fs.getfirst("zip_source", "")
|
||||
if raw_zip:
|
||||
zip_source = os.path.basename(raw_zip)[:255] # защита от path traversal + лимит
|
||||
|
||||
if not filename or not file_data:
|
||||
return {"ok": False, "error": "no file in request"}
|
||||
|
||||
mime = _mime_for(filename)
|
||||
b64 = base64.b64encode(file_data).decode()
|
||||
|
||||
if contract_id:
|
||||
try:
|
||||
supplements.delete_by_document(contract_id, filename)
|
||||
except Exception:
|
||||
pass # old record may not exist or FK issue — proceed with insert
|
||||
|
||||
doc = documents.insert(filename, mime, b64, batch_id=batch_id, zip_source=zip_source)
|
||||
|
||||
if not contract_id:
|
||||
from datetime import datetime
|
||||
now = datetime.now()
|
||||
c = contracts.insert(f"б/н {now.strftime('%Y%m%d')}-{now.strftime('%H%M')}")
|
||||
contract_id = c["id"]
|
||||
supp_type = "initial"
|
||||
else:
|
||||
supp_type = "additional"
|
||||
|
||||
supplements.insert(contract_id, doc["id"], supp_type)
|
||||
|
||||
parsed = parse_file(filename, file_data)
|
||||
if parsed and parsed.get("status") == "parsed":
|
||||
documents.set_parsed(doc["id"], parsed["elements"])
|
||||
elif parsed and parsed.get("status") == "error":
|
||||
documents.set_error(doc["id"], parsed.get("error", "parse failed"))
|
||||
|
||||
return {
|
||||
"ok": True,
|
||||
"doc_id": doc["id"],
|
||||
"contract_id": contract_id,
|
||||
"filename": filename,
|
||||
"size": len(file_data),
|
||||
"parsed": parsed,
|
||||
}
|
||||
|
||||
|
||||
def _mime_for(filename):
|
||||
ext = filename.rsplit(".", 1)[-1].lower() if "." in filename else ""
|
||||
mime_map = {
|
||||
"pdf": "application/pdf",
|
||||
"docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
||||
"doc": "application/msword",
|
||||
"zip": "application/zip",
|
||||
}
|
||||
return mime_map.get(ext, "application/octet-stream")
|
||||
Reference in New Issue
Block a user