one-api/common/embed-file-system.go
seven.yu 91260bff65 refactor: common optimizations
1. rename GetOrDefault to GetOrDefaultInt
2. refactor method with variadic parameter declaration.
2024-01-10 20:34:07 +08:00

33 lines
547 B
Go

package common
import (
"embed"
"github.com/gin-contrib/static"
"io/fs"
"net/http"
)
// Credit: https://github.com/gin-contrib/static/issues/19
type embedFileSystem struct {
http.FileSystem
}
func (e embedFileSystem) Exists(prefix, path string) bool {
_, err := e.Open(path)
if err != nil {
return false
}
return true
}
func EmbedFolder(fsEmbed embed.FS, targetPath string) static.ServeFileSystem {
efs, err := fs.Sub(fsEmbed, targetPath)
if err != nil {
panic(err)
}
return embedFileSystem{
FileSystem: http.FS(efs),
}
}