-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtmlcapture.go
111 lines (91 loc) · 2.86 KB
/
htmlcapture.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
package htmlcapture
import (
"bytes"
"fmt"
"html/template"
"os"
"strings"
"github.com/rohankarn35/htmlcapture/internal/browser"
imgutil "github.com/rohankarn35/htmlcapture/internal/image"
)
type CaptureOptions struct {
Input string // Can be a URL, HTML string, or file path
ViewportW int // Viewport width (default: 1080 for Instagram)
ViewportH int // Viewport height (default: 1350 for Instagram)
Selector string // CSS selector to capture a specific element
Variables map[string]string // Variables for dynamic HTML
}
var DefaultViewport = CaptureOptions{
ViewportW: 375,
ViewportH: 667,
}
func detectInputType(input string) (isFile bool, isURL bool) {
// Check if the input is a valid URL
if strings.HasPrefix(input, "http://") || strings.HasPrefix(input, "https://") {
return false, true
}
// Check if the input ends with .html and if the file exists
if strings.HasSuffix(input, ".html") {
return true, false
}
// Otherwise, assume it's a raw HTML string
return false, false
}
func processDynamicHTML(htmlContent string, variables map[string]string) (string, error) {
tmpl, err := template.New("html").Parse(htmlContent)
if err != nil {
return "", fmt.Errorf("failed to parse template: %w", err)
}
var buf bytes.Buffer
err = tmpl.Execute(&buf, variables)
if err != nil {
return "", fmt.Errorf("failed to execute template: %w", err)
}
return buf.String(), nil
}
func Capture(opts CaptureOptions) ([]byte, error) {
if opts.Input == "" {
return nil, fmt.Errorf("input cannot be empty")
}
isFile, isURL := detectInputType(opts.Input)
var htmlContent string
if isFile {
if _, err := os.Stat(opts.Input); err != nil {
return nil, fmt.Errorf("invalid file path")
}
content, err := os.ReadFile(opts.Input) // Read file content
if err != nil {
return nil, fmt.Errorf("failed to read file: %w", err)
}
htmlContent = string(content)
} else if !isURL {
htmlContent = opts.Input
}
// ✅ Fix: Ensure file-based HTML is processed for dynamic content
if len(opts.Variables) > 0 {
processedHTML, err := processDynamicHTML(htmlContent, opts.Variables)
if err != nil {
return nil, fmt.Errorf("failed to process dynamic HTML: %w", err)
}
htmlContent = processedHTML
}
if opts.ViewportW == 0 {
opts.ViewportW = DefaultViewport.ViewportW
}
if opts.ViewportH == 0 {
opts.ViewportH = DefaultViewport.ViewportH
}
targetInput := opts.Input
if isFile || !isURL {
targetInput = "data:text/html;charset=utf-8," + htmlContent
}
imageData, err := browser.CaptureScreenshot(targetInput, isFile, opts.ViewportW, opts.ViewportH, opts.Selector)
if err != nil {
return nil, fmt.Errorf("failed to capture screenshot: %w", err)
}
img, err := imgutil.EnhanceQuality(imageData)
if err != nil {
return nil, fmt.Errorf("failed to capture screenshot: %w", err)
}
return img, nil
}