forked from tgulacsi/agostle
/
filters_html.go
247 lines (231 loc) · 7.28 KB
/
filters_html.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// Copyright 2013 The Agostle Authors. All rights reserved.
// Use of this source code is governed by an Apache 2.0
// license that can be found in the LICENSE file.
package converter
import (
"fmt"
"io"
"os"
"path/filepath"
"sort"
"strings"
"github.com/tgulacsi/go/i18nmail"
)
type cidGroup struct {
htmlFn string
cids map[string]string
}
// HTMLPartFilter reads multipart/alternative (text/plain + text/html), preferring the html
// part + groups the multipart/related images which are referred in the html.
//
// multipart/related encapsulates multipart/alternative,
// which contains text/plain and text/html, the related part contains images,
// too - at least usually.
func HTMLPartFilter(inch <-chan i18nmail.MailPart, outch chan<- i18nmail.MailPart,
files chan<- ArchFileItem, errch chan<- error, ctx *Context) {
defer func() {
logger.Debug("msg", "HtmlFilter closes", "outch", outch)
close(outch)
}()
ctx = prepareContext(ctx, "")
var (
alter = ""
converter = GetConverter("text/html", nil)
aConverter Converter
tbd = make(map[string]struct{}, 4)
)
if !LeaveTempFiles {
defer func() {
tbdA := make([]string, 0, len(tbd))
for k := range tbd {
tbdA = append(tbdA, k)
}
sort.Strings(tbdA)
for i := len(tbdA) - 1; i > -1; i-- {
_ = unlinkAll(tbdA[i])
}
}()
}
html2pdf := func(fn string) (string, error) {
if fn == "" {
logger.Warn("msg", "empty filename!!!")
return "", fmt.Errorf("empty filename!")
}
tbd[fn] = struct{}{}
destfn := filepath.Join(ctx.Dir, filepath.Base(fn)+".pdf")
fh, err := os.Open(fn)
if err != nil {
err = fmt.Errorf("cannot open html %s: %s", fn, err)
} else {
logger.Debug("msg", "html2pdf calling", "converter", converter, "dest", destfn, "source", fh)
if err = converter(destfn, fh, "text/html", ctx); err != nil {
err = fmt.Errorf("error converting %s to %s: %s", fn, destfn, err)
}
}
if err != nil {
logger.Error("msg", "html2pdf", "error", err)
if alter != "" && aConverter != nil {
logger.Warn("msg", "html2pdf using alternative content", "alter", alter)
if fh, err = os.Open(alter); err != nil {
err = fmt.Errorf("cannot open txt %s: %s", alter, err)
} else {
logger.Debug("msg", "alternative", "converter", aConverter, "dest", destfn, "source", fh)
if err = aConverter(destfn, fh, "text/plain", ctx); err != nil {
err = fmt.Errorf("error converting %s to %s: %s", alter, destfn, err)
}
}
alter, aConverter = "", nil
}
}
return destfn, err
}
groups := make(map[int]cidGroup, 4)
var (
this, last int
err error
ok bool
fn, dn, cid string
parent, grandpa *i18nmail.MailPart
)
last = -1
for part := range inch {
logger.Debug("msg", "HtmlPartFilter receives", "seq", part.Seq, "ct", part.ContentType, "header", part.Header, "inch", inch, "groups", groups)
this = -1
parent = part.Parent
if parent == nil {
grandpa = nil
} else {
grandpa = parent.Parent
}
if parent == nil {
logger.Debug("msg", "HtmlPartFilter parent is nil!", "part-ct", part.ContentType)
} else {
logger.Debug("msg", "", "part-ct", part.ContentType, "parent-seq", parent.Seq, "parent-ct", parent.ContentType, "gp", grandpa)
}
if part.ContentType == "text/plain" || part.ContentType == "text/html" {
//if part.Parent.ContentType != "multipart/alternative" || part.Parent.ContentType != "multipart/related" {
if part.ContentType == "text/plain" && part.Parent.ContentType != "multipart/alternative" {
logger.Debug("msg", "SKIP non multipart/alternative", "parent-ct", part.Parent.ContentType)
goto Skip
}
if grandpa != nil {
this = grandpa.Seq
logger.Debug("msg", "grandpa", "gp-seq", grandpa.Seq, "gp-level", grandpa.Level)
}
if this != last && last > -1 && groups[last].htmlFn != "" {
logger.Debug("msg", "html2pdf", "ct", part.ContentType, "parent", part.Parent.ContentType, "this", this, "last", last, "groups", groups)
if fn, err = html2pdf(groups[last].htmlFn); err != nil {
goto Error
}
files <- ArchFileItem{Filename: fn}
}
if grandpa != nil {
dn = filepath.Join(ctx.Dir,
fmt.Sprintf("%02d!%03d.text--html", grandpa.Level, grandpa.Seq))
} else {
dn = ctx.Dir
}
_ = os.Mkdir(dn, 0755) //ignore errors
if parent != nil {
fn = fmt.Sprintf("%02d!%03d.index", parent.Level, parent.Seq)
} else {
fn = fmt.Sprintf("%02d!%03d.index", part.Level, part.Seq)
}
var cids map[string]string
if part.ContentType == "text/plain" {
fn = fn + ".txt"
} else {
cids = make(map[string]string, 4)
if SaveOriginalHTML {
if orifh, e := os.Create(filepath.Join(dn, fn) + "-ori.html"); e == nil {
defer func() { _ = orifh.Close() }()
part.Body = io.TeeReader(part.Body, orifh)
}
}
part.Body = NewCidMapper(cids, "images", part.Body)
fn = fmt.Sprintf("%s-%02d.html", fn, this)
}
fn = filepath.Join(dn, fn)
if err = writeToFile(fn, part.Body, part.ContentType /*, mailHeader*/); err != nil {
goto Error
}
tbd[fn] = struct{}{}
if part.ContentType == "text/plain" {
alter = fn
aConverter = GetConverter("text/plain", part.MediaType)
} else if part.ContentType == "text/html" {
//log.Printf("last==this? %b cidmap: %s", last == this, cids)
if _, ok := groups[this]; !ok {
groups[this] = cidGroup{htmlFn: fn, cids: cids}
} else {
err = fmt.Errorf("html for %d already occupied by %s - now comes %s",
last, groups[last].htmlFn, fn)
goto Error
}
}
last = this
continue
}
if !strings.HasPrefix(part.ContentType, "image/") {
logger.Debug("msg", "SKIP non-image", "ct", part.ContentType)
goto Skip
}
cid = strings.Trim(part.Header.Get("Content-ID"), "<>")
if cid == "" {
logger.Debug("msg", "SKIP, has no CID", "seq", part.Seq)
goto Skip
}
logger.Debug("msg", "CID", "cid", cid)
this = parent.Seq
{
found := false
if len(groups) > 0 {
for act := ∂ act != nil; act = act.Parent {
s := -1
if act.Parent != nil {
s = act.Parent.Seq
}
logger.Debug("msg", "parent", "act-seq", act.Seq, "s", s)
if _, ok := groups[act.Seq]; ok {
found = true
this = act.Seq
break
}
}
}
if !found {
err = fmt.Errorf("WARN this=%d not in %v", part.Seq, groups)
logger.Warn("msg", "SKIP not found", "error", err)
goto Skip
}
}
// K-MT9641 skip images which aren't in the .html
if fn, ok = groups[this].cids[cid]; !ok {
logger.Debug("msg", "SKIP not found CID", "seq", part.Seq, "cid", cid, "this", this, "groupcids", groups[this].cids)
goto Skip
}
fn = filepath.Join(filepath.Dir(groups[this].htmlFn), fn)
logger.Debug("msg", "writeToFile", "file", fn)
_ = os.Mkdir(filepath.Dir(fn), 0755) // ignore error
if err = writeToFile(fn, part.Body, part.ContentType /*, mailHeader*/); err != nil {
goto Error
}
tbd[filepath.Dir(fn)] = struct{}{}
continue
Error:
logger.Error("msg", "ERROR", "error", err)
if err != nil {
errch <- err
}
Skip:
logger.Debug("msg", "SKIP", "seq", part.Seq, "part", part, "ct", part.ContentType)
outch <- part
}
for k, v := range groups {
logger.Debug("msg", "html2pdf atend", "i", k, "htmlFn", v)
if fn, err = html2pdf(v.htmlFn); err != nil {
errch <- err
}
files <- ArchFileItem{Filename: fn}
}
}