-
Notifications
You must be signed in to change notification settings - Fork 2
/
singlefile.go
76 lines (58 loc) · 1.64 KB
/
singlefile.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
package processors
import (
"bytes"
"context"
"fmt"
"net/http"
"go.uber.org/zap"
"golang.org/x/net/html"
"github.com/derfenix/webarchive/adapters/processors/internal"
"github.com/derfenix/webarchive/entity"
)
func NewSingleFile(client *http.Client, log *zap.Logger) *SingleFile {
return &SingleFile{client: client, log: log}
}
type SingleFile struct {
client *http.Client
log *zap.Logger
}
func (s *SingleFile) Process(ctx context.Context, pageURL string, cache *entity.Cache) ([]entity.File, error) {
reader := cache.Reader()
if reader == nil {
response, err := s.get(ctx, pageURL)
if err != nil {
return nil, err
}
defer func() {
_ = response.Body.Close()
}()
reader = response.Body
}
inlinedHTML, err := internal.NewMediaInline(s.log, s.get).Inline(ctx, reader, pageURL)
if err != nil {
return nil, fmt.Errorf("inline media: %w", err)
}
buf := bytes.NewBuffer(nil)
if err := html.Render(buf, inlinedHTML); err != nil {
return nil, fmt.Errorf("render result html: %w", err)
}
htmlFile := entity.NewFile("page.html", buf.Bytes())
return []entity.File{htmlFile}, nil
}
func (s *SingleFile) get(ctx context.Context, url string) (*http.Response, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, fmt.Errorf("new request: %w", err)
}
response, err := s.client.Do(req)
if err != nil {
return nil, fmt.Errorf("do request: %w", err)
}
if response.StatusCode != http.StatusOK {
return nil, fmt.Errorf("want status 200, got %d", response.StatusCode)
}
if response.Body == nil {
return nil, fmt.Errorf("empty response body")
}
return response, nil
}