Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
admpub committed Feb 19, 2024
1 parent 1d47289 commit 9e5587a
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 19 deletions.
3 changes: 3 additions & 0 deletions application/cmd/bootconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@ package bootconfig

import (
"net/http"
"time"

"github.com/webx-top/com"
"github.com/webx-top/echo"
"github.com/webx-top/echo/middleware/render/driver"
)

var (
Bindata bool
StaticMW interface{}
HTTPCacheMaxAge = com.GetenvDuration(`NGING_HTTPCACHE_MAXAGE`, time.Hour*24*7)
BackendTmplMgr driver.Manager
FrontendTmplMgr driver.Manager
LangFSFunc func(dir string) http.FileSystem
Expand Down
24 changes: 14 additions & 10 deletions application/handler/manager/file/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/webx-top/echo"

"github.com/admpub/nging/v5/application/cmd/bootconfig"
uploadLibrary "github.com/admpub/nging/v5/application/library/upload"
"github.com/admpub/nging/v5/application/registry/upload"
"github.com/admpub/nging/v5/application/registry/upload/convert"
Expand All @@ -32,44 +33,47 @@ func File(ctx echo.Context) error {
}
}
file = filepath.Join(uploadLibrary.UploadDir, subdir, file)
maxAge := bootconfig.HTTPCacheMaxAge
if convert.CountConverters() < 1 {
return ctx.CacheableFile(file, maxAge)
}
var (
convertFunc convert.Convert
ok bool
originalFile string
)
extension := ctx.Query(`ex`)
if len(extension) > 0 {
extension = `.` + extension
if extension[0] != '.' {
extension = `.` + extension
}
convertFunc, ok = convert.GetConverter(extension)
if !ok {
return ctx.File(file)
return ctx.CacheableFile(file, maxAge)
}
originalFile = file
} else {
originalExtension := filepath.Ext(file)
originalExtension := filepath.Ext(file) // file.jpg.png
extension = strings.ToLower(originalExtension)
convertFunc, ok = convert.GetConverter(extension)
if !ok {
return ctx.File(file)
return ctx.CacheableFile(file, maxAge)
}
originalFile = strings.TrimSuffix(file, originalExtension)
index := strings.LastIndex(originalFile, `.`)
// 单扩展名或相同扩展名的情况下不转换格式
if index < 0 || strings.ToLower(originalFile[index:]) == extension {
return ctx.File(originalFile)
return ctx.CacheableFile(originalFile, maxAge)
}
}
supported := strings.Contains(ctx.Header(echo.HeaderAccept), "image/"+strings.TrimPrefix(extension, `.`))
if !supported {
return ctx.File(originalFile)
return ctx.CacheableFile(originalFile, maxAge)
}

