Skip to content

Commit 974d238

Browse files
committed
feat: hash image name
1 parent 178acde commit 974d238

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible
99
github.com/vanng822/go-premailer v1.23.0
1010
goauthentik.io/api/v3 v3.2024123.4
11+
golang.org/x/crypto v0.33.0
1112
golang.org/x/oauth2 v0.26.0
1213
gopkg.in/yaml.v3 v3.0.1
1314
)
@@ -19,6 +20,6 @@ require (
1920
github.com/gorilla/css v1.0.1 // indirect
2021
github.com/gorilla/securecookie v1.1.2 // indirect
2122
github.com/vanng822/css v1.0.1 // indirect
22-
golang.org/x/crypto v0.33.0 // indirect
2323
golang.org/x/net v0.34.0 // indirect
24+
golang.org/x/sys v0.30.0 // indirect
2425
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,8 @@ golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
303303
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
304304
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
305305
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
306+
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
307+
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
306308
golang.org/x/telemetry v0.0.0-20240228155512-f48c80bd79b2/go.mod h1:TeRTkGYfJXctD9OcfyVLyj2J3IxLnKwHJR8f4D8a3YE=
307309
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
308310
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=

internal/email/template.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"fmt"
66
"github.com/jordan-wright/email"
7+
"golang.org/x/crypto/sha3"
78
"html/template"
89
"net/textproto"
910
"os"
@@ -68,6 +69,10 @@ func (tm *TemplateManager) RenderTemplate(name string, data interface{}) (string
6869
if err != nil {
6970
return template.HTML(fmt.Sprintf("Failed to read image: %s", imageSrc))
7071
}
72+
// Inline images must have unique names
73+
// 메일 읽지도 않았는데 탈락 이미지 이름이 미리보기에 나오면 슬프지 않을까요?
74+
ext := filepath.Ext(imageSrc)
75+
imageSrc = fmt.Sprintf("%s%s", hashString(imageSrc), ext)
7176
header := textproto.MIMEHeader{
7277
"Content-ID": {fmt.Sprintf("<%s>", imageSrc)},
7378
}
@@ -89,6 +94,10 @@ func (tm *TemplateManager) RenderTemplate(name string, data interface{}) (string
8994
if err != nil {
9095
return template.HTML(fmt.Sprintf("Failed to read image: %s", imageSrc))
9196
}
97+
// Inline images must have unique names
98+
// 메일 읽지도 않았는데 탈락 이미지 이름이 미리보기에 나오면 슬프지 않을까요?
99+
ext := filepath.Ext(imageSrc)
100+
imageSrc = fmt.Sprintf("%s-%s%s", hashString(imageSrc), width, ext)
92101
header := textproto.MIMEHeader{
93102
"Content-ID": {fmt.Sprintf("<%s>", imageSrc)},
94103
}
@@ -148,3 +157,9 @@ func (tm *TemplateManager) DeleteTemplate(name string) {
148157
func (tm *TemplateManager) Templates() map[string]*template.Template {
149158
return tm.ExportedTemplates()
150159
}
160+
161+
func hashString(s string) string {
162+
h := sha3.New512()
163+
h.Write([]byte(s))
164+
return fmt.Sprintf("%x", h.Sum(nil))
165+
}

0 commit comments

Comments
 (0)