Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
derfenix committed Apr 15, 2023
1 parent e1fbfe0 commit 1f3e5ec
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions adapters/processors/processors.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net"
"net/http"
"net/http/cookiejar"
"strings"
"time"

"golang.org/x/net/html"
Expand All @@ -14,6 +15,8 @@ import (
"github.com/derfenix/webarchive/entity"
)

const defaultEncoding = "utf-8"

Check failure on line 18 in adapters/processors/processors.go

View workflow job for this annotation

GitHub Actions / test

other declaration of defaultEncoding

type processor interface {
Process(ctx context.Context, url string) ([]entity.File, error)
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}

0 comments on commit 1f3e5ec

Please sign in to comment.