优化结构

This commit is contained in:
Yvan
2026-08-24 13:25:16 +08:00
parent 7a9a738c6a
commit 0b6b8ddf1a
72 changed files with 2475 additions and 1443 deletions
+11 -21
View File
@@ -9,14 +9,15 @@ import (
"path"
"strings"
"kra/internal/conf"
"kra/internal/config"
"kra/internal/utils/uploadpolicy"
"github.com/gin-gonic/gin"
)
// Register adds local upload routes when local storage is enabled. A route is
// skipped when its root conflicts with an existing GET/HEAD route.
func Register(engine *gin.Engine, runtime *conf.Runtime) {
func Register(engine *gin.Engine, runtime *config.Store) {
config := localConfig(runtime)
if config == nil || strings.Trim(config.PathPrefix, "/") == "" {
return
@@ -36,7 +37,7 @@ func Register(engine *gin.Engine, runtime *conf.Runtime) {
// Serve resolves the current runtime configuration on every request so a
// configuration reload takes effect without rebuilding the Gin engine.
func Serve(c *gin.Context, runtime *conf.Runtime) bool {
func Serve(c *gin.Context, runtime *config.Store) bool {
config := localConfig(runtime)
if config == nil {
return false
@@ -45,18 +46,18 @@ func Serve(c *gin.Context, runtime *conf.Runtime) bool {
return serveAt(c, runtime, prefix)
}
func localConfig(runtime *conf.Runtime) *conf.AdminBackend_Local {
func localConfig(runtime *config.Store) *config.Local {
if runtime == nil {
return nil
}
config := runtime.Admin()
if config == nil || config.Local == nil || config.Local.StorePath == "" {
snapshot := runtime.Snapshot()
if snapshot == nil || snapshot.Admin == nil || snapshot.Admin.Local == nil || snapshot.Admin.Local.StorePath == "" {
return nil
}
if config.Storage != nil && config.Storage.Type != "" && config.Storage.Type != "local" {
if snapshot.Admin.Storage != nil && snapshot.Admin.Storage.Type != "" && snapshot.Admin.Storage.Type != "local" {
return nil
}
return config.Local
return snapshot.Admin.Local
}
func localStorageRouteConflicts(routes []gin.RouteInfo, prefix string) bool {
@@ -73,7 +74,7 @@ func localStorageRouteConflicts(routes []gin.RouteInfo, prefix string) bool {
return false
}
func serveAt(c *gin.Context, runtime *conf.Runtime, prefix string) bool {
func serveAt(c *gin.Context, runtime *config.Store, prefix string) bool {
config := localConfig(runtime)
if config == nil || "/"+strings.Trim(config.PathPrefix, "/") != prefix {
return false
@@ -83,24 +84,13 @@ func serveAt(c *gin.Context, runtime *conf.Runtime, prefix string) bool {
}
c.Header("X-Content-Type-Options", "nosniff")
filename := path.Base(c.Request.URL.Path)
if !canServeInline(filename) {
if !uploadpolicy.CanServeInline(filename) {
c.Header("Content-Disposition", mime.FormatMediaType("attachment", map[string]string{"filename": filename}))
}
http.StripPrefix(prefix, http.FileServer(filesOnly{FileSystem: http.Dir(config.StorePath)})).ServeHTTP(c.Writer, c.Request)
return true
}
func canServeInline(filename string) bool {
switch strings.ToLower(path.Ext(filename)) {
case ".jpg", ".jpeg", ".png", ".gif", ".webp", ".bmp", ".ico", ".avif",
".mp3", ".wav", ".ogg", ".m4a", ".flac", ".aac",
".mp4", ".webm", ".mov", ".avi", ".mkv":
return true
default:
return false
}
}
type filesOnly struct{ http.FileSystem }
func (f filesOnly) Open(name string) (http.File, error) {