-
Notifications
You must be signed in to change notification settings - Fork 2
/
zpl.go
225 lines (173 loc) · 4 KB
/
zpl.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
package zpl
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"os"
)
type converter struct {
Input io.Reader
output io.Writer
outputFormat string
density int // dpmm
width int // inch
height int // inch
}
type option func(c *converter) error
func WithInput(input io.Reader) option {
return func(c *converter) error {
if input == nil {
return ErrNilInput
}
c.Input = input
return nil
}
}
func WithInputFromArgs(args []string) option {
return func(c *converter) error {
if len(args) == 0 {
return nil
}
f, err := os.Open(args[0])
if err != nil {
return fmt.Errorf("failed to open input file: %w", err)
}
c.Input = f
return nil
}
}
func WithOutput(output io.Writer) option {
return func(c *converter) error {
if output == nil {
return ErrNilOutput
}
c.output = output
return nil
}
}
func WithOutputPath(path string) option {
return func(c *converter) error {
if path == "" {
return nil
}
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0755)
if err != nil {
return fmt.Errorf("failed to open output file: %w", err)
}
c.output = f
return nil
}
}
func WithOutputFormat(outputFormat string) option {
return func(c *converter) error {
for _, allowedOutputFormat := range allowedOutputFormats() {
if outputFormat == allowedOutputFormat {
c.outputFormat = outputFormat
return nil
}
}
return ErrInvalidOutputFormat
}
}
func WithDensity(density int) option {
return func(c *converter) error {
for _, allowedDensity := range allowedDensities() {
if density == allowedDensity {
c.density = density
return nil
}
}
return ErrInvalidDensity
}
}
func WithWidth(width int) option {
return func(c *converter) error {
c.width = width
return nil
}
}
func WithHeight(height int) option {
return func(c *converter) error {
c.height = height
return nil
}
}
func NewConverter(opts ...option) (*converter, error) {
c := &converter{
Input: os.Stdin,
output: os.Stdout,
outputFormat: PNG,
density: 8,
width: 4,
height: 6,
}
for _, opt := range opts {
if err := opt(c); err != nil {
return nil, err
}
}
return c, nil
}
func (conv *converter) Convert() error {
converted, err := conv.doRequest()
if err != nil {
return err
}
defer converted.Close()
if _, err := io.Copy(conv.output, converted); err != nil {
return fmt.Errorf("failed to write output: %w", err)
}
return nil
}
func (conv *converter) doRequest() (io.ReadCloser, error) {
url := fmt.Sprintf("http://api.labelary.com/v1/printers/%ddpmm/labels/%dx%d/0/", conv.density, conv.width, conv.height)
req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, url, conv.Input)
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
contentTypes := map[string]string{
PDF: "application/pdf",
PNG: "image/png",
}
req.Header.Set("Accept", contentTypes[conv.outputFormat])
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("request failed: %w", err)
}
return res.Body, nil
}
func Convert(opts ...option) error {
conv, err := NewConverter(opts...)
if err != nil {
return err
}
if err := conv.Convert(); err != nil {
return fmt.Errorf("failed to convert: %w", err)
}
return nil
}
func ConvertBytes(content []byte, opts ...option) ([]byte, error) {
output := &bytes.Buffer{}
options := []option{
WithInput(bytes.NewBuffer(content)),
WithOutput(output),
}
options = append(options, opts...)
if err := Convert(options...); err != nil {
return nil, err
}
return output.Bytes(), nil
}
func ToPNG(content []byte, opts ...option) ([]byte, error) {
options := []option{WithOutputFormat(PNG)}
options = append(options, opts...)
return ConvertBytes(content, options...)
}
func ToPDF(content []byte, opts ...option) ([]byte, error) {
options := []option{WithOutputFormat(PDF)}
options = append(options, opts...)
return ConvertBytes(content, options...)
}