fileGeneratorLock.RLock()
if err := ctx.File(file); err != echo.ErrNotFound {
fileGeneratorLock.RUnlock()
if err := ctx.CacheableFile(file, maxAge); err != echo.ErrNotFound {
return err
}
fileGeneratorLock.RUnlock()

fileGeneratorLock.Lock()
defer fileGeneratorLock.Unlock()
Expand Down
4 changes: 2 additions & 2 deletions application/library/bindata/bindata_nging.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ func Initialize() {
FrontendTmplAssetFS = NewAssetFS(FrontendTmplAssetPrefix)
}
}
bootconfig.StaticMW = bindata.Static("/public/assets/", StaticAssetFS)
bootconfig.StaticMW = bindata.Static("/public/assets/", StaticAssetFS, bootconfig.HTTPCacheMaxAge)
bootconfig.FaviconHandler = func(c echo.Context) error {
return c.File(bootconfig.FaviconPath, StaticAssetFS)
return c.CacheableFile(bootconfig.FaviconPath, bootconfig.HTTPCacheMaxAge, StaticAssetFS)
}
bootconfig.BackendTmplMgr = bindata.NewTmplManager(BackendTmplAssetFS)
if BackendTmplAssetFS == FrontendTmplAssetFS {
Expand Down
3 changes: 2 additions & 1 deletion application/library/bindata/bindata_none.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ var StaticOptions = &middleware.StaticOptions{
Root: "",
Path: "/public/assets/",
Fallback: []string{},
MaxAge: bootconfig.HTTPCacheMaxAge,
}

var PathAliases = ntemplate.NewPathAliases()
Expand All @@ -60,7 +61,7 @@ func Initialize() {
log.Error(`not found favicon file: ` + bootconfig.FaviconPath)
}
bootconfig.FaviconHandler = func(c echo.Context) error {
return c.File(bootconfig.FaviconPath)
return c.CacheableFile(bootconfig.FaviconPath, bootconfig.HTTPCacheMaxAge)
}
image.WatermarkOpen = func(file string) (image.FileReader, error) {
f, err := image.DefaultHTTPSystemOpen(file)
Expand Down
4 changes: 4 additions & 0 deletions application/registry/upload/convert/converters.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ func GetConverter(extension string) (Convert, bool) {
return convert, ok
}

func CountConverters() int {
return len(formatConverters)
}

// Unregister 取消注册
func Unregister(extension string) {
if _, ok := formatConverters[extension]; ok {
Expand Down
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ exclude github.com/gomodule/redigo v2.0.0+incompatible

// replace github.com/admpub/web-terminal => ../../admpub/web-terminal

// replace github.com/webx-top/echo => ../../webx-top/echo

require (
gitee.com/admpub/certmagic v0.8.8
github.com/abh/errorutil v1.0.0
Expand Down Expand Up @@ -112,7 +114,7 @@ require (
github.com/webx-top/codec v0.3.0
github.com/webx-top/com v1.2.12
github.com/webx-top/db v1.27.5
github.com/webx-top/echo v1.6.1
github.com/webx-top/echo v1.6.2
github.com/webx-top/image v0.1.0
github.com/webx-top/pagination v0.2.8 // indirect
golang.org/x/crypto v0.19.0
Expand Down Expand Up @@ -199,7 +201,7 @@ require (
github.com/bitly/go-simplejson v0.5.1 // indirect
github.com/blang/semver v3.5.1+incompatible // indirect
github.com/boombuler/barcode v1.0.1 // indirect
github.com/caddy-plugins/caddy-expires v1.1.2 // indirect
github.com/caddy-plugins/caddy-expires v1.1.3 // indirect
github.com/caddy-plugins/caddy-filter v0.15.2 // indirect
github.com/caddy-plugins/caddy-locale v0.0.2 // indirect
github.com/caddy-plugins/caddy-prometheus v0.1.0 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1306,8 +1306,8 @@ github.com/bwesterb/go-ristretto v1.2.0/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7N
github.com/bwesterb/go-ristretto v1.2.3/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0=
github.com/c-bata/go-prompt v0.2.5/go.mod h1:vFnjEGDIIA/Lib7giyE4E9c50Lvl8j0S+7FVlAwDAVw=
github.com/c2h5oh/datasize v0.0.0-20200112174442-28bbd4740fee/go.mod h1:S/7n9copUssQ56c7aAgHqftWO4LTf4xY6CGWt8Bc+3M=
github.com/caddy-plugins/caddy-expires v1.1.2 h1:x9yDVyThpskbzm33PuIVnKpQHgkj154oAjz213xv8qY=
github.com/caddy-plugins/caddy-expires v1.1.2/go.mod h1:ykVSc50CjCAqMPlTvxUlKA8L49UiClLUQm9VO7T9OlA=
github.com/caddy-plugins/caddy-expires v1.1.3 h1:jdkGqAKf5XQ1U5fUCTTo2s1oCtqd7BSG/+gmVjUzlqw=
github.com/caddy-plugins/caddy-expires v1.1.3/go.mod h1:ykVSc50CjCAqMPlTvxUlKA8L49UiClLUQm9VO7T9OlA=
github.com/caddy-plugins/caddy-filter v0.15.2 h1:HD6VwXVuHTeH0wMz4hzE/tYBRm+Mncpx9Ag3xilyqDE=
github.com/caddy-plugins/caddy-filter v0.15.2/go.mod h1:IjTyzAPq5yL80F6gCmxDzm2ZxHDX/RaqcXpMuc+4F8c=
github.com/caddy-plugins/caddy-locale v0.0.2 h1:9nEffdymHRL3WPmW7vHoZPtBbzMCzOcLbwwNjV6cUJo=
Expand Down Expand Up @@ -2746,8 +2746,8 @@ github.com/webx-top/com v1.2.12 h1:B09zf/DC88k+4MlLd9eOa4Gxh2fKhJ765NM5TH/p6Gs=
github.com/webx-top/com v1.2.12/go.mod h1:DDfATzu1w5+vD5XmG3YRTfLjaIqZWi/yeJ7HQEGsM2Q=
github.com/webx-top/db v1.27.5 h1:EW8lSGAWf6p/iZIxCxfu+jdvc4tyi9/TU7tgmCXj7OI=
github.com/webx-top/db v1.27.5/go.mod h1:wsRnkSTNJGQfM2fDNcusft460Xt+jIQPMqHQCExJ/oQ=
github.com/webx-top/echo v1.6.1 h1:e6t3J/htTbHLJ5s6rQZMu6zedDnNjSOk106qOMSUBjM=
github.com/webx-top/echo v1.6.1/go.mod h1:kQWELYp4RI5HE4JNt4kxhfWw9TZpYUXDItcZIs/9E2U=
github.com/webx-top/echo v1.6.2 h1:Wwxj9EV5pw6/MZm//rwwmbDoKQnzOjUMbuQ+dHV0R3s=
github.com/webx-top/echo v1.6.2/go.mod h1:kQWELYp4RI5HE4JNt4kxhfWw9TZpYUXDItcZIs/9E2U=
github.com/webx-top/echo-prometheus v1.1.2 h1:llHHZFWBxb/n0W2Tnn6UjnrOVNi/YQ+v2ETyOZ1248w=
github.com/webx-top/echo-prometheus v1.1.2/go.mod h1:8QYzHq8JjzNGKpRw4q4vopq/zcA87Flwqc4oNXVu2gk=
github.com/webx-top/image v0.1.0 h1:c01ZtTP3yjwok7nXzjm6pS6zL50z9m/9ax6D/iwfSJc=
Expand Down

0 comments on commit 9e5587a

Please sign in to comment.