-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtee_response.go
More file actions
132 lines (114 loc) · 2.67 KB
/
Copy pathtee_response.go
File metadata and controls
132 lines (114 loc) · 2.67 KB
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
package httpmirror
import (
"context"
"errors"
"fmt"
"io"
"io/fs"
"net/http"
"path"
"github.com/wzshiming/ioswmr"
)
type teeResponse struct {
fileInfo fs.FileInfo
swmr ioswmr.SWMR
etag string
}
func (t *teeResponse) ServeHTTP(w http.ResponseWriter, r *http.Request) {
size := t.fileInfo.Size()
if size > 0 {
rs := t.swmr.NewReadSeeker(0, int(size))
defer rs.Close()
name := path.Base(r.URL.Path)
if t.etag != "" {
w.Header().Set("ETag", t.etag)
}
w.Header().Set("Content-Type", "application/octet-stream")
http.ServeContent(w, r, name, t.fileInfo.ModTime(), rs)
} else {
rs := t.swmr.NewReader(0)
defer rs.Close()
if t.etag != "" {
w.Header().Set("ETag", t.etag)
}
w.WriteHeader(http.StatusOK)
if r.Method == http.MethodGet {
_, _ = io.Copy(w, rs)
}
}
}
func (m *MirrorHandler) cacheFileTee(ctx context.Context, sourceFile, cacheFile string) (*teeResponse, error) {
body, info, err := httpGet(ctx, m.client(), sourceFile, true)
if err != nil {
return nil, err
}
contentLength := info.Size()
if contentLength == 0 {
_ = body.Close()
return nil, ErrNotOK
}
if m.Logger != nil {
m.Logger.Println("Tee Cache", cacheFile, contentLength)
}
fw, err := m.RemoteCache.Writer(ctx, cacheFile)
if err != nil {
if m.Logger != nil {
m.Logger.Println("Cache writer error", cacheFile, contentLength, err)
}
_ = body.Close()
return nil, err
}
swmr := ioswmr.NewSWMR(
ioswmr.NewMemoryOrTemporaryFileBuffer(nil, nil),
ioswmr.WithAutoClose(),
ioswmr.WithBeforeCloseFunc(func() {
m.teeCache.Delete(cacheFile)
if m.Logger != nil {
m.Logger.Println("Tee Cache closed", cacheFile, err)
}
}),
)
tee := &teeResponse{
fileInfo: info,
swmr: swmr,
etag: info.ETag(),
}
sw := swmr.Writer()
go func() {
defer body.Close()
_, err := io.Copy(sw, body)
_ = sw.CloseWithError(err)
}()
go func() {
r := swmr.NewReader(0)
defer r.Close()
defer fw.Close()
n, err := io.Copy(fw, r)
if err != nil && !errors.Is(err, io.EOF) {
if m.Logger != nil {
m.Logger.Println("SWMR copy error", cacheFile, contentLength, n, err)
}
_ = fw.Cancel(context.Background())
return
}
if contentLength > 0 && n != contentLength {
err = fmt.Errorf("copied %d bytes, expected %d", n, contentLength)
if m.Logger != nil {
m.Logger.Println("Cache copy error", cacheFile, err)
}
_ = fw.Cancel(context.Background())
return
}
err = fw.Commit(context.Background())
if err != nil {
if m.Logger != nil {
m.Logger.Println("Cache Commit error", cacheFile, err)
}
return
}
if m.Logger != nil {
m.Logger.Println("Tee Cached", cacheFile, contentLength, n)
}
}()
return tee, nil
}