one-api/common/embed-file-system.go
cktsun1031 fa008e7196 Refactor and optimize code: streamline error
handling, remove redundant return statements,
correct gorm tag syntax, and improve type
assertions in relay.go
2023-11-06 16:04:13 +08:00

31 lines
526 B
Go

package common
import (
"embed"
"io/fs"
"net/http"
"github.com/gin-contrib/static"
)
// Credit: https://github.com/gin-contrib/static/issues/19
type embedFileSystem struct {
http.FileSystem
}
func (e embedFileSystem) Exists(prefix string, path string) bool {
_, err := e.Open(path)
return err == nil
}
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),
}
}