base64 JSON загрузка вместо FormData/multipart — обход лимита Ingress, v1.21
This commit is contained in:
+42
@@ -78,6 +78,7 @@ class ContractsApp:
|
||||
self.app.add_url_rule("/", "index", self._index, methods=["GET", "POST"])
|
||||
self.app.add_url_rule("/parse/<cid>", "parse", self._parse, methods=["GET"])
|
||||
self.app.add_url_rule("/chunk", "chunk", self._chunk_upload, methods=["POST"])
|
||||
self.app.add_url_rule("/upload", "upload_json", self._upload_json, methods=["POST"])
|
||||
self.app.add_url_rule("/health", "health", self._health)
|
||||
self.app.register_blueprint(test_bp)
|
||||
self.app.register_blueprint(upload_bp)
|
||||
@@ -129,6 +130,47 @@ class ContractsApp:
|
||||
|
||||
return jsonify({"contract_id": str(contract_id)})
|
||||
|
||||
# ── Загрузка base64 (JSON) ──────────────────────────────────
|
||||
|
||||
def _upload_json(self):
|
||||
"""Принять файл как base64 в JSON. Обходит проблему FormData/multipart."""
|
||||
data = request.get_json(silent=True)
|
||||
if not data:
|
||||
return jsonify({"error": "Нет JSON"}), 400
|
||||
|
||||
filename = data.get("filename", "")
|
||||
b64 = data.get("data", "")
|
||||
if not filename or not b64:
|
||||
return jsonify({"error": "Нет filename или data"}), 400
|
||||
|
||||
import base64 as b64mod
|
||||
try:
|
||||
file_bytes = b64mod.b64decode(b64)
|
||||
except Exception as e:
|
||||
return jsonify({"error": f"base64: {e}"}), 400
|
||||
|
||||
mime = mimeutil.guess_mime(filename) or "application/octet-stream"
|
||||
cid = data.get("cid")
|
||||
|
||||
if cid:
|
||||
contract_id = cid
|
||||
else:
|
||||
conn, err = db.connect()
|
||||
if err:
|
||||
return jsonify({"error": f"БД: {err}"}), 500
|
||||
cur = conn.cursor()
|
||||
cur.execute(
|
||||
"INSERT INTO contracts (number, client) VALUES (%s, %s) RETURNING id",
|
||||
("б/н " + __import__("datetime").datetime.now().strftime("%Y%m%d-%H%M"), ""),
|
||||
)
|
||||
contract_id = cur.fetchone()[0]
|
||||
conn.commit()
|
||||
cur.close()
|
||||
db.put_conn(conn)
|
||||
|
||||
doc_id = _save_file_to_db(filename, file_bytes, mime, str(contract_id))
|
||||
return jsonify({"contract_id": str(contract_id), "doc_id": str(doc_id) if doc_id else None})
|
||||
|
||||
# ── Чанковая загрузка ──────────────────────────────────────
|
||||
|
||||
def _chunk_upload(self):
|
||||
|
||||
Reference in New Issue
Block a user