fix(feishu): resolve lint errors for shadow and formatting
- Rename err variables to avoid shadowing in downloadExternalImage - Fix struct field alignment in TestExtractCardImageKeys Generated with Crush Assisted-by: GLM-5 via Crush <crush@charm.land>
This commit is contained in:
parent
efd67355e8
commit
c58c3fe044
2 changed files with 61 additions and 61 deletions
|
|
@ -293,88 +293,88 @@ func TestStripMentionPlaceholders(t *testing.T) {
|
||||||
|
|
||||||
func TestExtractCardImageKeys(t *testing.T) {
|
func TestExtractCardImageKeys(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
content string
|
content string
|
||||||
wantFeishuKeys []string
|
wantFeishuKeys []string
|
||||||
wantExternalURLs []string
|
wantExternalURLs []string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "empty content",
|
name: "empty content",
|
||||||
content: "",
|
content: "",
|
||||||
wantFeishuKeys: nil,
|
wantFeishuKeys: nil,
|
||||||
wantExternalURLs: nil,
|
wantExternalURLs: nil,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "invalid JSON",
|
name: "invalid JSON",
|
||||||
content: "not json",
|
content: "not json",
|
||||||
wantFeishuKeys: nil,
|
wantFeishuKeys: nil,
|
||||||
wantExternalURLs: nil,
|
wantExternalURLs: nil,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "card with no images",
|
name: "card with no images",
|
||||||
content: `{"schema":"2.0","body":{"elements":[{"tag":"markdown","content":"text"}]}}`,
|
content: `{"schema":"2.0","body":{"elements":[{"tag":"markdown","content":"text"}]}}`,
|
||||||
wantFeishuKeys: nil,
|
wantFeishuKeys: nil,
|
||||||
wantExternalURLs: nil,
|
wantExternalURLs: nil,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "single image with img_key",
|
name: "single image with img_key",
|
||||||
content: `{"elements":[{"tag":"img","img_key":"img_abc123"}]}`,
|
content: `{"elements":[{"tag":"img","img_key":"img_abc123"}]}`,
|
||||||
wantFeishuKeys: []string{"img_abc123"},
|
wantFeishuKeys: []string{"img_abc123"},
|
||||||
wantExternalURLs: nil,
|
wantExternalURLs: nil,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "single image with src as Feishu key",
|
name: "single image with src as Feishu key",
|
||||||
content: `{"elements":[{"tag":"img","src":"img_xyz789"}]}`,
|
content: `{"elements":[{"tag":"img","src":"img_xyz789"}]}`,
|
||||||
wantFeishuKeys: []string{"img_xyz789"},
|
wantFeishuKeys: []string{"img_xyz789"},
|
||||||
wantExternalURLs: nil,
|
wantExternalURLs: nil,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "multiple images",
|
name: "multiple images",
|
||||||
content: `{"elements":[{"tag":"img","img_key":"img_1"},{"tag":"div","text":{"content":"text"}},{"tag":"img","img_key":"img_2"}]}`,
|
content: `{"elements":[{"tag":"img","img_key":"img_1"},{"tag":"div","text":{"content":"text"}},{"tag":"img","img_key":"img_2"}]}`,
|
||||||
wantFeishuKeys: []string{"img_1", "img_2"},
|
wantFeishuKeys: []string{"img_1", "img_2"},
|
||||||
wantExternalURLs: nil,
|
wantExternalURLs: nil,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "nested image in columns",
|
name: "nested image in columns",
|
||||||
content: `{"elements":[{"tag":"div","columns":[{"tag":"img","img_key":"img_col1"},{"tag":"img","img_key":"img_col2"}]}]}`,
|
content: `{"elements":[{"tag":"div","columns":[{"tag":"img","img_key":"img_col1"},{"tag":"img","img_key":"img_col2"}]}]}`,
|
||||||
wantFeishuKeys: []string{"img_col1", "img_col2"},
|
wantFeishuKeys: []string{"img_col1", "img_col2"},
|
||||||
wantExternalURLs: nil,
|
wantExternalURLs: nil,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "image in action",
|
name: "image in action",
|
||||||
content: `{"elements":[{"tag":"action","actions":[{"tag":"img","img_key":"img_action"}]}]}`,
|
content: `{"elements":[{"tag":"action","actions":[{"tag":"img","img_key":"img_action"}]}]}`,
|
||||||
wantFeishuKeys: []string{"img_action"},
|
wantFeishuKeys: []string{"img_action"},
|
||||||
wantExternalURLs: nil,
|
wantExternalURLs: nil,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "icon element",
|
name: "icon element",
|
||||||
content: `{"elements":[{"tag":"icon","icon_key":"icon_123"}]}`,
|
content: `{"elements":[{"tag":"icon","icon_key":"icon_123"}]}`,
|
||||||
wantFeishuKeys: []string{"icon_123"},
|
wantFeishuKeys: []string{"icon_123"},
|
||||||
wantExternalURLs: nil,
|
wantExternalURLs: nil,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "complex card with text and images",
|
name: "complex card with text and images",
|
||||||
content: `{"header":{"title":{"content":"Title"}},"elements":[{"tag":"div","text":{"content":"Description"}},{"tag":"img","img_key":"img_main"}]}`,
|
content: `{"header":{"title":{"content":"Title"}},"elements":[{"tag":"div","text":{"content":"Description"}},{"tag":"img","img_key":"img_main"}]}`,
|
||||||
wantFeishuKeys: []string{"img_main"},
|
wantFeishuKeys: []string{"img_main"},
|
||||||
wantExternalURLs: nil,
|
wantExternalURLs: nil,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "external URL in src",
|
name: "external URL in src",
|
||||||
content: `{"elements":[{"tag":"img","src":"https://example.com/image.png"}]}`,
|
content: `{"elements":[{"tag":"img","src":"https://example.com/image.png"}]}`,
|
||||||
wantFeishuKeys: nil,
|
wantFeishuKeys: nil,
|
||||||
wantExternalURLs: []string{"https://example.com/image.png"},
|
wantExternalURLs: []string{"https://example.com/image.png"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "mixed Feishu keys and external URLs",
|
name: "mixed Feishu keys and external URLs",
|
||||||
content: `{"elements":[{"tag":"img","img_key":"img_feishu"},{"tag":"img","src":"https://cdn.example.com/external.jpg"},{"tag":"img","src":"img_another"}]}`,
|
content: `{"elements":[{"tag":"img","img_key":"img_feishu"},{"tag":"img","src":"https://cdn.example.com/external.jpg"},{"tag":"img","src":"img_another"}]}`,
|
||||||
wantFeishuKeys: []string{"img_feishu", "img_another"},
|
wantFeishuKeys: []string{"img_feishu", "img_another"},
|
||||||
wantExternalURLs: []string{"https://cdn.example.com/external.jpg"},
|
wantExternalURLs: []string{"https://cdn.example.com/external.jpg"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "multiple external URLs",
|
name: "multiple external URLs",
|
||||||
content: `{"elements":[{"tag":"img","src":"https://a.com/1.png"},{"tag":"img","src":"http://b.com/2.jpg"}]}`,
|
content: `{"elements":[{"tag":"img","src":"https://a.com/1.png"},{"tag":"img","src":"http://b.com/2.jpg"}]}`,
|
||||||
wantFeishuKeys: nil,
|
wantFeishuKeys: nil,
|
||||||
wantExternalURLs: []string{"https://a.com/1.png", "http://b.com/2.jpg"},
|
wantExternalURLs: []string{"https://a.com/1.png", "http://b.com/2.jpg"},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -753,25 +753,25 @@ func (c *FeishuChannel) downloadExternalImage(
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := io.Copy(out, resp.Body); err != nil {
|
if _, copyErr := io.Copy(out, resp.Body); copyErr != nil {
|
||||||
out.Close()
|
out.Close()
|
||||||
os.Remove(localPath)
|
os.Remove(localPath)
|
||||||
logger.ErrorCF("feishu", "Failed to write external image to file", map[string]any{
|
logger.ErrorCF("feishu", "Failed to write external image to file", map[string]any{
|
||||||
"error": err.Error(),
|
"error": copyErr.Error(),
|
||||||
})
|
})
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
out.Close()
|
out.Close()
|
||||||
|
|
||||||
// Store in MediaStore
|
// Store in MediaStore
|
||||||
ref, err := store.Store(localPath, media.MediaMeta{
|
ref, storeErr := store.Store(localPath, media.MediaMeta{
|
||||||
Filename: filename,
|
Filename: filename,
|
||||||
Source: "feishu_external",
|
Source: "feishu_external",
|
||||||
}, scope)
|
}, scope)
|
||||||
if err != nil {
|
if storeErr != nil {
|
||||||
logger.ErrorCF("feishu", "Failed to store external image", map[string]any{
|
logger.ErrorCF("feishu", "Failed to store external image", map[string]any{
|
||||||
"url": imageURL,
|
"url": imageURL,
|
||||||
"error": err.Error(),
|
"error": storeErr.Error(),
|
||||||
})
|
})
|
||||||
os.Remove(localPath)
|
os.Remove(localPath)
|
||||||
return ""
|
return ""
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue