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.

<!-- PR description-->

---

#### Does this PR need a docs update or release note?

- [ ]  Yes, it's included
- [ ] 🕐 Yes, but in a later PR
- [x]  No

#### Type of change

<!--- Please check the type of change your PR introduces: --->
- [ ] 🌻 Feature
- [x] 🐛 Bugfix
- [ ] 🗺️ Documentation
- [ ] 🤖 Supportability/Tests
- [ ] 💻 CI/Deployment
- [ ] 🧹 Tech Debt/Cleanup

#### Issue(s)

<!-- Can reference multiple issues. Use one of the following "magic words" - "closes, fixes" to auto-close the Github issue. -->
* #<issue>

#### Test Plan

<!-- How will this be tested prior to merging.-->
- [ ] 💪 Manual
- [ ]  Unit test
- [ ] 💚 E2E
This commit is contained in:
Abin Simon 2023-12-20 00:41:07 +05:30 committed by GitHub
parent 90bed4c14e
commit 6f1c5c6249
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 0 deletions

View File

@ -157,6 +157,16 @@ func FromJSON(ctx context.Context, body []byte) (string, error) {
return "", clues.WrapWC(ctx, err, "invalid content bytes") 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()) name := ptr.Val(attachment.GetName())
contentID, err := attachment.GetBackingStore().Get("contentId") contentID, err := attachment.GetBackingStore().Get("contentId")

View File

@ -7,6 +7,7 @@ import (
"time" "time"
"github.com/jhillyerd/enmime" "github.com/jhillyerd/enmime"
kjson "github.com/microsoft/kiota-serialization-json-go"
"github.com/microsoftgraph/msgraph-sdk-go/models" "github.com/microsoftgraph/msgraph-sdk-go/models"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -129,3 +130,32 @@ func (suite *EMLUnitSuite) TestConvert_messageble_to_eml() {
assert.Equal(t, source, target) 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")
}