聊天增加图片功能

This commit is contained in:
yml2213
2026-05-29 12:56:25 +08:00
parent 3b8b65ab56
commit 0f44b024cb
11 changed files with 590 additions and 28 deletions
+41 -1
View File
@@ -2,6 +2,7 @@ package chat
import (
"errors"
"net/url"
"strings"
)
@@ -63,15 +64,54 @@ func (s *Service) SendMessage(principal Principal, conversationID uint64, req Se
return nil, ErrDependencyUnavailable
}
req.Content = strings.TrimSpace(req.Content)
if conversationID == 0 || req.Content == "" {
attachments, ok := normalizeAttachmentURLS(req.AttachmentURLS)
req.AttachmentURLS = attachments
if conversationID == 0 || (req.Content == "" && len(req.AttachmentURLS) == 0) {
return nil, ErrInvalidMessage
}
if len([]rune(req.Content)) > 1000 {
return nil, ErrInvalidMessage
}
if !ok {
return nil, ErrInvalidMessage
}
return s.repo.SendMessage(principal, conversationID, req)
}
func normalizeAttachmentURLS(items []string) ([]string, bool) {
if len(items) > 9 {
return nil, false
}
result := make([]string, 0, len(items))
for _, item := range items {
value := strings.TrimSpace(item)
if value == "" {
continue
}
if len(value) > 500 || strings.Contains(value, "..") {
return nil, false
}
parsed, err := url.Parse(value)
if err != nil {
return nil, false
}
if parsed.Host != "" {
return nil, false
}
switch parsed.Path {
case "/api/files/object", "/api/admin/files/object", "/api/public/files/object":
default:
return nil, false
}
key := strings.TrimSpace(parsed.Query().Get("key"))
if key == "" || strings.Contains(key, "..") {
return nil, false
}
result = append(result, value)
}
return result, true
}
func (s *Service) MarkRead(principal Principal, conversationID uint64) error {
if s.repo == nil {
return ErrDependencyUnavailable