ai-gateway/common/embed-file-system.go
2024-08-06 23:54:49 +08:00

32 lines
584 B
Go

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