Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
admpub committed Jun 18, 2024
1 parent c69492a commit c94def5
Showing 1 changed file with 29 additions and 11 deletions.
40 changes: 29 additions & 11 deletions middleware/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"path"
"path/filepath"
"strings"
"time"

"github.com/admpub/log"
Expand Down Expand Up @@ -36,15 +37,16 @@ type (
// Skipper defines a function to skip middleware.
Skipper echo.Skipper `json:"-"`

Path string `json:"path"` //UrlPath
Root string `json:"root"`
Fallback []string `json:"fallback"`
Index string `json:"index"`
Browse bool `json:"browse"`
Template string `json:"template"`
Debug bool `json:"debug"`
FS http.FileSystem `json:"-"`
MaxAge time.Duration `json:"maxAge"`
Path string `json:"path"` //UrlPath
Root string `json:"root"`
Fallback []string `json:"fallback"`
Index string `json:"index"`
Browse bool `json:"browse"`
Template string `json:"template"`
Debug bool `json:"debug"`
FS http.FileSystem `json:"-"`
MaxAge time.Duration `json:"maxAge"`
TrimPrefix string `json:"trimPrefix"`

open func(string) (http.File, error)
render func(echo.Context, interface{}) error
Expand Down Expand Up @@ -78,8 +80,21 @@ func (s *StaticOptions) Init() *StaticOptions {
log.GetLogger("echo").Debug(`[middleware][static] `, `Register assets directory: `, fallback)
}
}
if len(s.Path) > 0 && s.Path[0] != '/' {
s.Path = `/` + s.Path
if len(s.Path) > 0 {
if s.Path[len(s.Path)-1] != '/' {
s.Path = s.Path + `/`
}
if s.Path[0] != '/' {
s.Path = `/` + s.Path
}
}
if len(s.TrimPrefix) > 0 {
if s.TrimPrefix[len(s.TrimPrefix)-1] != '/' {
s.TrimPrefix = s.TrimPrefix + `/`
}
if s.TrimPrefix[0] != '/' {
s.TrimPrefix = `/` + s.TrimPrefix
}
}
if s.Debug {
log.GetLogger("echo").Debug(`[middleware][static] `, `Static: `, s.Path, "\t-> ", s.Root)
Expand Down Expand Up @@ -207,6 +222,9 @@ func (s *StaticOptions) Middleware() echo.MiddlewareFunc {
}
file = file[length:]
file = path.Clean(file)
if len(s.TrimPrefix) > 0 {
file = strings.TrimPrefix(file, s.TrimPrefix)
}
}
err := s.findFile(c, s.Root, hasIndex, file, render, opener)
if err == nil {
Expand Down

0 comments on commit c94def5

Please sign in to comment.