55 lines
1.2 KiB
Go
55 lines
1.2 KiB
Go
package image
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"errors"
|
|
"strings"
|
|
)
|
|
|
|
const etagDigestLength = 32
|
|
|
|
type VariantRequestError struct {
|
|
cause error
|
|
Detail string
|
|
}
|
|
|
|
func NewUnknownVariantError(name string) error {
|
|
return VariantRequestError{cause: ErrUnknownVariant, Detail: name}
|
|
}
|
|
|
|
func NewUnsupportedParameterError(parameter string) error {
|
|
return VariantRequestError{cause: ErrUnsupportedParameter, Detail: parameter}
|
|
}
|
|
|
|
func (e VariantRequestError) Error() string {
|
|
return e.cause.Error() + ": " + e.Detail
|
|
}
|
|
|
|
func (e VariantRequestError) Unwrap() error {
|
|
return e.cause
|
|
}
|
|
|
|
func VariantRequestDetail(err error) string {
|
|
var requestError VariantRequestError
|
|
if errors.As(err, &requestError) {
|
|
return requestError.Detail
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func VariantETag(settings VariantSettings, sourceName string) string {
|
|
digest := sha256.Sum256([]byte(settings.Fingerprint() + "|" + sourceName))
|
|
return `"` + hex.EncodeToString(digest[:])[:etagDigestLength] + `"`
|
|
}
|
|
|
|
func ETagMatches(ifNoneMatch string, etag string) bool {
|
|
for _, candidate := range strings.Split(ifNoneMatch, ",") {
|
|
trimmed := strings.TrimPrefix(strings.TrimSpace(candidate), "W/")
|
|
if trimmed == "*" || trimmed == etag {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|