forked from go-shiori/obelisk
-
Notifications
You must be signed in to change notification settings - Fork 1
/
archiver.go
280 lines (237 loc) · 6.73 KB
/
archiver.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package obelisk
import (
"context"
"fmt"
"io"
"net/http"
nurl "net/url"
"os"
"path/filepath"
"strings"
"sync"
"time"
"github.com/cenkalti/backoff/v4"
"github.com/kennygrant/sanitize"
"golang.org/x/sync/semaphore"
)
var (
defaultUserAgent = "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:73.0) Gecko/20100101 Firefox/73.0"
maxElapsedTime = 30 * time.Second
)
// Request is data of archival request.
type Request struct {
Input io.Reader
URL string
// Deprecated: Use `Archiver.WithCookies` instead.
Cookies []*http.Cookie
origin *nurl.URL // The original URL request was based from the input. If there are no redirects, it should be the same as `URL`.
}
// Asset is asset that used in a web page.
type Asset struct {
Data []byte
ContentType string
}
// Archiver is the core of obelisk, which used to download a
// web page then embeds its assets.
type Archiver struct {
sync.RWMutex
Cache map[string]Asset
UserAgent string
EnableLog bool
EnableVerboseLog bool
LocalFile bool
DisableJS bool
DisableCSS bool
DisableEmbeds bool
DisableMedias bool
Transport http.RoundTripper
RequestTimeout time.Duration
MaxRetries int
MaxConcurrentDownload int64
SkipResourceURLError bool
WrapDirectory string // directory to stores resources
isValidated bool
cookies []*http.Cookie
httpClient *http.Client
dlSemaphore *semaphore.Weighted
}
// Validate prepares Archiver to make sure its configurations
// are valid and ready to use. Must be run at least once before
// archival started.
func (arc *Archiver) Validate() {
if arc.Cache == nil {
arc.Cache = make(map[string]Asset)
}
if arc.UserAgent == "" {
arc.UserAgent = defaultUserAgent
}
if arc.MaxConcurrentDownload <= 0 {
arc.MaxConcurrentDownload = 10
}
arc.isValidated = true
arc.dlSemaphore = semaphore.NewWeighted(arc.MaxConcurrentDownload)
if arc.Transport == nil {
arc.Transport = http.DefaultTransport
}
arc.httpClient = &http.Client{
Timeout: arc.RequestTimeout,
Transport: arc.Transport,
}
}
// Archive starts archival process for the specified request.
// Returns the archival result, content type and error if there are any.
func (arc *Archiver) Archive(ctx context.Context, req Request) ([]byte, string, error) {
// Make sure archiver has been validated
if !arc.isValidated {
return nil, "", fmt.Errorf("archiver hasn't been validated")
}
// Validate request
if req.URL == "" {
return nil, "", fmt.Errorf("request url is not specified")
}
var (
err error
url *nurl.URL
contentType string = "text/html"
)
url, err = nurl.Parse(req.URL)
if err != nil {
return nil, "", fmt.Errorf("url \"%s\" is not valid", req.URL)
}
if url.Scheme == "file" || url.Scheme == "" {
arc.LocalFile = true
contentType = getFileContentType(url.Path)
req.Input, err = os.Open(url.Path)
if err != nil {
return nil, "", fmt.Errorf("failed to open file: %w", err)
}
if !strings.HasPrefix(contentType, "text/html") {
content, err := os.ReadFile(url.Path)
return content, contentType, err
}
url.Path = filepath.Dir(url.Path)
} else {
if url.Scheme == "" || url.Hostname() == "" {
return nil, "", fmt.Errorf("url \"%s\" is not valid", req.URL)
}
// Set the original url
req.origin = url
ctx = withOrigin(ctx, req.origin)
url = arc.finalURI(url)
// If needed download page from source URL
if req.Input == nil {
resp, err := arc.downloadFile(url.String(), "")
if err != nil {
return nil, "", fmt.Errorf("download failed: %w", err)
}
defer resp.Body.Close()
req.Input = resp.Body
contentType = resp.Header.Get("Content-Type")
}
// Check the type of the downloaded file.
// If it's not HTML, just return it as it is.
if !strings.HasPrefix(contentType, "text/html") {
content, err := io.ReadAll(req.Input)
return content, contentType, err
}
}
// If it's HTML process it
result, err := arc.processHTML(ctx, req.Input, url, false)
if err != nil {
return nil, "", err
}
return s2b(result), contentType, nil
}
// WithCookies attach request cookies to `Archiver`.
func (arc *Archiver) WithCookies(cookies []*http.Cookie) *Archiver {
arc.cookies = cookies
return arc
}
// finalURI returns the final URL that has been redirected to another URL.
func (arc *Archiver) finalURI(u *nurl.URL) *nurl.URL {
req, err := http.NewRequest(http.MethodHead, u.String(), nil)
if err != nil {
return u
}
req.Header.Set("User-Agent", arc.UserAgent)
resp, err := arc.httpClient.Do(req)
if err != nil {
return u
}
defer resp.Body.Close()
return resp.Request.URL
}
func (arc *Archiver) downloadFile(url string, parentURL string) (*http.Response, error) {
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", arc.UserAgent)
if parentURL != "" {
req.Header.Set("Referer", parentURL)
}
for _, cookie := range arc.cookies {
req.AddCookie(cookie)
}
var resp *http.Response
op := func() error {
var err error
resp, err = arc.httpClient.Do(req) //nolint:bodyclose,goimports
if err == nil && (resp.StatusCode >= http.StatusInternalServerError || resp.StatusCode == http.StatusTooManyRequests) {
err = fmt.Errorf("failed to fetch with status code: %d", resp.StatusCode)
}
return err
}
exp := backoff.NewExponentialBackOff()
exp.MaxElapsedTime = maxElapsedTime
bo := backoff.WithMaxRetries(exp, uint64(arc.MaxRetries))
err = backoff.Retry(op, bo)
return resp, err
}
func (arc *Archiver) transform(uri string, content []byte, contentType string) string {
// If no directory to store files is specified, save as a single file.
if arc.WrapDirectory == "" {
if contentType == "" {
contentType = http.DetectContentType(content)
}
return createDataURL(content, contentType)
}
path, name, err := arc.store(uri)
if err != nil {
name = sanitize.BaseName(uri)
path = filepath.Join(arc.WrapDirectory, name)
}
if err := os.WriteFile(path, content, 0600); err != nil {
// Fallback to creating data URL
if contentType == "" {
contentType = http.DetectContentType(content)
}
return createDataURL(content, contentType)
}
return filepath.Join(".", name)
}
func (arc *Archiver) store(uri string) (path string, rel string, err error) {
if uri == "" {
return "", "", nil
}
u, err := nurl.ParseRequestURI(uri)
if err != nil {
return "", "", err
}
// e.g. /statics/css/foo.css
rel, err = filepath.Abs(u.Path)
if err != nil {
return "", "", err
}
// e.g. /tmp/some/statics/css/
dir := filepath.Join(arc.WrapDirectory, filepath.Dir(rel))
if err != nil {
return "", "", err
}
if err := os.MkdirAll(dir, 0700); err != nil {
return "", "", err
}
// e.g. /tmp/some/statics/css/foo.css
path = filepath.Join(dir, filepath.Base(rel))
return path, rel, nil
}