-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencodeWr.go
89 lines (74 loc) · 1.53 KB
/
encodeWr.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
package icat
import (
"bytes"
"encoding/base64"
"fmt"
"io"
"os"
"reflect"
)
type EncodeWr struct {
W io.Writer
buf *bytes.Buffer
enc io.WriteCloser
}
func NewEncodeWr(w io.Writer, buff []byte) *EncodeWr {
if buff == nil {
buff = make([]byte, 0, 10240)
}
return &EncodeWr{
W: w,
buf: bytes.NewBuffer(buff),
enc: base64.NewEncoder(base64.StdEncoding, w),
}
}
func (p *EncodeWr) Write(buf []byte) (n int, err error) {
return p.buf.Write(buf)
}
func (p *EncodeWr) FlushStdout() error {
fmt.Fprint(p.W, "\033]1337;File=;inline=1:")
_, err := io.Copy(p.enc, p.buf)
p.enc.Close()
fmt.Fprintln(p.W, "\a")
// p.buf.Reset()
return err
}
func (p *EncodeWr) close() error {
return p.enc.Close()
}
func (p *EncodeWr) FlushBase64Stdout(imgBase64 string) error {
defer p.close()
fmt.Fprint(p.W, "\033]1337;File=;inline=1:")
defer fmt.Fprintln(p.W, "\a")
_, err := fmt.Fprintln(p.W, imgBase64)
return err
}
func (p *EncodeWr) Flush() error {
defer p.close()
_, err := io.Copy(p.W, p.buf)
p.buf.Reset()
return err
}
type EncodeStdout struct {
enc io.WriteCloser
writed bool
}
var (
EOFB = []byte{174, 66, 96, 130}
)
func NewEncodeStdout() *EncodeStdout {
return &EncodeStdout{
enc: base64.NewEncoder(base64.StdEncoding, os.Stdout),
}
}
func (p *EncodeStdout) Write(buf []byte) (n int, err error) {
if !p.writed {
fmt.Fprint(os.Stdout, "\033]1337;File=;inline=1:")
p.writed = true
}
n, err = p.enc.Write(buf)
if n == 4 && reflect.DeepEqual(buf, EOFB) {
fmt.Fprintln(os.Stdout, "\a")
}
return n, err
}