Skip to content

Commit 4aeb08e

Browse files
authored
Merge pull request #15 from ainsleyclark/mime
Added mime type detection
2 parents acbed73 + 413e401 commit 4aeb08e

File tree

7 files changed

+108
-6
lines changed

7 files changed

+108
-6
lines changed

.github/workflows/email.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,9 @@ jobs:
6262
# Make file runnable, might not be necessary
6363
chmod +x "${GITHUB_WORKSPACE}/bin/tests.sh"
6464
# Run tests
65-
"${GITHUB_WORKSPACE}/bin/tests.sh"
65+
"${GITHUB_WORKSPACE}/bin/tests.sh mailgun"
66+
"${GITHUB_WORKSPACE}/bin/tests.sh postmark"
67+
"${GITHUB_WORKSPACE}/bin/tests.sh sendgrid"
68+
"${GITHUB_WORKSPACE}/bin/tests.sh smtp"
69+
"${GITHUB_WORKSPACE}/bin/tests.sh sparkpost"
6670

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ go 1.16
44

55
require (
66
github.com/davecgh/go-spew v1.1.1 // indirect
7-
github.com/gabriel-vasile/mimetype v1.2.0
87
github.com/joho/godotenv v1.4.0
98
github.com/stretchr/testify v1.7.0
109
)

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
22
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
33
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4-
github.com/gabriel-vasile/mimetype v1.2.0 h1:A6z5J8OhjiWFV91sQ3dMI8apYu/tvP9keDaMM3Xu6p4=
5-
github.com/gabriel-vasile/mimetype v1.2.0/go.mod h1:6CDPel/o/3/s4+bp6kIbsWATq8pmgOisOPG40CJa6To=
64
github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg=
75
github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
86
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=

internal/client/client.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import (
2525
"time"
2626
)
2727

28+
// Requester defines the method used for interacting with
29+
// a Mailable API.
2830
type Requester interface {
2931
// Do accepts a message, url endpoint and optional Headers to POST data
3032
// to a drivers API.

internal/mime/mime.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2020 The Go Mail Authors. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package mime
15+
16+
import (
17+
"bytes"
18+
"net/http"
19+
)
20+
21+
const (
22+
// sniffLength is the amount of bytes to read to
23+
// detect the MIME type.
24+
sniffLength uint32 = 512
25+
)
26+
27+
// DetectBuffer returns the MIME type found from the provided byte slice.
28+
//
29+
// The result is always a valid MIME type, with application/octet-stream
30+
// returned when identification failed.
31+
// Uses http.DetectContentType with a layer for SVG detection.
32+
func DetectBuffer(buf []byte) string {
33+
header := make([]byte, sniffLength)
34+
copy(header, buf)
35+
36+
// Detect for SVGs
37+
// See https://github.com/golang/go/issues/15888
38+
if bytes.Contains(header, []byte("<svg")) {
39+
return "image/svg+xml"
40+
}
41+
42+
return http.DetectContentType(header)
43+
}

internal/mime/mime_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2020 The Go Mail Authors. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package mime
15+
16+
import (
17+
"github.com/stretchr/testify/assert"
18+
"io/ioutil"
19+
"os"
20+
"path/filepath"
21+
"testing"
22+
)
23+
24+
func TestDetectBuffer(t *testing.T) {
25+
tt := map[string]struct {
26+
input string
27+
want string
28+
}{
29+
"PNG": {
30+
"gopher.png",
31+
"image/png",
32+
},
33+
"JPG": {
34+
"gopher.jpg",
35+
"image/jpeg",
36+
},
37+
"SVG": {
38+
"gopher.svg",
39+
"image/svg+xml",
40+
},
41+
}
42+
43+
for name, test := range tt {
44+
t.Run(name, func(t *testing.T) {
45+
wd, err := os.Getwd()
46+
assert.NoError(t, err)
47+
48+
path := filepath.Join(filepath.Join(wd, "../../testdata"), test.input)
49+
file, err := ioutil.ReadFile(path)
50+
assert.NoError(t, err)
51+
52+
got := DetectBuffer(file)
53+
assert.Equal(t, test.want, got)
54+
})
55+
}
56+
}

mail/attachments.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ package mail
1515

1616
import (
1717
"encoding/base64"
18-
"github.com/gabriel-vasile/mimetype"
18+
"github.com/ainsleyclark/go-mail/internal/mime"
1919
)
2020

2121
// Attachment defines an email attachment for Go Mail.
@@ -28,7 +28,7 @@ type Attachment struct {
2828

2929
// Mime returns the mime type of the byte data.
3030
func (a Attachment) Mime() string {
31-
return mimetype.Detect(a.Bytes).String()
31+
return mime.DetectBuffer(a.Bytes)
3232
}
3333

3434
// B64 returns the base 64 encoding of the attachment.

0 commit comments

Comments
 (0)