feat(content): enhance ContentChunk structure with Provider and StorageKey fields
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"cs-agent/internal/pkg/enums"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"golang.org/x/net/html"
|
"golang.org/x/net/html"
|
||||||
@@ -14,9 +15,11 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type ContentChunk struct {
|
type ContentChunk struct {
|
||||||
Type ContentChunkType //
|
Type ContentChunkType //
|
||||||
Content string // text content or image url
|
Content string // text content or image url
|
||||||
AssetID string // image asset id
|
AssetID string // image asset id
|
||||||
|
Provider enums.AssetProvider // image provider
|
||||||
|
StorageKey string // image storage key
|
||||||
}
|
}
|
||||||
|
|
||||||
func SplitHTMLContentChunks(content string) ([]ContentChunk, error) {
|
func SplitHTMLContentChunks(content string) ([]ContentChunk, error) {
|
||||||
@@ -59,13 +62,17 @@ func SplitHTMLContentChunks(content string) ([]ContentChunk, error) {
|
|||||||
flushText()
|
flushText()
|
||||||
src := strings.TrimSpace(findContentChunkHTMLAttr(node, "src"))
|
src := strings.TrimSpace(findContentChunkHTMLAttr(node, "src"))
|
||||||
assetID := strings.TrimSpace(findContentChunkHTMLAttr(node, "data-asset-id"))
|
assetID := strings.TrimSpace(findContentChunkHTMLAttr(node, "data-asset-id"))
|
||||||
|
provider := enums.AssetProvider(strings.TrimSpace(findContentChunkHTMLAttr(node, "data-provider")))
|
||||||
|
storageKey := strings.TrimSpace(findContentChunkHTMLAttr(node, "data-storage-key"))
|
||||||
if src == "" && assetID == "" {
|
if src == "" && assetID == "" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
chunks = append(chunks, ContentChunk{
|
chunks = append(chunks, ContentChunk{
|
||||||
Type: ContentChunkTypeImage,
|
Type: ContentChunkTypeImage,
|
||||||
Content: src,
|
Content: src,
|
||||||
AssetID: assetID,
|
AssetID: assetID,
|
||||||
|
Provider: provider,
|
||||||
|
StorageKey: storageKey,
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"cs-agent/internal/pkg/enums"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
@@ -55,7 +56,12 @@ func TestSplitHTMLContentChunks(t *testing.T) {
|
|||||||
name: "image with asset metadata only",
|
name: "image with asset metadata only",
|
||||||
content: `<p><img data-asset-id="asset_1" data-provider="local" data-storage-key="images/a.png" alt="a"></p>`,
|
content: `<p><img data-asset-id="asset_1" data-provider="local" data-storage-key="images/a.png" alt="a"></p>`,
|
||||||
want: []ContentChunk{
|
want: []ContentChunk{
|
||||||
{Type: ContentChunkTypeImage, AssetID: "asset_1"},
|
{
|
||||||
|
Type: ContentChunkTypeImage,
|
||||||
|
AssetID: "asset_1",
|
||||||
|
Provider: enums.AssetProviderLocal,
|
||||||
|
StorageKey: "images/a.png",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import (
|
|||||||
"cs-agent/internal/repositories"
|
"cs-agent/internal/repositories"
|
||||||
"cs-agent/internal/wxwork"
|
"cs-agent/internal/wxwork"
|
||||||
|
|
||||||
|
"github.com/mlogclub/simple/common/strs"
|
||||||
"github.com/mlogclub/simple/sqls"
|
"github.com/mlogclub/simple/sqls"
|
||||||
"github.com/silenceper/wechat/v2/work/kf/sendmsg"
|
"github.com/silenceper/wechat/v2/work/kf/sendmsg"
|
||||||
)
|
)
|
||||||
@@ -445,27 +446,24 @@ func (s *wxWorkKFOutboundService) buildHTMLChunks(content string) ([]wxWorkKFOut
|
|||||||
for _, chunk := range contentChunks {
|
for _, chunk := range contentChunks {
|
||||||
switch chunk.Type {
|
switch chunk.Type {
|
||||||
case utils.ContentChunkTypeText:
|
case utils.ContentChunkTypeText:
|
||||||
text := strings.TrimSpace(chunk.Content)
|
if strs.IsNotBlank(chunk.Content) {
|
||||||
if text == "" {
|
chunks = append(chunks, wxWorkKFOutboundChunk{
|
||||||
continue
|
MessageType: enums.IMMessageTypeText,
|
||||||
|
Content: chunk.Content,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
chunks = append(chunks, wxWorkKFOutboundChunk{
|
|
||||||
MessageType: enums.IMMessageTypeText,
|
|
||||||
Content: text,
|
|
||||||
})
|
|
||||||
case utils.ContentChunkTypeImage:
|
case utils.ContentChunkTypeImage:
|
||||||
assetID := strings.TrimSpace(chunk.AssetID)
|
if strs.IsBlank(chunk.AssetID) {
|
||||||
if assetID == "" {
|
|
||||||
chunks = append(chunks, wxWorkKFOutboundChunk{
|
chunks = append(chunks, wxWorkKFOutboundChunk{
|
||||||
MessageType: enums.IMMessageTypeText,
|
MessageType: enums.IMMessageTypeText,
|
||||||
Content: "[图片]",
|
Content: "[图片]",
|
||||||
})
|
})
|
||||||
continue
|
} else {
|
||||||
|
chunks = append(chunks, wxWorkKFOutboundChunk{
|
||||||
|
MessageType: enums.IMMessageTypeImage,
|
||||||
|
AssetID: chunk.AssetID,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
chunks = append(chunks, wxWorkKFOutboundChunk{
|
|
||||||
MessageType: enums.IMMessageTypeImage,
|
|
||||||
AssetID: assetID,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(chunks) == 0 {
|
if len(chunks) == 0 {
|
||||||
|
|||||||
Reference in New Issue
Block a user