From 007bd24374f4413630e4b0e344b71d57909cc59c Mon Sep 17 00:00:00 2001 From: jxxghp Date: Sun, 18 May 2025 15:25:45 +0800 Subject: [PATCH] fix message link check --- app/chain/message.py | 2 +- app/utils/string.py | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/app/chain/message.py b/app/chain/message.py index c4a99ef7..8efb0763 100644 --- a/app/chain/message.py +++ b/app/chain/message.py @@ -423,7 +423,7 @@ class MessageChain(ChainBase): # 聊天 content = text action = "Chat" - elif text.startswith("http"): + elif StringUtils.is_link(text): # 链接 content = text action = "Link" diff --git a/app/utils/string.py b/app/utils/string.py index 1bcc1faa..4adf193c 100644 --- a/app/utils/string.py +++ b/app/utils/string.py @@ -908,3 +908,20 @@ class StringUtils: :return: 如果elem有效(非None且长度大于0),返回True;否则返回False """ return elem is not None and len(elem) > 0 + + @staticmethod + def is_link(text: str) -> bool: + """ + 检查文件是否为链接地址,支持各类协议 + :param text: 要检查的文本 + :return: 如果URL有效,返回True;否则返回False + """ + if not text: + return False + # 检查是否以http、https、ftp等协议开头 + if re.match(r'^(http|https|ftp|ftps|sftp|ws|wss)://', text): + return True + # 检查是否为IP地址或域名 + if re.match(r'^[a-zA-Z0-9.-]+(\.[a-zA-Z]{2,})?$', text): + return True + return False