mirror of
https://github.com/derfenix/webarchive.git
synced 2026-03-11 12:41:54 +03:00
Refactoring
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/cookiejar"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"golang.org/x/net/html"
|
||||
@@ -14,6 +15,8 @@ import (
|
||||
"github.com/derfenix/webarchive/entity"
|
||||
)
|
||||
|
||||
const defaultEncoding = "utf-8"
|
||||
|
||||
type processor interface {
|
||||
Process(ctx context.Context, url string) ([]entity.File, error)
|
||||
}
|
||||
@@ -128,6 +131,7 @@ func (p *Processors) GetMeta(ctx context.Context, url string) (entity.Meta, erro
|
||||
|
||||
meta := entity.Meta{}
|
||||
getMetaData(htmlNode, &meta)
|
||||
meta.Encoding = encodingFromHeader(response.Header)
|
||||
|
||||
return meta, nil
|
||||
}
|
||||
@@ -156,3 +160,19 @@ func getMetaData(n *html.Node, meta *entity.Meta) {
|
||||
getMetaData(c, meta)
|
||||
}
|
||||
}
|
||||
|
||||
func encodingFromHeader(headers http.Header) string {
|
||||
var foundEncoding bool
|
||||
var encoding string
|
||||
|
||||
_, encoding, foundEncoding = strings.Cut(headers.Get("Content-Type"), "; ")
|
||||
if foundEncoding {
|
||||
_, encoding, foundEncoding = strings.Cut(encoding, "=")
|
||||
}
|
||||
|
||||
if !foundEncoding {
|
||||
encoding = defaultEncoding
|
||||
}
|
||||
|
||||
return encoding
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user