| package common
|
|
|
| import (
|
| "embed"
|
| "io/fs"
|
| "net/http"
|
| "os"
|
|
|
| "github.com/gin-contrib/static"
|
| )
|
|
|
|
|
|
|
| type embedFileSystem struct {
|
| http.FileSystem
|
| }
|
|
|
| func (e *embedFileSystem) Exists(prefix string, path string) bool {
|
| _, err := e.Open(path)
|
| if err != nil {
|
| return false
|
| }
|
| return true
|
| }
|
|
|
| func (e *embedFileSystem) Open(name string) (http.File, error) {
|
| if name == "/" {
|
|
|
|
|
| return nil, os.ErrNotExist
|
| }
|
| return e.FileSystem.Open(name)
|
| }
|
|
|
| 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),
|
| }
|
| }
|
|
|