feat: image variants engine
This commit is contained in:
1 parent
f0845de643
commit
f1d56ef50e
80 files changed
+7312
-84
No files matched your search
@@ -0,0 +1,54 @@
|
||||
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
|
||||
}
|
||||
Reference in new issue
Block a user