fix(fetch): 钉钉空文档显式报 empty_document
DingTalk exports empty online documents to valid but contentless docx/xlsx; WeKnora then rejects them with a generic remote_parse_failed. Detect blank exports (no text, no embedded media) at fetch time and report the cause in Chinese, and add the matching dead-letter hint.
This commit is contained in:
@@ -381,20 +381,29 @@ def _space_key(roots, name):
|
||||
|
||||
|
||||
def dingtalk_download(manifest, max_bytes):
|
||||
spreadsheet = pathlib.PurePosixPath(manifest["name"]).suffix.lower() == ".axls"
|
||||
|
||||
def guarded(local_path):
|
||||
if pathlib.PurePosixPath(str(local_path)).suffix.lower() in (".docx", ".xlsx") \
|
||||
and exported_doc_is_blank(local_path, spreadsheet):
|
||||
raise FetchError("钉钉文档正文为空,无需入库(如确有内容请在钉钉里补充后重新取件)",
|
||||
retryable=False, code="empty_document")
|
||||
return local_path
|
||||
|
||||
export = dingtalk_local_export(manifest)
|
||||
modified = str(manifest.get("modified_at") or "")
|
||||
if export:
|
||||
path, fresh = export
|
||||
size = os.path.getsize(path)
|
||||
if size > 0 and (fresh or not modified):
|
||||
return path, size, sha256_file(path), True
|
||||
return guarded(path), size, sha256_file(path), True
|
||||
# stale local export -> fall through to a live export when available
|
||||
raw_key, declared_type = dingtalk_raw_keys(manifest)
|
||||
headers = load_capture_headers()
|
||||
live_path = live_export_dingtalk_document(raw_key, headers, manifest, max_bytes)
|
||||
if export and not live_path:
|
||||
path, fresh = export
|
||||
return path, os.path.getsize(path), sha256_file(path), True # keep old bytes; staleness shown in UI
|
||||
return guarded(path), os.path.getsize(path), sha256_file(path), True # keep old bytes; staleness shown in UI
|
||||
if not live_path:
|
||||
raise MissingSource("DingTalk file is not exported and cannot be exported automatically")
|
||||
return live_path
|
||||
@@ -436,6 +445,30 @@ def dingtalk_put_bytes(url, payload):
|
||||
retryable=error.code == 429 or error.code >= 500, code="http_%d" % error.code)
|
||||
|
||||
|
||||
def exported_doc_is_blank(path, spreadsheet):
|
||||
"""DingTalk happily exports an empty online document to a valid but
|
||||
contentless docx/xlsx. The parser rejects those with a generic
|
||||
remote_parse_failed, so flag them here with an actionable message."""
|
||||
try:
|
||||
import zipfile
|
||||
with zipfile.ZipFile(path) as archive:
|
||||
names = archive.namelist()
|
||||
if any(name.startswith("word/media/") or name.startswith("xl/media/") for name in names):
|
||||
return False # image-only doc: the parser still has something to read
|
||||
parts = ["xl/sharedStrings.xml"] if spreadsheet else ["word/document.xml"]
|
||||
for part in parts:
|
||||
try:
|
||||
blob = archive.read(part)
|
||||
except KeyError:
|
||||
blob = b""
|
||||
text = re.sub(rb"<[^>]+>", b"", blob).strip()
|
||||
if text:
|
||||
return False
|
||||
return True
|
||||
except Exception:
|
||||
return False # unreadable zip: the magic/type checks handle that error
|
||||
|
||||
|
||||
def dingtalk_export_adoc(info, headers, manifest, max_bytes, spreadsheet=False):
|
||||
doc_key = str(info.get("docKey") or "")
|
||||
dentry_key = str(info.get("dentryKey") or "")
|
||||
@@ -479,7 +512,11 @@ def dingtalk_export_adoc(info, headers, manifest, max_bytes, spreadsheet=False):
|
||||
headers, extra_headers=extra)
|
||||
detail = status.get("data") or {}
|
||||
if detail.get("status") == "success" and detail.get("ossUrl"):
|
||||
return http_get(detail["ossUrl"], {}, 180, max_bytes) + (False,)
|
||||
result = http_get(detail["ossUrl"], {}, 180, max_bytes)
|
||||
if exported_doc_is_blank(result[0], spreadsheet):
|
||||
raise FetchError("钉钉文档正文为空,无需入库(如确有内容请在钉钉里补充后重新取件)",
|
||||
retryable=False, code="empty_document")
|
||||
return result + (False,)
|
||||
if detail.get("status") in ("fail", "failed", "error"):
|
||||
raise FetchError("DingTalk export job failed", retryable=False, code="export_failed")
|
||||
time.sleep(3)
|
||||
|
||||
Reference in New Issue
Block a user