From bd4c6dd627497403aa36b4c7400638045682e4f6 Mon Sep 17 00:00:00 2001 From: liuwei Date: Tue, 1 Apr 2025 17:16:13 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B6=88=E6=81=AF=E5=88=97=E8=A1=A8=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E6=98=BE=E7=A4=BA=E5=9B=BE=E7=89=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- admin/dashboard/server.py | 12 ++++++++++++ admin/dashboard/templates/message_list.html | 21 ++++++++++++++++++--- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/admin/dashboard/server.py b/admin/dashboard/server.py index e9f8a44..8c97674 100644 --- a/admin/dashboard/server.py +++ b/admin/dashboard/server.py @@ -87,6 +87,18 @@ class DashboardServer: @app.route('/static/') def serve_static(filename): return send_from_directory(static_folder, filename) + + # 配置静态文件访问 + # 获取项目根目录下的static/images目录 + project_root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) + images_dir = os.path.join(project_root, "static", "images") + + # 确保目录存在 + os.makedirs(images_dir, exist_ok=True) + + @app.route('/static/images/') + def serve_images(filename): + return send_from_directory(images_dir, filename) # 添加一个路由处理favicon请求 @app.route('/favicon.ico') diff --git a/admin/dashboard/templates/message_list.html b/admin/dashboard/templates/message_list.html index ba54263..a39b5de 100644 --- a/admin/dashboard/templates/message_list.html +++ b/admin/dashboard/templates/message_list.html @@ -341,8 +341,23 @@ // 如果路径为空,返回空字符串 if (!imagePath) return ''; - // 提取文件名 - const fileName = imagePath.split('\\').pop().split('/').pop(); + // 如果已经是完整URL,直接返回 + if (imagePath.startsWith('http://') || imagePath.startsWith('https://')) { + return imagePath; + } + + // 提取文件名和路径 + const pathParts = imagePath.split(/[\/\\]/); + const fileName = pathParts[pathParts.length - 1]; + + // 如果路径包含群ID(通常是倒数第二个部分) + if (pathParts.length >= 2) { + const groupId = pathParts[pathParts.length - 2]; + // 检查是否是群ID格式(通常以@chatroom结尾) + if (groupId.includes('@chatroom')) { + return `/static/images/${groupId}/${fileName}`; + } + } // 检查路径中是否包含static/images if (imagePath.includes('static/images') || imagePath.includes('static\\images')) { @@ -353,7 +368,7 @@ } } - // 如果不包含static/images,则直接使用文件名 + // 如果以上都不匹配,则直接使用文件名 return `/static/images/${fileName}`; } },