Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions apps/common/handle/impl/pdf_split_handle.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ def check_links_in_pdf(doc):

class PdfSplitHandle(BaseSplitHandle):
def handle(self, file, pattern_list: List, with_filter: bool, limit: int, get_buffer, save_image):
# Prefer get_buffer so ZipExtFile members (no .chunks()) work the same as UploadedFile.
buffer = get_buffer(file)
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
# 将上传的文件保存到临时文件中
for chunk in file.chunks():
temp_file.write(chunk)
temp_file.write(buffer)
# 获取临时文件的路径
temp_file_path = temp_file.name

Expand Down
55 changes: 55 additions & 0 deletions apps/common/handle/impl/tests/test_pdf_zip_buffer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""Regression for MaxKB #7436: PDF members inside zip have no .chunks()."""

from io import BytesIO


class _ZipLikePdf:
"""Minimal stand-in for zipfile.ZipExtFile (read-only, no chunks)."""

def __init__(self, name: str, data: bytes):
self.name = name
self._buf = BytesIO(data)

def read(self, *args, **kwargs):
return self._buf.read(*args, **kwargs)


def test_pdf_handle_reads_via_get_buffer_without_chunks(monkeypatch, tmp_path):
import types
import sys

# Avoid importing the real fitz-heavy module graph; exercise the buffer write path.
from common.handle.impl import pdf_split_handle as mod

calls = {}

class FakeDoc:
def close(self):
pass

def fake_open(path):
calls["path"] = path
with open(path, "rb") as f:
calls["bytes"] = f.read()
return FakeDoc()

monkeypatch.setattr(mod.fitz, "open", fake_open)
monkeypatch.setattr(
mod.PdfSplitHandle,
"handle_toc",
staticmethod(lambda *a, **k: [{"title": "t", "content": "c"}]),
)

payload = b"%PDF-1.4 fake"
file_obj = _ZipLikePdf("doc.pdf", payload)

def get_buffer(f):
return f.read()

result = mod.PdfSplitHandle().handle(
file_obj, None, False, 500, get_buffer, lambda images: None
)

assert result["name"] == "doc.pdf"
assert calls["bytes"] == payload
assert not hasattr(file_obj, "chunks")
7 changes: 5 additions & 2 deletions apps/common/handle/impl/zip_split_handle.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
@desc:
"""
import io
import logging
import os
import re
import uuid
Expand All @@ -29,6 +30,8 @@
from dataset.models import Image
from django.utils.translation import gettext_lazy as _

max_kb = logging.getLogger("max_kb")


class FileBufferHandle:
buffer = None
Expand Down Expand Up @@ -138,8 +141,8 @@ def handle(self, file, pattern_list: List, with_filter: bool, limit: int, get_bu
result = [*result, *value]
else:
result.append(value)
except Exception:
pass
except Exception as e:
max_kb.warning(f"Zip member: {file}, split error: {e}")
image_list = get_image_list(result, files)
result = filter_image_file(result, image_list)
image_mode_list = []
Expand Down
Loading