mirror of
https://github.com/kataras/iris.git
synced 2026-01-07 12:07:28 +00:00
add support for the go standard embed tag for locale files
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"path"
|
||||
)
|
||||
|
||||
// ResolveFS accepts a single input argument of any type
|
||||
@@ -44,3 +45,67 @@ var ResolveFS = func(fsOrDir interface{}) http.FileSystem {
|
||||
|
||||
return fileSystem
|
||||
}
|
||||
|
||||
// FindNames accepts a "http.FileSystem" and a root name and returns
|
||||
// the list containg its file names.
|
||||
func FindNames(fileSystem http.FileSystem, name string) ([]string, error) {
|
||||
f, err := fileSystem.Open(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
fi, err := f.Stat()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !fi.IsDir() {
|
||||
return []string{name}, nil
|
||||
}
|
||||
|
||||
fileinfos, err := f.Readdir(-1)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
files := make([]string, 0)
|
||||
|
||||
for _, info := range fileinfos {
|
||||
// Note:
|
||||
// go-bindata has absolute names with os.Separator,
|
||||
// http.Dir the basename.
|
||||
filename := toBaseName(info.Name())
|
||||
fullname := path.Join(name, filename)
|
||||
if fullname == name { // prevent looping through itself.
|
||||
continue
|
||||
}
|
||||
rfiles, err := FindNames(fileSystem, fullname)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
files = append(files, rfiles...)
|
||||
}
|
||||
|
||||
return files, nil
|
||||
}
|
||||
|
||||
// Instead of path.Base(filepath.ToSlash(s))
|
||||
// let's do something like that, it is faster
|
||||
// (used to list directories on serve-time too):
|
||||
func toBaseName(s string) string {
|
||||
n := len(s) - 1
|
||||
for i := n; i >= 0; i-- {
|
||||
if c := s[i]; c == '/' || c == '\\' {
|
||||
if i == n {
|
||||
// "s" ends with a slash, remove it and retry.
|
||||
return toBaseName(s[:n])
|
||||
}
|
||||
|
||||
return s[i+1:] // return the rest, trimming the slash.
|
||||
}
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user