85 lines
2.1 KiB
Go
85 lines
2.1 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
)
|
|
|
|
// BundleEntry is one tab in a bundle JSON file.
|
|
type BundleEntry struct {
|
|
Host string `json:"host"`
|
|
Title string `json:"title"`
|
|
Icon string `json:"icon"`
|
|
IconW int `json:"icon_w,omitempty"`
|
|
IconH int `json:"icon_h,omitempty"`
|
|
IframeOk bool `json:"iframe_ok"`
|
|
}
|
|
|
|
// Bundle is the top-level JSON structure.
|
|
type Bundle struct {
|
|
Entries []BundleEntry `json:"entries"`
|
|
}
|
|
|
|
// buildEntry creates a BundleEntry for a host, converting its icon if available.
|
|
func buildEntry(ctx context.Context, host HostRow, iconsBucket string, logWriter *LogWriter, stats *Stats) BundleEntry {
|
|
entry := BundleEntry{
|
|
Host: host.Hostname,
|
|
Title: host.HtmlTitle,
|
|
Icon: "",
|
|
IframeOk: host.IframeAllowed,
|
|
}
|
|
|
|
if host.BestIconS3Key == "" {
|
|
return entry
|
|
}
|
|
|
|
encoded, w, h, convertErr := safeConvert(ctx, host.BestIconS3Key, iconsBucket)
|
|
if convertErr != "" {
|
|
stats.ConvertErrors.Add(1)
|
|
logLine := fmt.Sprintf("CONVERT_ERROR: %s %s", host.Hostname, convertErr)
|
|
fmt.Println(logLine)
|
|
if logWriter != nil {
|
|
logWriter.Write(logLine, true)
|
|
}
|
|
return entry
|
|
}
|
|
|
|
entry.Icon = encoded
|
|
entry.IconW = w
|
|
entry.IconH = h
|
|
return entry
|
|
}
|
|
|
|
// safeConvert wraps convertIconToBase64PNG with panic recovery.
|
|
func safeConvert(ctx context.Context, s3Key, iconsBucket string) (encoded string, w, h int, errMsg string) {
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
errMsg = fmt.Sprintf("panic: %v", r)
|
|
}
|
|
}()
|
|
|
|
var err error
|
|
encoded, w, h, err = convertIconToBase64PNG(ctx, s3Key, iconsBucket)
|
|
if err != nil {
|
|
return "", 0, 0, err.Error()
|
|
}
|
|
return encoded, w, h, ""
|
|
}
|
|
|
|
func serializeBundle(entries []BundleEntry) ([]byte, error) {
|
|
bundle := Bundle{Entries: entries}
|
|
return json.Marshal(bundle)
|
|
}
|
|
|
|
func writeBundleLocal(outputDir string, index int, data []byte) error {
|
|
path := filepath.Join(outputDir, fmt.Sprintf("%04d.json", index))
|
|
return os.WriteFile(path, data, 0644)
|
|
}
|
|
|
|
func writeBundleS3(ctx context.Context, bucket string, index int, data []byte) error {
|
|
key := fmt.Sprintf("tabs/%04d.json", index)
|
|
return s3UploadBundle(ctx, bucket, key, data)
|
|
}
|