-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsource.go
More file actions
169 lines (150 loc) · 3.95 KB
/
source.go
File metadata and controls
169 lines (150 loc) · 3.95 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
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
package keepcurrent
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"net/http"
"os"
"path"
"sync"
"time"
"github.com/mholt/archives"
)
type webSource struct {
url string
etag string
mx sync.RWMutex
client *http.Client
}
// FromWeb constructs a source from the given URL.
func FromWeb(url string) Source {
return FromWebWithClient(url, http.DefaultClient)
}
// FromWebWithClient is the same as FromWeb but with a custom http.Client
func FromWebWithClient(url string, client *http.Client) Source {
return &webSource{url: url, client: client}
}
// Fetch implements the Source interface
func (s *webSource) Fetch(ifNewerThan time.Time) (io.ReadCloser, error) {
req, err := http.NewRequest(http.MethodGet, s.url, nil)
if err != nil {
return nil, err
}
if !ifNewerThan.IsZero() {
req.Header.Add("If-Modified-Since", ifNewerThan.Format(http.TimeFormat))
}
if s.getETag() != "" {
req.Header.Add("If-None-Match", s.etag)
}
resp, err := s.client.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode == http.StatusNotModified {
return nil, ErrUnmodified
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected HTTP status %v", resp.StatusCode)
}
etag := resp.Header.Get("ETag")
if etag != "" {
s.setETag(etag)
}
return resp.Body, nil
}
func (s *webSource) getETag() string {
s.mx.RLock()
defer s.mx.RUnlock()
return s.etag
}
func (s *webSource) setETag(etag string) {
s.mx.Lock()
s.etag = etag
s.mx.Unlock()
}
type tarGzSource struct {
s Source
expectedName string
}
// FromTarGz wraps a source to decompress one specific file from the gzipped
// tarball.
func FromTarGz(s Source, expectedName string) Source {
return &tarGzSource{s, expectedName}
}
var errFound = errors.New("found")
func (s *tarGzSource) Fetch(ifNewerThan time.Time) (io.ReadCloser, error) {
rc, err := s.s.Fetch(ifNewerThan)
if err != nil {
return nil, err
}
defer rc.Close()
// archives.CompressedArchive.Extract() reads ca.Extraction (not ca.Archival),
// and returns "no extraction format" if it's nil. Setting Archival here was a
// bug that made every Fetch() fail silently.
format := archives.CompressedArchive{
Compression: archives.Gz{},
Extraction: archives.Tar{},
}
var buf []byte
err = format.Extract(context.Background(), rc, func(ctx context.Context, info archives.FileInfo) error {
// NameInArchive is the full stored path (e.g. "GeoLite2-City_20260116/GeoLite2-City.mmdb").
// Callers pass the basename, so compare on basename — mirrors the behavior of the
// archiver/v3-based implementation this replaced.
if path.Base(info.NameInArchive) == s.expectedName {
f, err := info.Open()
if err != nil {
return err
}
defer f.Close()
buf, err = io.ReadAll(f)
if err != nil {
return err
}
return errFound
}
return nil
})
if errors.Is(err, errFound) {
return io.NopCloser(bytes.NewReader(buf)), nil
}
if err != nil {
return nil, err
}
return nil, fmt.Errorf("file %q not found in archive", s.expectedName)
}
type fileSource struct {
path string
preprocessor func(io.ReadCloser) (io.ReadCloser, error)
}
// FromFile constructs a source from the given file path.
func FromFile(path string) Source {
return &fileSource{path, nil}
}
// FromFileWithPreprocessor constructs a source from the given file path, while modifying the file data using preprocessor function
func FromFileWithPreprocessor(path string, preprocessor func(io.ReadCloser) (io.ReadCloser, error)) Source {
return &fileSource{path, preprocessor}
}
func (s *fileSource) Fetch(ifNewerThan time.Time) (io.ReadCloser, error) {
f, err := os.Open(s.path)
if err != nil {
return nil, err
}
fi, err := f.Stat()
if err != nil {
return nil, err
}
if !ifNewerThan.IsZero() && ifNewerThan.Before(fi.ModTime()) {
return nil, ErrUnmodified
}
var result io.ReadCloser
result = f
if s.preprocessor != nil {
result, err = s.preprocessor(f)
if err != nil {
return nil, err
}
}
return result, nil
}