From 6f1c5c6249ef8de97b80914b3ccd34debb0c052c Mon Sep 17 00:00:00 2001 From: Abin Simon Date: Wed, 20 Dec 2023 00:41:07 +0530 Subject: [PATCH] Do not try to add empty attachments to eml export (#4880) The upstream library we are currently using errors out if we try to attach an empty file. This PR skips trying to attach empty files. --- #### Does this PR need a docs update or release note? - [ ] :white_check_mark: Yes, it's included - [ ] :clock1: Yes, but in a later PR - [x] :no_entry: No #### Type of change - [ ] :sunflower: Feature - [x] :bug: Bugfix - [ ] :world_map: Documentation - [ ] :robot: Supportability/Tests - [ ] :computer: CI/Deployment - [ ] :broom: Tech Debt/Cleanup #### Issue(s) * # #### Test Plan - [ ] :muscle: Manual - [ ] :zap: Unit test - [ ] :green_heart: E2E --- src/internal/converters/eml/eml.go | 10 +++++++++ src/internal/converters/eml/eml_test.go | 30 +++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/src/internal/converters/eml/eml.go b/src/internal/converters/eml/eml.go index 4cb03bce4..b26a2a1bb 100644 --- a/src/internal/converters/eml/eml.go +++ b/src/internal/converters/eml/eml.go @@ -157,6 +157,16 @@ func FromJSON(ctx context.Context, body []byte) (string, error) { return "", clues.WrapWC(ctx, err, "invalid content bytes") } + if len(bts) == 0 { + // TODO(meain): pass the data through after + // https://github.com/xhit/go-simple-mail/issues/96 + logger.Ctx(ctx). + With("attachment_id", ptr.Val(attachment.GetId())). + Info("empty attachment") + + continue + } + name := ptr.Val(attachment.GetName()) contentID, err := attachment.GetBackingStore().Get("contentId") diff --git a/src/internal/converters/eml/eml_test.go b/src/internal/converters/eml/eml_test.go index 53f705c3a..cdd010978 100644 --- a/src/internal/converters/eml/eml_test.go +++ b/src/internal/converters/eml/eml_test.go @@ -7,6 +7,7 @@ import ( "time" "github.com/jhillyerd/enmime" + kjson "github.com/microsoft/kiota-serialization-json-go" "github.com/microsoftgraph/msgraph-sdk-go/models" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -129,3 +130,32 @@ func (suite *EMLUnitSuite) TestConvert_messageble_to_eml() { assert.Equal(t, source, target) } + +func (suite *EMLUnitSuite) TestConvert_empty_attachment_no_err() { + t := suite.T() + + ctx, flush := tester.NewContext(t) + defer flush() + + body := []byte(testdata.EmailWithAttachments) + + msg, err := api.BytesToMessageable(body) + require.NoError(t, err, "creating message") + + attachments := msg.GetAttachments() + err = attachments[0].GetBackingStore().Set("contentBytes", []uint8{}) + require.NoError(t, err, "setting content bytes") + + writer := kjson.NewJsonSerializationWriter() + + defer writer.Close() + + err = writer.WriteObjectValue("", msg) + require.NoError(t, err, "serializing message") + + nbody, err := writer.GetSerializedContent() + require.NoError(t, err, "getting serialized content") + + _, err = FromJSON(ctx, nbody) + assert.NoError(t, err, "converting to eml") +}