fix: Large document thumbnail display failed (#2763)

### What problem does this PR solve?

In MySQL, when the thumbnail base64 of a document is relatively large,
the display of the document's thumbnail fails.
Now, I put the document thumbnail into MiniIO storage.

### Type of change

- [✓] Bug Fix (non-breaking change which fixes an issue)

---------

Co-authored-by: chongchuanbing <chongchuanbing@gmail.com>
This commit is contained in:
chongchuanbing
2024-10-10 09:09:29 +08:00
committed by GitHub
parent f7a73c5149
commit 485bfd6c08
4 changed files with 29 additions and 12 deletions

View File

@ -25,6 +25,7 @@ from cachetools import LRUCache, cached
from ruamel.yaml import YAML
from api.db import FileType
from api.contants import IMG_BASE64_PREFIX
PROJECT_BASE = os.getenv("RAG_PROJECT_BASE") or os.getenv("RAG_DEPLOY_BASE")
RAG_BASE = os.getenv("RAG_BASE")
@ -168,23 +169,20 @@ def filename_type(filename):
return FileType.OTHER.value
def thumbnail(filename, blob):
def thumbnail_img(filename, blob):
filename = filename.lower()
if re.match(r".*\.pdf$", filename):
pdf = pdfplumber.open(BytesIO(blob))
buffered = BytesIO()
pdf.pages[0].to_image(resolution=32).annotated.save(buffered, format="png")
return "data:image/png;base64," + \
base64.b64encode(buffered.getvalue()).decode("utf-8")
return buffered.getvalue()
if re.match(r".*\.(jpg|jpeg|png|tif|gif|icon|ico|webp)$", filename):
image = Image.open(BytesIO(blob))
image.thumbnail((30, 30))
buffered = BytesIO()
image.save(buffered, format="png")
return "data:image/png;base64," + \
base64.b64encode(buffered.getvalue()).decode("utf-8")
return buffered.getvalue()
if re.match(r".*\.(ppt|pptx)$", filename):
import aspose.slides as slides
@ -194,11 +192,15 @@ def thumbnail(filename, blob):
buffered = BytesIO()
presentation.slides[0].get_thumbnail(0.03, 0.03).save(
buffered, drawing.imaging.ImageFormat.png)
return "data:image/png;base64," + \
base64.b64encode(buffered.getvalue()).decode("utf-8")
return buffered.getvalue()
except Exception as e:
pass
return None
def thumbnail(filename, blob):
img = thumbnail_img(filename, blob)
return IMG_BASE64_PREFIX + \
base64.b64encode(img).decode("utf-8")
def traversal_files(base):
for root, ds, fs in os.walk(base):