Skip to content

fix: PDF members inside a zip archive were silently skipped during split - #7448

Open
Jackkp0t wants to merge 1 commit into
1Panel-dev:v2from
Jackkp0t:fix/zip-pdf-split-empty
Open

Jackkp0t wants to merge 1 commit into
1Panel-dev:v2from
Jackkp0t:fix/zip-pdf-split-empty

Conversation

@Jackkp0t

Copy link
Copy Markdown

Fixes #7436

问题

上传只包含 PDF 的 zip 压缩包时,split 接口返回空数组、页面分段列表为空,且服务端没有任何错误日志;单独上传同一个 PDF 则一切正常。

根因

PdfSplitHandle.handle 通过 file.chunks() 读取文件内容,而 chunks() 是 Django UploadedFile 才有的方法。zip 内的 PDF 成员传入的是 zipfile.ZipExtFile,没有 chunks(),直接抛出 AttributeError;该异常随后被 ZipSplitHandle.handle 里的 except Exception: pass 静默吞掉,于是压缩包内的 PDF 全部被跳过,split 返回空列表且无任何日志。

修复

  1. apps/common/handle/impl/text/pdf_split_handle.py:改用调用方注入的 get_buffer 读取文件(其余 6 个 split handle 均已使用该方式,对 UploadedFile 和 ZipExtFile 都有效),写入临时文件的逻辑不变。
  2. apps/common/handle/impl/text/zip_split_handle.py:压缩包成员解析失败时用 maxkb_logger.warning 记录 member 名与异常,不再静默吞掉,避免同类问题再次完全不可见。

验证

  • 用真实 pypdf 生成含文本的 PDF,在离线环境直接驱动 ZipSplitHandle.handle 走完整分派路径(仅 stub Django 胶水与无关注入,PDF 解析、zip 解包、分派逻辑均为仓库真实代码):
场景 修复前(v2) 修复后
zip 内只有 PDF(issue 场景) [],无任何日志 正常解析出分段
zip 内 PDF + txt 只剩 txt,PDF 静默丢失 两个成员都解析
zip 内只有 txt 正常 正常(无回归)
直接上传 PDF(不经过 zip) 正常 正常(无回归)
  • ruff check 对两个改动文件无新增告警(get_file_name 处的存量 F841 在 v2 上同样存在,与本次改动无关,未改动)。

PdfSplitHandle.handle read the file with file.chunks(), which only exists
on Django UploadedFile. When a PDF arrives as a member of a zip archive it
is a zipfile.ZipExtFile, so the call raised AttributeError, which
ZipSplitHandle.handle swallowed without logging, and the split API returned
an empty result for zips containing only PDFs.

Read the buffer through the get_buffer abstraction that every other split
handle already uses (it works for both UploadedFile and ZipExtFile), and
log the member name and error instead of silently dropping it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] 知识库上传包含 PDF 的 zip 压缩包后分段结果为空

1 participant