MCPcopy Create free account
hub / github.com/GoEdgeLab/EdgeNode / PrepareWebP

Method PrepareWebP

internal/nodes/http_writer.go:512–588  ·  view source on GitHub ↗

PrepareWebP 准备WebP

(resp *http.Response, size int64)

Source from the content-addressed store, hash-verified

510
511// PrepareWebP 准备WebP
512func (this *HTTPWriter) PrepareWebP(resp *http.Response, size int64) {
513 if resp == nil {
514 return
515 }
516
517 // 集群配置
518 var policy = this.req.nodeConfig.FindWebPImagePolicyWithClusterId(this.req.ReqServer.ClusterId)
519 if policy == nil {
520 policy = nodeconfigs.DefaultWebPImagePolicy
521 }
522 if !policy.IsOn {
523 return
524 }
525
526 // 只有在开启了缓存之后,才会转换,防止占用的系统资源过高
527 if policy.RequireCache && this.req.cacheRef == nil {
528 return
529 }
530 this.webpQuality = policy.Quality
531
532 // 限制最小和最大尺寸
533 // TODO 需要将reader修改为LimitReader
534 if resp.ContentLength == 0 {
535 return
536 }
537
538 if resp.ContentLength > 0 && (resp.ContentLength < policy.MinLengthBytes() || (policy.MaxLengthBytes() > 0 && resp.ContentLength > policy.MaxLengthBytes())) {
539 return
540 }
541
542 var contentType = this.GetHeader("Content-Type")
543
544 if this.req.web != nil &&
545 this.req.web.WebP != nil &&
546 this.req.web.WebP.IsOn &&
547 this.req.web.WebP.MatchResponse(contentType, size, filepath.Ext(this.req.Path()), this.req.Format) &&
548 this.req.web.WebP.MatchAccept(this.req.requestHeader("Accept")) {
549 // 检查是否已经因为尺寸过大而忽略
550 if webPIgnoreURLSet.Has(this.req.URL()) {
551 return
552 }
553
554 // 如果已经是WebP不再重复处理
555 // TODO 考虑是否需要很严格的匹配
556 if strings.Contains(contentType, "image/webp") {
557 return
558 }
559
560 // 检查当前是否正在转换
561 if atomic.LoadInt32(&webPThreads) >= webPMaxThreads {
562 return
563 }
564
565 var contentEncoding = this.GetHeader("Content-Encoding")
566 if len(contentEncoding) > 0 {
567 if compressions.SupportEncoding(contentEncoding) {
568 reader, err := compressions.NewReader(resp.Body, contentEncoding)
569 if err != nil {

Callers 1

PrepareMethod · 0.95

Calls 11

GetHeaderMethod · 0.95
HeaderMethod · 0.95
SupportEncodingFunction · 0.92
NewReaderFunction · 0.92
requestHeaderMethod · 0.80
URLMethod · 0.80
MatchResponseMethod · 0.45
PathMethod · 0.45
HasMethod · 0.45
ContainsMethod · 0.45
SetMethod · 0.45

Tested by

no test coverage detected