feat: add content validation
- Add content validation to filter empty or invalid content - Add special handling for excalidraw clipboard content - Add minimum content length check - Add user feedback for invalid content - Improve error messages
This commit is contained in:
parent
0a5179209d
commit
bcd15f141f
@ -40,6 +40,33 @@ class TextCaptureService:
|
|||||||
)
|
)
|
||||||
self.logger = logging.getLogger(__name__)
|
self.logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
def is_valid_content(self, content):
|
||||||
|
"""验证内容是否有效"""
|
||||||
|
if not content:
|
||||||
|
return False
|
||||||
|
|
||||||
|
# 检查是否是空的JSON对象
|
||||||
|
if content.strip().startswith('{') and content.strip().endswith('}'):
|
||||||
|
try:
|
||||||
|
import json
|
||||||
|
data = json.loads(content)
|
||||||
|
# 检查是否是空的excalidraw内容
|
||||||
|
if data.get('type') == 'excalidraw/clipboard' and not data.get('elements'):
|
||||||
|
self.logger.info("跳过空的excalidraw内容")
|
||||||
|
return False
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
# 检查内容是否全是空白字符
|
||||||
|
if not content.strip():
|
||||||
|
return False
|
||||||
|
|
||||||
|
# 检查内容是否太短
|
||||||
|
if len(content.strip()) < 5:
|
||||||
|
return False
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
def get_clipboard_content(self):
|
def get_clipboard_content(self):
|
||||||
"""获取剪贴板内容"""
|
"""获取剪贴板内容"""
|
||||||
try:
|
try:
|
||||||
@ -50,7 +77,7 @@ class TextCaptureService:
|
|||||||
try:
|
try:
|
||||||
content = win32clipboard.GetClipboardData(win32con.CF_UNICODETEXT)
|
content = win32clipboard.GetClipboardData(win32con.CF_UNICODETEXT)
|
||||||
self.logger.info("获取Unicode文本格式内容")
|
self.logger.info("获取Unicode文本格式内容")
|
||||||
if content:
|
if content and self.is_valid_content(content):
|
||||||
return content
|
return content
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.logger.debug(f"获取Unicode文本失败: {e}")
|
self.logger.debug(f"获取Unicode文本失败: {e}")
|
||||||
@ -76,7 +103,7 @@ class TextCaptureService:
|
|||||||
html_content = html_content[start:end+7]
|
html_content = html_content[start:end+7]
|
||||||
|
|
||||||
content = h.handle(html_content).strip()
|
content = h.handle(html_content).strip()
|
||||||
if content:
|
if content and self.is_valid_content(content):
|
||||||
return content
|
return content
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.logger.debug(f"获取HTML格式失败: {e}")
|
self.logger.debug(f"获取HTML格式失败: {e}")
|
||||||
@ -86,18 +113,24 @@ class TextCaptureService:
|
|||||||
text_content = win32clipboard.GetClipboardData(win32con.CF_TEXT)
|
text_content = win32clipboard.GetClipboardData(win32con.CF_TEXT)
|
||||||
self.logger.info("获取普通文本格式内容")
|
self.logger.info("获取普通文本格式内容")
|
||||||
if text_content:
|
if text_content:
|
||||||
return text_content.decode('gbk')
|
content = text_content.decode('gbk')
|
||||||
|
if self.is_valid_content(content):
|
||||||
|
return content
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.logger.debug(f"获取普通文本失败: {e}")
|
self.logger.debug(f"获取普通文本失败: {e}")
|
||||||
|
|
||||||
if not content:
|
if not content or not self.is_valid_content(content):
|
||||||
self.logger.warning("剪贴板内容为空")
|
self.logger.warning("剪贴板内容为空或无效")
|
||||||
|
if hasattr(self, 'gui') and self.gui:
|
||||||
|
self.gui.show_message("剪贴板内容为空或无效", error=True)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
return content
|
return content
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.logger.error(f"获取剪贴板内容失败: {e}")
|
self.logger.error(f"获取剪贴板内容失败: {e}")
|
||||||
|
if hasattr(self, 'gui') and self.gui:
|
||||||
|
self.gui.show_message(f"获取剪贴板内容失败: {str(e)}", error=True)
|
||||||
return None
|
return None
|
||||||
finally:
|
finally:
|
||||||
try:
|
try:
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user