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("/", "index", self._index, methods=["GET", "POST"])
|
||||||
self.app.add_url_rule("/parse/<cid>", "parse", self._parse, methods=["GET"])
|
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("/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.add_url_rule("/health", "health", self._health)
|
||||||
self.app.register_blueprint(test_bp)
|
self.app.register_blueprint(test_bp)
|
||||||
self.app.register_blueprint(upload_bp)
|
self.app.register_blueprint(upload_bp)
|
||||||
@@ -129,6 +130,47 @@ class ContractsApp:
|
|||||||
|
|
||||||
return jsonify({"contract_id": str(contract_id)})
|
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):
|
def _chunk_upload(self):
|
||||||
|
|||||||
+36
-32
@@ -59,7 +59,7 @@
|
|||||||
<body>
|
<body>
|
||||||
<div class="topbar">
|
<div class="topbar">
|
||||||
<img src="{{ url_for('static', filename='nubes-logo.svg') }}" alt="Nubes">
|
<img src="{{ url_for('static', filename='nubes-logo.svg') }}" alt="Nubes">
|
||||||
<span class="title">Сверка договоров <span style="font-weight:400;color:var(--muted);font-size:12px;">v1.20</span></span>
|
<span class="title">Сверка договоров <span style="font-weight:400;color:var(--muted);font-size:12px;">v1.21</span></span>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="content">
|
<div class="content">
|
||||||
@@ -155,24 +155,36 @@
|
|||||||
|
|
||||||
window.removeFile = removeFile;
|
window.removeFile = removeFile;
|
||||||
|
|
||||||
// ── Чанковая загрузка (50KB) ────────────────────────────────
|
// ── Загрузка base64 (JSON) ──────────────────────────────────
|
||||||
|
|
||||||
async function uploadFileChunked(file, cid, onProgress) {
|
function readFileAsBase64(file, onProgress) {
|
||||||
var CHUNK_SIZE = 50 * 1024;
|
return new Promise(function(resolve, reject) {
|
||||||
var totalChunks = Math.ceil(file.size / CHUNK_SIZE);
|
var reader = new FileReader();
|
||||||
var uploadId = file.name + '_' + Date.now() + '_' + Math.random().toString(36).substr(2, 6);
|
reader.onprogress = function(e) {
|
||||||
var totalSent = 0;
|
if (e.lengthComputable && onProgress) onProgress(Math.round(e.loaded / e.total * 20));
|
||||||
var lastResp = null;
|
};
|
||||||
|
reader.onload = function() {
|
||||||
|
var b64 = reader.result.split(',')[1];
|
||||||
|
resolve(b64);
|
||||||
|
};
|
||||||
|
reader.onerror = function() { reject(new Error('Read error')); };
|
||||||
|
reader.readAsDataURL(file);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
for (var i = 0; i < totalChunks; i++) {
|
function uploadFileJSON(file, cid, onProgress) {
|
||||||
var start = i * CHUNK_SIZE;
|
return new Promise(function(resolve, reject) {
|
||||||
var end = Math.min(start + CHUNK_SIZE, file.size);
|
readFileAsBase64(file, onProgress).then(function(b64) {
|
||||||
var chunk = file.slice(start, end);
|
|
||||||
|
|
||||||
var resp = await new Promise(function(resolve, reject) {
|
|
||||||
var xhr = new XMLHttpRequest();
|
var xhr = new XMLHttpRequest();
|
||||||
xhr.open('POST', '/chunk');
|
xhr.open('POST', '/upload');
|
||||||
xhr.timeout = 60000;
|
xhr.setRequestHeader('Content-Type', 'application/json');
|
||||||
|
xhr.timeout = 120000;
|
||||||
|
|
||||||
|
xhr.upload.onprogress = function(e) {
|
||||||
|
if (e.lengthComputable && onProgress) {
|
||||||
|
onProgress(20 + Math.round(e.loaded / e.total * 80));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
xhr.onload = function() {
|
xhr.onload = function() {
|
||||||
if (xhr.status === 200) {
|
if (xhr.status === 200) {
|
||||||
@@ -188,21 +200,13 @@
|
|||||||
xhr.onerror = function() { reject(new Error('Сеть')); };
|
xhr.onerror = function() { reject(new Error('Сеть')); };
|
||||||
xhr.ontimeout = function() { reject(new Error('Таймаут')); };
|
xhr.ontimeout = function() { reject(new Error('Таймаут')); };
|
||||||
|
|
||||||
var fd = new FormData();
|
xhr.send(JSON.stringify({
|
||||||
fd.append('chunk', chunk);
|
filename: file.name,
|
||||||
fd.append('upload_id', uploadId);
|
data: b64,
|
||||||
fd.append('filename', file.name);
|
cid: cid || null
|
||||||
fd.append('chunk_index', i);
|
}));
|
||||||
fd.append('total_chunks', totalChunks);
|
}).catch(reject);
|
||||||
if (cid) fd.append('cid', cid);
|
});
|
||||||
xhr.send(fd);
|
|
||||||
});
|
|
||||||
|
|
||||||
if (resp && resp.contract_id) lastResp = resp;
|
|
||||||
totalSent += (end - start);
|
|
||||||
if (onProgress) onProgress(Math.round(totalSent / file.size * 100));
|
|
||||||
}
|
|
||||||
return lastResp;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ── Выбор файла → сразу загрузка ────────────────────────────
|
// ── Выбор файла → сразу загрузка ────────────────────────────
|
||||||
@@ -226,7 +230,7 @@
|
|||||||
var startTime = Date.now();
|
var startTime = Date.now();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
var resp = await uploadFileChunked(f, contractId, function(pct) {
|
var resp = await uploadFileJSON(f, contractId, function(pct) {
|
||||||
fileQueue[rowIdx].status = '<span style="color:var(--muted);">↑ ' + pct + '%</span>';
|
fileQueue[rowIdx].status = '<span style="color:var(--muted);">↑ ' + pct + '%</span>';
|
||||||
renderTable();
|
renderTable();
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user