Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
fix(lark): ingest standalone file and audio messages (MUL-6198) (#6984)
xiangboit4946f468526c
diff --git a/server/internal/integrations/lark/feishu_channel_test.go b/server/internal/integrations/lark/feishu_channel_test.goindex 6f075d7a95..e1996af528 100644--- a/server/internal/integrations/lark/feishu_channel_test.go+++ b/server/internal/integrations/lark/feishu_channel_test.go@@ -257,6 +257,8 @@ func TestFeishuMediaResolver_HasMedia(t *testing.T) {{"text", InboundMessage{MessageID: "om_t", MessageType: "text", Body: "hello", Content: `{"text":"hello"}`}, false},{"image", InboundMessage{MessageID: "om_i", MessageType: "image", Body: "[Image]", Content: `{"image_key":"img_k"}`}, true},{"video", InboundMessage{MessageID: "om_v", MessageType: "media", Body: "[Video]", Content: `{"file_key":"file_k"}`}, true},+ {"file", InboundMessage{MessageID: "om_f", MessageType: "file", Body: "[File]", Content: `{"file_key":"file_k","file_name":"spec.html"}`}, true},+ {"audio", InboundMessage{MessageID: "om_a", MessageType: "audio", Body: "[Audio]", Content: `{"file_key":"audio_k"}`}, true},{"post with image", InboundMessage{MessageID: "om_p", MessageType: "post",Content: `{"content":[[{"tag":"img","image_key":"img_post"}]]}`}, true},{"post text only", InboundMessage{MessageID: "om_pt", MessageType: "post",@@ -272,6 +274,112 @@ func TestFeishuMediaResolver_HasMedia(t *testing.T) {}}+func TestFeishuMediaResolver_AttachesStandaloneFileAndAudioMediaRefs(t *testing.T) {+ tests := []struct {+ name string+ messageType string+ body string+ content string+ contentType string+ downloadFilename string+ wantFilename string+ wantContentType string+ wantType channel.MsgType+ wantKey string+ }{+ {+ name: "html file",+ messageType: "file",+ body: "[File]",+ content: `{"file_key":"file_standalone","file_name":"integration.html"}`,+ contentType: "text/html",+ downloadFilename: "integration.html",+ wantFilename: "integration.html",+ wantContentType: "text/html",+ wantType: channel.MsgTypeFile,+ wantKey: "file_standalone",+ },+ {+ name: "zip file",+ messageType: "file",+ body: "[File]",+ content: `{"file_key":"zip_standalone","file_name":"integration.zip"}`,+ contentType: "application/zip",+ downloadFilename: "integration.zip",+ wantFilename: "integration.zip",+ wantContentType: "application/zip",+ wantType: channel.MsgTypeFile,+ wantKey: "zip_standalone",+ },+ {+ name: "audio without response filename",+ messageType: "audio",+ body: "[Audio]",+ content: `{"file_key":"audio_standalone"}`,+ contentType: "audio/opus",+ wantFilename: "feishu-audio-om_audio_without_response_filename.opus",+ wantContentType: "audio/opus",+ wantType: channel.MsgTypeAudio,+ wantKey: "audio_standalone",+ },+ {+ name: "audio with extensionless response filename",+ messageType: "audio",+ body: "[Audio]",+ content: `{"file_key":"audio_extensionless"}`,+ contentType: "audio/octet-stream",+ downloadFilename: "3CET6CGRY9",+ wantFilename: "3CET6CGRY9.opus",+ wantContentType: "audio/opus",+ wantType: channel.MsgTypeAudio,+ wantKey: "audio_extensionless",+ },+ }++ for _, tc := range tests {+ t.Run(tc.name, func(t *testing.T) {+ sender := &fakeSender{downloaded: DownloadedResource{+ Data: []byte("payload"),+ ContentType: tc.contentType,+ Filename: tc.downloadFilename,+ SizeBytes: 7,+ }}+ storage := &fakeMediaStorage{}+ resolver := NewFeishuMediaResolver(sender, fakeCreds{secret: "plain"}, storage, &fakeMediaLedger{}, newDiscardLogger())+ lm := InboundMessage{+ MessageID: "om_" + tc.name,+ MessageType: tc.messageType,+ Body: tc.body,+ Content: tc.content,+ }++ got := resolver.ResolveMedia(context.Background(), testMediaInstallation(t), engine.ResolvedIdentity{},+ uuidFromString(t, "22222222-2222-2222-2222-222222222222"), uuidFromString(t, "33333333-3333-4333-8333-333333333333"), channelMessageFromLark(lm))++ if len(sender.downloadCalls) != 1 {+ t.Fatalf("download calls = %d, want 1", len(sender.downloadCalls))+ }+ call := sender.downloadCalls[0]+ if call.MessageID != lm.MessageID || call.FileKey != tc.wantKey || call.Type != "file" {+ t.Fatalf("download params wrong: %+v", call)+ }+ if len(storage.uploads) != 1 {+ t.Fatalf("uploads = %d, want 1", len(storage.uploads))+ }+ if len(got.MediaRefs) != 1 {+ t.Fatalf("media refs = %+v, want 1", got.MediaRefs)+ }+ ref := got.MediaRefs[0]+ if ref.Type != tc.wantType || ref.Filename != tc.wantFilename || ref.MimeType != tc.wantContentType || ref.SizeBytes != 7 {+ t.Fatalf("media ref wrong: %+v", ref)+ }+ if upload := storage.uploads[0]; upload.filename != tc.wantFilename || upload.contentType != tc.wantContentType {+ t.Fatalf("upload metadata wrong: %+v", upload)+ }+ })+ }+}+// TestFeishuMediaResolver_RecordsIntentBeforeUpload pins the ledger ordering:// the pending row is durable BEFORE the PUT, carries the URL the attachment// will hold, and identifies the message the reconciler checks against.diff --git a/server/internal/integrations/lark/media_filename_test.go b/server/internal/integrations/lark/media_filename_test.goindex e1c510348f..6ff2b291e5 100644--- a/server/internal/integrations/lark/media_filename_test.go+++ b/server/internal/integrations/lark/media_filename_test.go@@ -40,6 +40,19 @@ func TestMediaExtensionKnowsPDF(t *testing.T) {}}+func TestMediaExtensionKnowsCommonAudioTypes(t *testing.T) {+ for contentType, want := range map[string]string{+ "audio/opus": ".opus",+ "audio/ogg": ".ogg",+ "audio/amr": ".amr",+ "audio/mpeg": ".mp3",+ } {+ if got := mediaExtension(contentType); got != want {+ t.Errorf("mediaExtension(%q) = %q, want %q", contentType, got, want)+ }+ }+}+// Every resource on one message shares a MessageID, so without the index// three photos in one send produce three objects with the same name.func TestGeneratedMediaFilenamesAreDistinctWithinAMessage(t *testing.T) {@@ -64,3 +77,12 @@ func TestProvidedFilenameStillWins(t *testing.T) {t.Errorf("mediaFilename = %q, want the platform's own name", got)}}++func TestProvidedAudioFilenameKeepsExistingExtension(t *testing.T) {+ lm := InboundMessage{MessageID: "om_audio"}+ res := larkMediaResource{kind: channel.MsgTypeAudio}+ downloaded := DownloadedResourceStream{Filename: "voice.ogg"}+ if got := mediaFilename(lm, res, downloaded, "audio/opus", 0); got != "voice.ogg" {+ t.Errorf("mediaFilename = %q, want the platform's existing extension preserved", got)+ }+}diff --git a/server/internal/integrations/lark/media_ingest.go b/server/internal/integrations/lark/media_ingest.goindex 5c0b0519aa..318bc9f547 100644--- a/server/internal/integrations/lark/media_ingest.go+++ b/server/internal/integrations/lark/media_ingest.go@@ -51,8 +51,9 @@ func NewFeishuMediaResolver(api APIClient, creds CredentialsResolver, storage me}// HasMedia reports whether the message carries downloadable Feishu resources-// (standalone image/video or post-embedded img/media spans). Pure in-memory-// decode of the already-received payload — it runs on the connector ACK path.+// (standalone image/video/file/audio or post-embedded img/media spans). Pure+// in-memory decode of the already-received payload — it runs on the connector+// ACK path.func (r *feishuMediaResolver) HasMedia(msg channel.InboundMessage) bool {lm, err := larkMsgFromRaw(msg)if err != nil {@@ -118,13 +119,7 @@ func (r *feishuMediaResolver) ResolveMedia(ctx context.Context, inst engine.Resor.logMediaWarn("lark media download failed", lm, err)continue}- contentType := got.ContentType- if contentType == "" {- contentType = res.mimeType- }- if contentType == "" {- contentType = "application/octet-stream"- }+ contentType := mediaContentType(res, got)filename := mediaFilename(lm, res, got, contentType, resIndex)uploadedBytes, err := r.uploadResource(ctx, key, got.Body, got.SizeBytes, contentType, filename)if err != nil {@@ -285,6 +280,23 @@ func mediaResourcesFromMessage(lm InboundMessage) []larkMediaResource {sizeBytes: sizeBytes,messageID: lm.MessageID,}}+ case "file", "audio":+ if payload.FileKey == "" {+ return nil+ }+ kind := channel.MsgTypeFile+ if lm.MessageType == "audio" {+ kind = channel.MsgTypeAudio+ }+ return []larkMediaResource{{+ key: payload.FileKey,+ kind: kind,+ fetchType: "file",+ filename: filename,+ mimeType: mimeType,+ sizeBytes: sizeBytes,+ messageID: lm.MessageID,+ }}default:return nil}@@ -348,7 +360,7 @@ func mediaResourcesFromPost(lm InboundMessage) []larkMediaResource {func mediaFilename(lm InboundMessage, res larkMediaResource, got DownloadedResourceStream, contentType string, index int) string {for _, candidate := range []string{got.Filename, res.filename} {if name := cleanFilename(candidate); name != "" {- return name+ return ensureAudioFilenameExtension(name, res.kind, contentType)}}prefix := "feishu-file"@@ -357,6 +369,8 @@ func mediaFilename(lm InboundMessage, res larkMediaResource, got DownloadedResouprefix = "feishu-image"case channel.MsgTypeVideo:prefix = "feishu-video"+ case channel.MsgTypeAudio:+ prefix = "feishu-audio"}name := prefix + "-" + safePathSegment(lm.MessageID)if index > 0 {@@ -365,6 +379,45 @@ func mediaFilename(lm InboundMessage, res larkMediaResource, got DownloadedResoureturn name + mediaExtension(contentType)}+func mediaContentType(res larkMediaResource, got DownloadedResourceStream) string {+ contentType := strings.TrimSpace(got.ContentType)+ if res.kind == channel.MsgTypeAudio && isGenericBinaryContentType(contentType) {+ if hinted := strings.TrimSpace(res.mimeType); !isGenericBinaryContentType(hinted) {+ return hinted+ }+ // Feishu audio messages are Opus. Its resource endpoint can return an+ // extensionless Content-Disposition filename together with the generic+ // audio/octet-stream type, so preserve the protocol-level format here.+ return "audio/opus"+ }+ if contentType == "" {+ contentType = strings.TrimSpace(res.mimeType)+ }+ if contentType == "" {+ return "application/octet-stream"+ }+ return contentType+}++func isGenericBinaryContentType(contentType string) bool {+ if semi := strings.IndexByte(contentType, ';'); semi >= 0 {+ contentType = contentType[:semi]+ }+ switch strings.ToLower(strings.TrimSpace(contentType)) {+ case "", "application/octet-stream", "audio/octet-stream":+ return true+ default:+ return false+ }+}++func ensureAudioFilenameExtension(name string, kind channel.MsgType, contentType string) string {+ if kind != channel.MsgTypeAudio || path.Ext(name) != "" {+ return name+ }+ return name + mediaExtension(contentType)+}+func cleanFilename(name string) string {name = strings.TrimSpace(name)if name == "" {@@ -396,6 +449,14 @@ func mediaExtension(contentType string) string {return ".webp"case "video/mp4":return ".mp4"+ case "audio/opus":+ return ".opus"+ case "audio/ogg":+ return ".ogg"+ case "audio/amr":+ return ".amr"+ case "audio/mpeg":+ return ".mp3"case "application/pdf":// mime.ExtensionsByType returns ExtensionsByType(".pdf") on most// systems, but it reads /etc/mime.types — which a slim container