-
Notifications
You must be signed in to change notification settings - Fork 20
/
format_mime.go
71 lines (54 loc) · 1.41 KB
/
format_mime.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
package condition
import (
"context"
"encoding/json"
"fmt"
"github.com/brexhq/substation/v2/config"
"github.com/brexhq/substation/v2/message"
iconfig "github.com/brexhq/substation/v2/internal/config"
"github.com/brexhq/substation/v2/internal/media"
)
type formatMIMEConfig struct {
// Type is the media type used for comparison during inspection. Media types follow this specification: https://mimesniff.spec.whatwg.org/.
Type string `json:"type"`
Object iconfig.Object `json:"object"`
}
func (c *formatMIMEConfig) Decode(in interface{}) error {
return iconfig.Decode(in, c)
}
func (c *formatMIMEConfig) Validate() error {
if c.Type == "" {
return fmt.Errorf("type: %v", iconfig.ErrMissingRequiredOption)
}
return nil
}
func newFormatMIME(_ context.Context, cfg config.Config) (*formatMIME, error) {
conf := formatMIMEConfig{}
if err := conf.Decode(cfg.Settings); err != nil {
return nil, err
}
if err := conf.Validate(); err != nil {
return nil, err
}
insp := formatMIME{
conf: conf,
}
return &insp, nil
}
type formatMIME struct {
conf formatMIMEConfig
}
func (c *formatMIME) Condition(ctx context.Context, msg *message.Message) (bool, error) {
if msg.HasFlag(message.IsControl) {
return false, nil
}
media := media.Bytes(msg.Data())
if media == c.conf.Type {
return true, nil
}
return false, nil
}
func (c *formatMIME) String() string {
b, _ := json.Marshal(c.conf)
return string(b)
}