Files
new-api/common/embed-file-system.go
coderabbitai[bot] 4e9cc4a36c 📝 Add docstrings to hotfix/analytic
Docstrings generation was requested by @wzxjohn.

* https://github.com/QuantumNous/new-api/pull/2133#issuecomment-3484993715

The following files were modified:

* `common/embed-file-system.go`
2025-11-04 09:56:24 +00:00

44 lines
883 B
Go

package common
import (
"embed"
"io/fs"
"net/http"
"os"
"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)
if err != nil {
return false
}
return true
}
func (e *embedFileSystem) Open(name string) (http.File, error) {
if name == "/" {
// This will make sure the index page goes to NoRouter handler,
// which will use the replaced index bytes with analytic codes.
return nil, os.ErrNotExist
}
return e.FileSystem.Open(name)
}
// requested subtree cannot be opened.
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),
}
}