支持自动转换图像文件为WebP

This commit is contained in:
刘祥超
2021-10-01 16:25:31 +08:00
parent 702c631460
commit f01a45a746
5 changed files with 640 additions and 345 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -22,6 +22,9 @@ service HTTPWebService {
// 更改压缩配置 // 更改压缩配置
rpc updateHTTPWebCompression (UpdateHTTPWebCompressionRequest) returns (RPCSuccess); rpc updateHTTPWebCompression (UpdateHTTPWebCompressionRequest) returns (RPCSuccess);
// 更改WebP配置
rpc updateHTTPWebWebP (UpdateHTTPWebWebPRequest) returns (RPCSuccess);
// 更改字符集配置 // 更改字符集配置
rpc updateHTTPWebCharset (UpdateHTTPWebCharsetRequest) returns (RPCSuccess); rpc updateHTTPWebCharset (UpdateHTTPWebCharsetRequest) returns (RPCSuccess);
@@ -113,6 +116,12 @@ message UpdateHTTPWebCompressionRequest {
bytes compressionJSON = 2; bytes compressionJSON = 2;
} }
// 更改WebP配置
message UpdateHTTPWebWebPRequest {
int64 webId = 1;
bytes webpJSON = 2;
}
// 更改字符集配置 // 更改字符集配置
message UpdateHTTPWebCharsetRequest { message UpdateHTTPWebCharsetRequest {
int64 webId = 1; int64 webId = 1;

View File

@@ -6,13 +6,12 @@ import (
) )
type HTTPWebConfig struct { type HTTPWebConfig struct {
Id int64 `yaml:"id" json:"id"` // ID Id int64 `yaml:"id" json:"id"` // ID
IsOn bool `yaml:"isOn" json:"isOn"` // 是否启用 IsOn bool `yaml:"isOn" json:"isOn"` // 是否启用
Locations []*HTTPLocationConfig `yaml:"locations" json:"locations"` // 路径规则 TODO Locations []*HTTPLocationConfig `yaml:"locations" json:"locations"` // 路径规则 TODO
LocationRefs []*HTTPLocationRef `yaml:"locationRefs" json:"locationRefs"` // 路径规则应用 LocationRefs []*HTTPLocationRef `yaml:"locationRefs" json:"locationRefs"` // 路径规则应用
//GzipRef *HTTPGzipRef `yaml:"gzipRef" json:"gzipRef"` // Gzip引用 Compression *HTTPCompressionConfig `yaml:"compression" json:"compression"` // 压缩配置
//Gzip *HTTPCompressGzipConfig `yaml:"gzip" json:"gzip"` // Gzip配置 WebP *WebPImageConfig `yaml:"webp" json:"webp"` // WebP配置
Compression *HTTPCompressionConfig `yaml:"compression" json:"compression"` //
Charset *HTTPCharsetConfig `yaml:"charset" json:"charset"` // 字符编码 Charset *HTTPCharsetConfig `yaml:"charset" json:"charset"` // 字符编码
Shutdown *HTTPShutdownConfig `yaml:"shutdown" json:"shutdown"` // 临时关闭配置 Shutdown *HTTPShutdownConfig `yaml:"shutdown" json:"shutdown"` // 临时关闭配置
Pages []*HTTPPageConfig `yaml:"pages" json:"pages"` // 特殊页面配置 Pages []*HTTPPageConfig `yaml:"pages" json:"pages"` // 特殊页面配置
@@ -234,6 +233,14 @@ func (this *HTTPWebConfig) Init() error {
} }
} }
// webp
if this.WebP != nil {
err := this.WebP.Init()
if err != nil {
return err
}
}
return nil return nil
} }

View File

