ZIP: распаковка и парсинг каждого файла внутри, v1.3
This commit is contained in:
+65
-17
@@ -165,25 +165,73 @@ class ContractsApp:
|
|||||||
raw = doc["original_bytes"]
|
raw = doc["original_bytes"]
|
||||||
file_data = bytes(raw) if isinstance(raw, memoryview) else raw
|
file_data = bytes(raw) if isinstance(raw, memoryview) else raw
|
||||||
|
|
||||||
# Парсинг
|
# Парсинг: ZIP — распаковать и парсить каждый файл отдельно
|
||||||
pr = parser_mod.parse(file_data, s["mime_type"])
|
if s["mime_type"] == "application/zip":
|
||||||
elements = []
|
import io as io_mod, zipfile as zf_mod
|
||||||
if s["mime_type"] == "application/zip" and "files" in pr:
|
zip_names = []
|
||||||
for zf in pr["files"]:
|
try:
|
||||||
elements.extend(zf.get("elements", []))
|
with zf_mod.ZipFile(io_mod.BytesIO(file_data)) as zf:
|
||||||
|
for zname in zf.namelist():
|
||||||
|
if zname.endswith("/"):
|
||||||
|
continue
|
||||||
|
zip_names.append(zname)
|
||||||
|
zdata = zf.read(zname)
|
||||||
|
zmime = mimeutil.guess_mime(zname) or "application/octet-stream"
|
||||||
|
zbasename = zname.split("/")[-1] if "/" in zname else zname
|
||||||
|
|
||||||
|
# Сохранить как отдельный документ
|
||||||
|
db.execute(
|
||||||
|
"INSERT INTO documents (filename, mime_type, original_bytes, status) VALUES (%s,%s,%s,'uploaded')",
|
||||||
|
(zbasename, zmime, zdata),
|
||||||
|
)
|
||||||
|
zdoc, _ = db.query_one(
|
||||||
|
"SELECT id FROM documents WHERE filename=%s ORDER BY created_at DESC LIMIT 1",
|
||||||
|
(zbasename,),
|
||||||
|
)
|
||||||
|
zdoc_id = zdoc["id"] if zdoc else None
|
||||||
|
if zdoc_id:
|
||||||
|
db.execute(
|
||||||
|
"INSERT INTO supplements (contract_id, document_id, type) VALUES (%s,%s,'initial')",
|
||||||
|
(cid, str(zdoc_id)),
|
||||||
|
)
|
||||||
|
|
||||||
|
# Парсинг внутреннего файла
|
||||||
|
zf_start = time_mod.time()
|
||||||
|
total_bytes += len(zdata)
|
||||||
|
yield f"data: {json.dumps({'type': 'file_start', 'name': zbasename, 'bytes': len(zdata)})}\n\n"
|
||||||
|
|
||||||
|
try:
|
||||||
|
zpr = parser_mod.parse(zdata, zmime)
|
||||||
|
zelements = zpr.get("elements", [])
|
||||||
|
ztext = textify.to_text(zelements) if zelements else ""
|
||||||
|
db.execute(
|
||||||
|
"UPDATE documents SET parsed_text=%s, status='parsed' WHERE id=%s",
|
||||||
|
(ztext, str(zdoc_id)),
|
||||||
|
)
|
||||||
|
zelapsed = round(time_mod.time() - zf_start, 1)
|
||||||
|
files_processed += 1
|
||||||
|
yield f"data: {json.dumps({'type': 'file_done', 'name': zbasename, 'time_s': zelapsed, 'elements': len(zelements)})}\n\n"
|
||||||
|
except Exception as e:
|
||||||
|
yield f"data: {json.dumps({'type': 'file_error', 'name': zbasename, 'error': str(e)})}\n\n"
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
yield f"data: {json.dumps({'type': 'file_error', 'name': s['filename'], 'error': 'ZIP: ' + str(e)})}\n\n"
|
||||||
|
|
||||||
|
# ZIP сам — expanded
|
||||||
|
db.execute("UPDATE documents SET status='expanded' WHERE id=%s", (s["doc_id"],))
|
||||||
|
yield f"data: {json.dumps({'type': 'zip_expanded', 'name': s['filename'], 'count': len(zip_names)})}\n\n"
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
pr = parser_mod.parse(file_data, s["mime_type"])
|
||||||
elements = pr.get("elements", [])
|
elements = pr.get("elements", [])
|
||||||
|
text = textify.to_text(elements) if elements else ""
|
||||||
text = textify.to_text(elements) if elements else ""
|
db.execute(
|
||||||
db.execute(
|
"UPDATE documents SET parsed_text=%s, status='parsed' WHERE id=%s",
|
||||||
"UPDATE documents SET parsed_text=%s, status='parsed' WHERE id=%s",
|
(text, str(s["doc_id"])),
|
||||||
(text, str(s["doc_id"])),
|
)
|
||||||
)
|
elapsed = round(time_mod.time() - file_start, 1)
|
||||||
|
files_processed += 1
|
||||||
elapsed = round(time_mod.time() - file_start, 1)
|
yield f"data: {json.dumps({'type': 'file_done', 'name': s['filename'], 'time_s': elapsed, 'elements': len(elements)})}\n\n"
|
||||||
files_processed += 1
|
|
||||||
|
|
||||||
yield f"data: {json.dumps({'type': 'file_done', 'name': s['filename'], 'time_s': elapsed, 'elements': len(elements)})}\n\n"
|
|
||||||
|
|
||||||
total_time = round(time_mod.time() - start_time, 1)
|
total_time = round(time_mod.time() - start_time, 1)
|
||||||
yield f"data: {json.dumps({'type': 'summary', 'total_time_s': total_time, 'total_bytes': total_bytes, 'files_processed': files_processed})}\n\n"
|
yield f"data: {json.dumps({'type': 'summary', 'total_time_s': total_time, 'total_bytes': total_bytes, 'files_processed': files_processed})}\n\n"
|
||||||
|
|||||||
@@ -47,7 +47,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.2</span></span>
|
<span class="title">Сверка договоров <span style="font-weight:400;color:var(--muted);font-size:12px;">v1.3</span></span>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="content">
|
<div class="content">
|
||||||
@@ -229,6 +229,13 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
else if (d.type === 'zip_expanded') {
|
||||||
|
var row = findRowByName(d.name);
|
||||||
|
if (row) {
|
||||||
|
row.querySelector('.status-cell').innerHTML = '<span style="color:var(--muted);">↗ ' + d.count + ' файлов</span>';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
else if (d.type === 'summary') {
|
else if (d.type === 'summary') {
|
||||||
clearInterval(timerInterval);
|
clearInterval(timerInterval);
|
||||||
es.close();
|
es.close();
|
||||||
|
|||||||
Reference in New Issue
Block a user