Skip to content

Commit aadb459

Browse files
Binxliuruibin
authored andcommitted
fix: parse pdf file null char not allowed.
1 parent 7ea1c71 commit aadb459

2 files changed

Lines changed: 16 additions & 8 deletions

File tree

apps/common/handle/impl/text/pdf_split_handle.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ def collect_toc(doc, outline, level, toc):
228228
title = item.get("/Title")
229229
if title is None:
230230
title = str(item)
231-
toc.append((level, str(title), page_number))
231+
toc.append((level, str(title).replace("\0", ""), page_number))
232232

233233
@staticmethod
234234
def handle_toc(doc, limit):
@@ -522,7 +522,7 @@ def visitor_text(text, cm, tm, font_dict, font_size):
522522
except BaseException:
523523
return ""
524524

525-
return "".join(text_parts).strip().split("\n")[0].replace(".", "").strip()
525+
return "".join(text_parts).replace("\0", "").strip().split("\n")[0].replace(".", "").strip()
526526

527527
@staticmethod
528528
def extract_first_line(page):
@@ -531,6 +531,7 @@ def extract_first_line(page):
531531

532532
@staticmethod
533533
def handle_chapter_title(title):
534+
title = title.replace("\0", "")
534535
title = re.sub(r"[一二三四五六七八九十\s*]、\s*", "", title)
535536
title = re.sub(r"第[一二三四五六七八九十]章\s*", "", title)
536537
return title

apps/knowledge/serializers/paragraph.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@
4848
from knowledge.task.generate import generate_related_by_paragraph_id_list
4949

5050

51+
class NullCharacterStrippedCharField(serializers.CharField):
52+
def to_internal_value(self, data):
53+
if isinstance(data, str):
54+
data = data.replace("\x00", "")
55+
return super().to_internal_value(data)
56+
57+
5158
class ParagraphSerializer(serializers.ModelSerializer):
5259
class Meta:
5360
model = Paragraph
@@ -59,21 +66,21 @@ class ParagraphInstanceSerializer(serializers.Serializer):
5966
段落实例对象
6067
"""
6168

62-
content = serializers.CharField(
69+
content = NullCharacterStrippedCharField(
6370
required=True, label=_("content"), max_length=102400, min_length=1, allow_null=True, allow_blank=True
6471
)
65-
title = serializers.CharField(
72+
title = NullCharacterStrippedCharField(
6673
required=False, max_length=256, label=_("section title"), allow_null=True, allow_blank=True
6774
)
6875
problem_list = ProblemInstanceSerializer(required=False, many=True)
6976
is_active = serializers.BooleanField(required=False, label=_("Is active"))
7077

7178

7279
class EditParagraphSerializers(serializers.Serializer):
73-
title = serializers.CharField(
80+
title = NullCharacterStrippedCharField(
7481
required=False, max_length=256, label=_("section title"), allow_null=True, allow_blank=True
7582
)
76-
content = serializers.CharField(
83+
content = NullCharacterStrippedCharField(
7784
required=False, max_length=102400, allow_null=True, allow_blank=True, label=_("section title")
7885
)
7986
problem_list = ProblemInstanceSerializer(required=False, many=True)
@@ -91,10 +98,10 @@ class ParagraphBatchGenerateRelatedSerializer(serializers.Serializer):
9198

9299

93100
class ParagraphSerializers(serializers.Serializer):
94-
title = serializers.CharField(
101+
title = NullCharacterStrippedCharField(
95102
required=False, max_length=256, label=_("section title"), allow_null=True, allow_blank=True
96103
)
97-
content = serializers.CharField(required=True, max_length=102400, label=_("section title"))
104+
content = NullCharacterStrippedCharField(required=True, max_length=102400, label=_("section title"))
98105

99106
class Problem(serializers.Serializer):
100107
workspace_id = serializers.CharField(required=True, label=_("workspace id"))

0 commit comments

Comments
 (0)