From f17f63bb944d886b552facf4b28a557b8e4a64d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20=27Necoro=27=20Neumann?= Date: Fri, 29 Oct 2021 01:31:10 +0200 Subject: #64: Set filename for included images --- CHANGELOG.md | 2 ++ internal/feed/item.go | 5 +++-- internal/feed/mail.go | 20 +++++++++++++++++--- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3adbf45..aa5a807 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Added +- [Issue #64](https://github.com/Necoro/feed2imap-go/issues/64): Set filename for included images. ## [1.2.0] - 2021-10-20 ### Added diff --git a/internal/feed/item.go b/internal/feed/item.go index f7dd506..b45f5c5 100644 --- a/internal/feed/item.go +++ b/internal/feed/item.go @@ -17,6 +17,7 @@ import ( type feedImage struct { image []byte mime string + name string } type Item struct { @@ -69,8 +70,8 @@ func (item *Item) AddReason(reason string) { } } -func (item *Item) addImage(img []byte, mime string) int { - i := feedImage{img, mime} +func (item *Item) addImage(img []byte, mime string, name string) int { + i := feedImage{img, mime, name} item.images = append(item.images, i) return len(item.images) } diff --git a/internal/feed/mail.go b/internal/feed/mail.go index b6cd231..f5863a6 100644 --- a/internal/feed/mail.go +++ b/internal/feed/mail.go @@ -123,10 +123,19 @@ func (item *Item) writeHtmlPart(w *message.Writer) error { return item.writeContentPart(w, "html", template.Html) } +func (img *feedImage) buildNameMap(key string) map[string]string { + if img.name == "" { + return nil + } + return map[string]string{key: img.name} +} + func (img *feedImage) writeImagePart(w *message.Writer, cid string) error { var ih message.Header - ih.SetContentType(img.mime, nil) - ih.SetContentDisposition("inline", nil) + // set filename for both Type and Disposition + // according to standard, it belongs to the latter -- but some clients expect the former + ih.SetContentType(img.mime, img.buildNameMap("name")) + ih.SetContentDisposition("inline", img.buildNameMap("filename")) ih.Set("Content-Transfer-Encoding", "base64") ih.SetText("Content-ID", fmt.Sprintf("<%s>", cid)) @@ -288,7 +297,12 @@ func (item *Item) downloadImage(src string) string { if feed.EmbedImages { return "data:" + mime + ";base64," + base64.StdEncoding.EncodeToString(img) } else { - idx := item.addImage(img, mime) + name := path.Base(src) + if name == "/" || name == "." || name == " " { + name = "" + } + + idx := item.addImage(img, mime, name) return "cid:" + cidNr(idx) } } -- cgit v1.2.3