Skip to content

Commit

Permalink
fix issues:Image file not deleted when a doc is removed #9541 (#10465)
Browse files Browse the repository at this point in the history
Signed-off-by: root <[email protected]>
Co-authored-by: root <[email protected]>
  • Loading branch information
liuhaoran1212 and root authored Nov 11, 2024
1 parent 9550b88 commit 570f10d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
16 changes: 16 additions & 0 deletions api/core/tools/utils/web_reader_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,3 +356,19 @@ def content_digest(element):
digest.update(child.encode("utf-8"))
digest = digest.hexdigest()
return digest


def get_image_upload_file_ids(content):
pattern = r"!\[image\]\((http?://.*?(file-preview|image-preview))\)"
matches = re.findall(pattern, content)
image_upload_file_ids = []
for match in matches:
if match[1] == "file-preview":
content_pattern = r"files/([^/]+)/file-preview"
else:
content_pattern = r"files/([^/]+)/image-preview"
content_match = re.search(content_pattern, match[0])
if content_match:
image_upload_file_id = content_match.group(1)
image_upload_file_ids.append(image_upload_file_id)
return image_upload_file_ids
11 changes: 11 additions & 0 deletions api/tasks/clean_dataset_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from celery import shared_task

from core.rag.index_processor.index_processor_factory import IndexProcessorFactory
from core.tools.utils.web_reader_tool import get_image_upload_file_ids
from extensions.ext_database import db
from extensions.ext_storage import storage
from models.dataset import (
Expand Down Expand Up @@ -67,6 +68,16 @@ def clean_dataset_task(
db.session.delete(document)

for segment in segments:
image_upload_file_ids = get_image_upload_file_ids(segment.content)
for upload_file_id in image_upload_file_ids:
image_file = db.session.query(UploadFile).filter(UploadFile.id == upload_file_id).first()
try:
storage.delete(image_file.key)
except Exception:
logging.exception(
"Delete image_files failed when storage deleted, \
image_upload_file_is: {}".format(upload_file_id)
)
db.session.delete(segment)

db.session.query(DatasetProcessRule).filter(DatasetProcessRule.dataset_id == dataset_id).delete()
Expand Down
11 changes: 11 additions & 0 deletions api/tasks/clean_document_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from celery import shared_task

from core.rag.index_processor.index_processor_factory import IndexProcessorFactory
from core.tools.utils.web_reader_tool import get_image_upload_file_ids
from extensions.ext_database import db
from extensions.ext_storage import storage
from models.dataset import Dataset, DocumentSegment
Expand Down Expand Up @@ -40,6 +41,16 @@ def clean_document_task(document_id: str, dataset_id: str, doc_form: str, file_i
index_processor.clean(dataset, index_node_ids)

for segment in segments:
image_upload_file_ids = get_image_upload_file_ids(segment.content)
for upload_file_id in image_upload_file_ids:
image_file = db.session.query(UploadFile).filter(UploadFile.id == upload_file_id).first()
try:
storage.delete(image_file.key)
except Exception:
logging.exception(
"Delete image_files failed when storage deleted, \
image_upload_file_is: {}".format(upload_file_id)
)
db.session.delete(segment)

db.session.commit()
Expand Down

0 comments on commit 570f10d

Please sign in to comment.