@@ -0,0 +1,140 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package serverconfigs
import (
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
"strings"
)
// WebPImageConfig WebP配置
type WebPImageConfig struct {
IsPrior bool `yaml:"isPrior" json:"isPrior"`
IsOn bool `yaml:"isOn" json:"isOn"`
Quality int `yaml:"quality" json:"quality"` // 0-100
MinLength *shared.SizeCapacity `yaml:"minLength" json:"minLength"` // 最小压缩对象比如4m, 24k
MaxLength *shared.SizeCapacity `yaml:"maxLength" json:"maxLength"` // 最大压缩对象
MimeTypes []string `yaml:"mimeTypes" json:"mimeTypes"` // 支持的MimeType支持image/*这样的通配符使用
Extensions []string `yaml:"extensions" json:"extensions"` // 文件扩展名,包含点符号,不区分大小写
Conds *shared.HTTPRequestCondsConfig `yaml:"conds" json:"conds"` // 匹配条件
minLength int64
maxLength int64
mimeTypeRules []*shared.MimeTypeRule
extensions []string
}
func (this *WebPImageConfig) Init() error {
if this.MinLength != nil {
this.minLength = this.MinLength.Bytes()
}
if this.MaxLength != nil {
this.maxLength = this.MaxLength.Bytes()
}
if this.Conds != nil {
err := this.Conds.Init()
if err != nil {
return err
}
}
// mime types
this.mimeTypeRules = []*shared.MimeTypeRule{}
for _, mimeType := range this.MimeTypes {
rule, err := shared.NewMimeTypeRule(mimeType)
if err != nil {
return err
}
this.mimeTypeRules = append(this.mimeTypeRules, rule)
}
// extensions
this.extensions = []string{}
for _, ext := range this.Extensions {
ext = strings.ToLower(ext)
if len(ext) > 0 && ext[0] != '.' {
ext = "." + ext
}
this.extensions = append(this.extensions, ext)
}
return nil
}
// MatchResponse 是否匹配响应
func (this *WebPImageConfig) MatchResponse(mimeType string, contentLength int64, requestExt string, formatter shared.Formatter) bool {
if this.Conds != nil && formatter != nil {
if !this.Conds.MatchRequest(formatter) {
return false
}
if !this.Conds.MatchResponse(formatter) {
return false
}
}
// min length
if this.minLength > 0 && contentLength < this.minLength {
return false
}
// max length
if this.maxLength > 0 && contentLength > this.maxLength {
return false
}
// extensions
if len(this.extensions) > 0 {
if len(requestExt) > 0 {
for _, ext := range this.extensions {
if ext == requestExt {
if strings.Contains(mimeType, "image/") {
return true
}
}
}
}
}
// mime types
if len(this.mimeTypeRules) > 0 {
if len(mimeType) > 0 {
var index = strings.Index(mimeType, ";")
if index >= 0 {
mimeType = mimeType[:index]
}
for _, rule := range this.mimeTypeRules {
if rule.Match(mimeType) {
return true
}
}
}
}
// 如果没有指定条件,则所有的都能压缩
if len(this.extensions) == 0 && len(this.mimeTypeRules) == 0 {
return true
}
return false
}
// MatchAccept 检查客户端是否能接受WebP
func (this *WebPImageConfig) MatchAccept(acceptContentTypes string) bool {
var t = "image/webp"
var index = strings.Index(acceptContentTypes, t)
if index < 0 {
return false
}
var l = len(acceptContentTypes)
if index > 0 && acceptContentTypes[index-1] != ',' {
return false
}
if index+len(t) < l && acceptContentTypes[index+len(t)] != ',' {
return false
}
return true
}

View File

@@ -0,0 +1,21 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package serverconfigs
import (
"github.com/iwind/TeaGo/assert"
"testing"
)
func TestWebPImageConfig_MatchAccept(t *testing.T) {
var a = assert.NewAssertion(t)
{
var c = &WebPImageConfig{}
a.IsFalse(c.MatchAccept(""))
a.IsTrue(c.MatchAccept("image/webp"))
a.IsTrue(c.MatchAccept("image/webp,image/png"))
a.IsTrue(c.MatchAccept("image/jpeg,image/webp,image/png"))
a.IsFalse(c.MatchAccept("mimage/webp"))
a.IsFalse(c.MatchAccept("image/webpm"))
}
}