-
Notifications
You must be signed in to change notification settings - Fork 0
/
safe_receiver.go
180 lines (155 loc) · 3.61 KB
/
safe_receiver.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
package safetyrecevier
import (
"bytes"
"io/ioutil"
"os"
)
type readHandler func([]byte) (int, error)
type seekHandler func(int64, int) (int64, error)
type appendHandler func([]byte, int)
type handler struct {
read readHandler
seek seekHandler
append appendHandler
}
type tmpFileReader interface {
Read(p []byte) (n int, err error)
Close() error
Seek(offset int64, whence int) (int64, error)
}
// SafeReceive safe receive handler
type SafeReceive struct {
maxMemorySize int
b []byte
byteSize int
isLargeFile bool
tmpFile *os.File
tmpFileRead tmpFileReader
reader *bytes.Reader
handle handler
}
// New receiver
func New(maxSize int) *SafeReceive {
sr := &SafeReceive{
maxMemorySize: maxSize,
}
sr.handle = handler{
read: sr.read,
seek: sr.seek,
append: sr.append,
}
return sr
}
// Read implement read interface
func (s *SafeReceive) Read(p []byte) (n int, err error) {
return s.handle.read(p)
}
// Seek implement seek interface
func (s *SafeReceive) Seek(offset int64, whence int) (int64, error) {
return s.handle.seek(offset, whence)
}
// CloseReceive close receive stream
func (s *SafeReceive) CloseReceive() error {
return s.closeReceive()
}
// CloseReader close reader and remove tmp file
func (s *SafeReceive) CloseReader() error {
return s.closeReader()
}
// Append chunk,n from expect from Read()
func (s *SafeReceive) Append(chunk []byte, n int) {
s.handle.append(chunk, n)
}
func (s *SafeReceive) append(chunk []byte, n int) {
s.b = append(s.b, chunk[:n]...)
var chunkSize int
if chunkSize = len(chunk); chunkSize >= s.maxMemorySize || chunkSize+s.byteSize >= s.maxMemorySize {
s.isLargeFile = true
s.setLargeFileHandler()
s.initTmpFile()
s.flush()
return
}
s.byteSize += chunkSize
}
// GetIsLargeFile is the large file
func (s *SafeReceive) GetIsLargeFile() bool {
return s.isLargeFile
}
// GetBytes get bytes if the large file return nil
func (s *SafeReceive) GetBytes() []byte {
return s.b
}
// GetTmpFilePath get tmp file path
func (s *SafeReceive) GetTmpFilePath() string {
if s.tmpFile == nil {
return ""
}
return s.tmpFile.Name()
}
func (s *SafeReceive) largeFileAppend(chunk []byte, n int) {
if _, err := s.tmpFile.Write(chunk[:n]); err != nil {
panic(err)
}
}
func (s *SafeReceive) initTmpFile() {
pwdDir, err := os.Getwd()
if err != nil {
panic(err)
}
s.tmpFile, err = ioutil.TempFile(pwdDir, "")
if err != nil {
panic(err)
}
}
func (s *SafeReceive) flush() {
if _, err := s.tmpFile.Write(s.b); err != nil {
panic(err)
}
s.b = nil
s.byteSize = 0
}
func (s *SafeReceive) setLargeFileHandler() {
s.handle.read = s.largeFileRead
s.handle.seek = s.largeSeek
s.handle.append = s.largeFileAppend
}
func (s *SafeReceive) largeFileRead(p []byte) (n int, err error) {
if s.tmpFileRead == nil {
s.tmpFileRead, err = os.Open(s.tmpFile.Name())
if err != nil {
return
}
}
return s.tmpFileRead.Read(p)
}
func (s *SafeReceive) read(p []byte) (n int, err error) {
if s.reader == nil {
s.reader = bytes.NewReader(s.b)
}
return s.reader.Read(p)
}
func (s *SafeReceive) largeSeek(offset int64, whence int) (int64, error) {
return s.tmpFile.Seek(offset, whence)
}
func (s *SafeReceive) seek(offset int64, whence int) (int64, error) {
if s.reader == nil {
s.reader = bytes.NewReader(s.b)
}
return s.reader.Seek(offset, whence)
}
func (s *SafeReceive) closeReceive() (err error) {
if s.tmpFile != nil {
err = s.tmpFile.Close()
}
return
}
func (s *SafeReceive) closeReader() (err error) {
if s.tmpFileRead != nil {
err = s.tmpFileRead.Close()
}
if s.tmpFile != nil {
os.Remove(s.tmpFile.Name())
}
return
}