-
Notifications
You must be signed in to change notification settings - Fork 1
/
printer.go
157 lines (128 loc) · 3.16 KB
/
printer.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
package main
import (
"bytes"
"encoding/base64"
"encoding/hex"
"errors"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"os/exec"
"strconv"
"strings"
"unicode/utf8"
"golang.org/x/text/encoding/charmap"
)
func listen() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
printHandler(w, r)
})
if err := http.ListenAndServe(":9823", nil); err != nil {
log.Fatal(err)
}
}
func printHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET,POST,PUT")
w.Header().Set("Access-Control-Allow-Headers", "auth,clientguid,pragma,Content-Type,User-Agent,Referer,Origin")
w.Header().Set("Access-Control-Expose-Headers", "Cache-Breaker")
data := r.FormValue("data")
if data == "" {
data = r.PostFormValue("data")
}
b, err := base64.StdEncoding.DecodeString(data)
if err != nil {
fmt.Println("Decode error", err)
w.Write([]byte(err.Error()))
}
s, err := url.QueryUnescape(string(b))
if err != nil {
fmt.Println("Encode error", err)
w.Write([]byte(err.Error()))
}
//b = []byte(s)
b, err = charmap.Windows1252.NewEncoder().Bytes([]byte(s))
if err != nil {
// TODO validate chars and replace invalid with ?
// For now just print the original text.
fmt.Println("ENCODING", err)
}
if err := printer.Write(b); err != nil {
fmt.Println("Write error", err)
w.Write([]byte(err.Error()))
}
}
type Printer struct {
CUPS bool
Name string
file io.Writer
}
func New(name string, cups bool) (*Printer, error) {
if cups {
return &Printer{Name: name, CUPS: true}, nil
}
if strings.HasPrefix(name, "\\") {
return &Printer{Name: name}, nil
}
f, err := os.Create(name)
if err != nil {
return nil, err
}
return &Printer{Name: name, file: f}, nil
}
func (p *Printer) sendToCUPS(b []byte) error {
cmd := exec.Command("lpr", "-o", "raw", "-H", "localhost", "-P", p.Name)
pipe, err := cmd.StdinPipe()
if err != nil {
return err
}
go func() {
io.Copy(pipe, bytes.NewReader(b))
pipe.Close()
}()
out, err := cmd.CombinedOutput()
if err != nil {
return errors.New(string(out))
}
return nil
}
func decode(b []byte) ([]byte, error) {
var buf bytes.Buffer
for i, step, l := 0, 0, len(b); i < l; i += step {
r, width := utf8.DecodeRune(b[i:])
// \b marks the start of a byte sequence
if r == '\b' {
// the next byte is the length of the sequence
s := string(b[i+1 : i+3])
step = 3
// Use 00 for length greater than 255 in the next 4 bytes
if s == "00" {
s = string(b[i+3 : i+11])
step = 11
}
// convert the hex string to the number of bytes
h, err := strconv.ParseInt(s, 16, 0)
if err != nil {
return nil, err
}
// a byte is expressed as 2 hex digits
numBytes := int(h) * 2
// decode hex digits to bytes
dst := make([]byte, int(h))
_, err = hex.Decode(dst, b[i+step:i+step+numBytes])
if err != nil {
return nil, fmt.Errorf("Invalid hex value (%s): %v", b[i+step:i+step+numBytes], err)
}
buf.Write(dst)
step += numBytes
continue
}
buf.Write(b[i : i+width])
step = width
}
return buf.Bytes(), nil
}