-
Notifications
You must be signed in to change notification settings - Fork 21
/
init.go
103 lines (88 loc) · 2.4 KB
/
init.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
package main
import (
"encoding/json"
"fmt"
"image/color"
"io/ioutil"
"log"
"strings"
"github.com/knadh/pfxsigner/internal/processor"
"github.com/unidoc/unipdf/v3/model"
"github.com/urfave/cli"
)
func initApp(f cli.ActionFunc) cli.ActionFunc {
return func(c *cli.Context) error {
// Read JSON config.
fName := c.GlobalString("props-file")
log.Printf("loading signature properties from %s", fName)
b, err := ioutil.ReadFile(fName)
if err != nil {
log.Fatalf("error opening properties file %s: %v", fName, err)
}
// Make properties.
pr, err := parseProps(b)
if err != nil {
log.Fatalf("error in JSON properties file: %v", err)
}
// Initialize global workers.
proc = processor.New(pr, logger)
// Load one or more PFX certificates.
for _, p := range c.GlobalStringSlice("pfx") {
c := strings.Split(p, "|")
if len(c) != 3 {
log.Fatalf("invalid certificate entry '%s'", p)
}
if err := proc.LoadPFX(c[0], c[1], c[2]); err != nil {
log.Fatalf("error loading PFX: %v", err)
}
}
return f(c)
}
}
// parseProps loads signature properties from the given JSON blob.
func parseProps(b []byte) (processor.SignProps, error) {
var pr processor.SignProps
if err := json.Unmarshal(b, &pr); err != nil {
return pr, fmt.Errorf("error parsing JSON properties file: %v", err)
}
// Validate colours.
var err error
pr.Style.FontColorRGBA, err = parseHexColor(pr.Style.FontColor)
if err != nil {
return pr, fmt.Errorf("invalid `fontColor`: %v", err)
}
pr.Style.BgColorRGBA, err = parseHexColor(pr.Style.BgColor)
if err != nil {
return pr, fmt.Errorf("invalid `bgColor`: %v", err)
}
pr.Style.BorderColorRGBA, err = parseHexColor(pr.Style.BorderColor)
if err != nil {
return pr, fmt.Errorf("invalid `borderColor`: %v", err)
}
return pr, nil
}
// parseHexColor converts a hex colour string to RGB.
// https://stackoverflow.com/questions/54197913/parse-hex-string-to-image-color
func parseHexColor(s string) (model.PdfColorDeviceRGB, error) {
var (
c = color.RGBA{A: 0xff}
err error
)
switch len(s) {
case 7:
_, err = fmt.Sscanf(s, "#%02x%02x%02x", &c.R, &c.G, &c.B)
case 4:
_, err = fmt.Sscanf(s, "#%1x%1x%1x", &c.R, &c.G, &c.B)
// Double the hex digits:
c.R *= 17
c.G *= 17
c.B *= 17
default:
err = fmt.Errorf("invalid colour hex length, must be 7 or 4")
}
return model.PdfColorDeviceRGB{
float64(c.R),
float64(c.G),
float64(c.B),
}, err
}