gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

options.go (2397B)


      1 package gzip
      2 
      3 import (
      4 	"compress/gzip"
      5 	"net/http"
      6 	"regexp"
      7 	"strings"
      8 
      9 	"github.com/gin-gonic/gin"
     10 )
     11 
     12 var (
     13 	DefaultExcludedExtentions = NewExcludedExtensions([]string{
     14 		".png", ".gif", ".jpeg", ".jpg",
     15 	})
     16 	DefaultOptions = &Options{
     17 		ExcludedExtensions: DefaultExcludedExtentions,
     18 	}
     19 )
     20 
     21 type Options struct {
     22 	ExcludedExtensions   ExcludedExtensions
     23 	ExcludedPaths        ExcludedPaths
     24 	ExcludedPathesRegexs ExcludedPathesRegexs
     25 	DecompressFn         func(c *gin.Context)
     26 }
     27 
     28 type Option func(*Options)
     29 
     30 func WithExcludedExtensions(args []string) Option {
     31 	return func(o *Options) {
     32 		o.ExcludedExtensions = NewExcludedExtensions(args)
     33 	}
     34 }
     35 
     36 func WithExcludedPaths(args []string) Option {
     37 	return func(o *Options) {
     38 		o.ExcludedPaths = NewExcludedPaths(args)
     39 	}
     40 }
     41 
     42 func WithExcludedPathsRegexs(args []string) Option {
     43 	return func(o *Options) {
     44 		o.ExcludedPathesRegexs = NewExcludedPathesRegexs(args)
     45 	}
     46 }
     47 
     48 func WithDecompressFn(decompressFn func(c *gin.Context)) Option {
     49 	return func(o *Options) {
     50 		o.DecompressFn = decompressFn
     51 	}
     52 }
     53 
     54 // Using map for better lookup performance
     55 type ExcludedExtensions map[string]bool
     56 
     57 func NewExcludedExtensions(extensions []string) ExcludedExtensions {
     58 	res := make(ExcludedExtensions)
     59 	for _, e := range extensions {
     60 		res[e] = true
     61 	}
     62 	return res
     63 }
     64 
     65 func (e ExcludedExtensions) Contains(target string) bool {
     66 	_, ok := e[target]
     67 	return ok
     68 }
     69 
     70 type ExcludedPaths []string
     71 
     72 func NewExcludedPaths(paths []string) ExcludedPaths {
     73 	return ExcludedPaths(paths)
     74 }
     75 
     76 func (e ExcludedPaths) Contains(requestURI string) bool {
     77 	for _, path := range e {
     78 		if strings.HasPrefix(requestURI, path) {
     79 			return true
     80 		}
     81 	}
     82 	return false
     83 }
     84 
     85 type ExcludedPathesRegexs []*regexp.Regexp
     86 
     87 func NewExcludedPathesRegexs(regexs []string) ExcludedPathesRegexs {
     88 	result := make([]*regexp.Regexp, len(regexs))
     89 	for i, reg := range regexs {
     90 		result[i] = regexp.MustCompile(reg)
     91 	}
     92 	return result
     93 }
     94 
     95 func (e ExcludedPathesRegexs) Contains(requestURI string) bool {
     96 	for _, reg := range e {
     97 		if reg.MatchString(requestURI) {
     98 			return true
     99 		}
    100 	}
    101 	return false
    102 }
    103 
    104 func DefaultDecompressHandle(c *gin.Context) {
    105 	if c.Request.Body == nil {
    106 		return
    107 	}
    108 	r, err := gzip.NewReader(c.Request.Body)
    109 	if err != nil {
    110 		_ = c.AbortWithError(http.StatusBadRequest, err)
    111 		return
    112 	}
    113 	c.Request.Header.Del("Content-Encoding")
    114 	c.Request.Header.Del("Content-Length")
    115 	c.Request.Body = r
    116 }