fix: Jeditor,BLock Editor,Content新建记录时,拖拽图片上传后,图片无法关联保存后的记录
This commit is contained in:
parent
069557eba6
commit
f03c25425a
@ -431,6 +431,50 @@ def relink_mismatched_files(pg: "Page") -> None:
|
||||
pg.delete_key("__temporary_name")
|
||||
|
||||
|
||||
def relink_editor_uploaded_files(pg: "Page") -> None:
|
||||
"""
|
||||
Relink pre-uploaded files in editor fields (Jeditor, BlockEditor, Text Editor, HTML Editor, Content)
|
||||
from the temporary name to the saved record's real name.
|
||||
|
||||
When images are pasted or dropped into an editor on a new record, the file is uploaded
|
||||
immediately via upload_file. At that point the record has a temporary name (new-xxx),
|
||||
so the File record's attached_to_name is set to that temporary name. After the record
|
||||
is saved with its final name, this function scans editor fields for /files/ URLs,
|
||||
finds matching File records attached to the temporary name, and relinks them.
|
||||
"""
|
||||
|
||||
temp_name = pg.get("__temporary_name", None)
|
||||
|
||||
if not temp_name:
|
||||
return
|
||||
|
||||
import re
|
||||
|
||||
for df in pg.meta.get("fields", {"fieldtype": ("in", ("Text Editor", "HTML Editor", "Jeditor", "Block Editor", "Content"))}):
|
||||
content = pg.get(df.fieldname)
|
||||
if not content or not isinstance(content, str):
|
||||
continue
|
||||
|
||||
for file_url in re.findall(r'(/[^\s"\'>]*files/[^\s"\'>]+)', content):
|
||||
file_url = file_url.rstrip('"\'')
|
||||
|
||||
mislinked = jingrow.db.get_value(
|
||||
"File",
|
||||
{
|
||||
"file_url": file_url,
|
||||
"attached_to_name": temp_name,
|
||||
"attached_to_pagetype": pg.pagetype,
|
||||
},
|
||||
as_dict=True,
|
||||
)
|
||||
if mislinked:
|
||||
jingrow.db.set_value(
|
||||
"File",
|
||||
mislinked.name,
|
||||
field={"attached_to_name": pg.name},
|
||||
)
|
||||
|
||||
|
||||
def decode_file_content(content: bytes) -> bytes:
|
||||
if isinstance(content, str):
|
||||
content = content.encode("utf-8")
|
||||
|
||||
@ -1302,12 +1302,18 @@ class BasePage:
|
||||
return cast_fieldtype(df.fieldtype, value, show_warning=False)
|
||||
|
||||
def _extract_images_from_editor(self):
|
||||
from jingrow.core.pagetype.file.utils import extract_images_from_pg
|
||||
from jingrow.core.pagetype.file.utils import (
|
||||
extract_images_from_pg,
|
||||
relink_editor_uploaded_files,
|
||||
)
|
||||
|
||||
if self.pagetype != "PageType":
|
||||
for df in self.meta.get("fields", {"fieldtype": ("in", ("Text Editor", "HTML Editor"))}):
|
||||
for df in self.meta.get("fields", {"fieldtype": ("in", ("Text Editor", "HTML Editor", "Jeditor", "Block Editor", "Content"))}):
|
||||
extract_images_from_pg(self, df.fieldname)
|
||||
|
||||
# Link pre-uploaded editor files (paste/drop before save) to the saved record
|
||||
relink_editor_uploaded_files(self)
|
||||
|
||||
|
||||
def _filter(data, filters, limit=None):
|
||||
"""pass filters as:
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user