forked from mirror/pkger
troubled times
This commit is contained in:
parent
d453473dc0
commit
2e1702c1e5
|
@ -65,9 +65,22 @@ func NewFileInfo(info os.FileInfo) *FileInfo {
|
|||
}
|
||||
|
||||
func WithName(name string, info os.FileInfo) *FileInfo {
|
||||
if ft, ok := info.(*FileInfo); ok {
|
||||
ft.Details.Name = cleanName(name)
|
||||
return ft
|
||||
s := cleanName(name)
|
||||
|
||||
if !strings.HasPrefix(s, "/") {
|
||||
s = "/" + s
|
||||
}
|
||||
|
||||
fo := NewFileInfo(info)
|
||||
fo.Details.Name = cleanName(name)
|
||||
return fo
|
||||
}
|
||||
|
||||
func WithRelName(name string, info os.FileInfo) *FileInfo {
|
||||
s := cleanName(name)
|
||||
|
||||
if !strings.HasPrefix(s, "/") {
|
||||
s = "/" + s
|
||||
}
|
||||
|
||||
fo := NewFileInfo(info)
|
||||
|
@ -79,8 +92,5 @@ func cleanName(s string) string {
|
|||
if strings.Contains(s, "\\") {
|
||||
s = strings.Replace(s, "\\", "/", -1)
|
||||
}
|
||||
if !strings.HasPrefix(s, "/") {
|
||||
s = "/" + s
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
|
|
@ -151,11 +151,7 @@ func (f *File) Readdir(count int) ([]os.FileInfo, error) {
|
|||
return nil
|
||||
}
|
||||
|
||||
info = pkging.WithName(strings.TrimPrefix(info.Name(), f.parent.Name), info)
|
||||
if minf, ok := info.(*pkging.FileInfo); ok {
|
||||
minf.Details.Name = strings.TrimPrefix(info.Name(), "/")
|
||||
info = minf
|
||||
}
|
||||
info = pkging.WithRelName(strings.TrimPrefix(info.Name(), f.parent.Name), info)
|
||||
infos = append(infos, info)
|
||||
if info.IsDir() && path != root {
|
||||
return filepath.SkipDir
|
||||
|
|
|
@ -5,7 +5,6 @@ import (
|
|||
"net/http"
|
||||
"net/http/httptest"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/markbates/pkger/pkging/pkgutil"
|
||||
|
@ -64,9 +63,9 @@ func (s Suite) Test_HTTP_Dir(t *testing.T) {
|
|||
req string
|
||||
exp string
|
||||
}{
|
||||
{in: "/", req: "/", exp: `>/public/</a`},
|
||||
{in: ":" + "/", req: "/", exp: `>/public/</a`},
|
||||
{in: ip + ":" + "/", req: "/", exp: `>/public/</a`},
|
||||
{in: "/", req: "/", exp: `>public/</a`},
|
||||
{in: ":" + "/", req: "/", exp: `>public/</a`},
|
||||
{in: ip + ":" + "/", req: "/", exp: `>public/</a`},
|
||||
}
|
||||
|
||||
for _, tt := range table {
|
||||
|
@ -105,7 +104,7 @@ func (s Suite) Test_HTTP_Dir_IndexHTML(t *testing.T) {
|
|||
{in: ip + ":" + "/public", req: "/"},
|
||||
}
|
||||
|
||||
exp := "!/public/index.html"
|
||||
exp := "index.html"
|
||||
for _, tt := range table {
|
||||
t.Run(tt.in+exp, func(st *testing.T) {
|
||||
r := require.New(st)
|
||||
|
@ -123,8 +122,8 @@ func (s Suite) Test_HTTP_Dir_IndexHTML(t *testing.T) {
|
|||
b, err := ioutil.ReadAll(res.Body)
|
||||
r.NoError(err)
|
||||
|
||||
body := strings.TrimSpace(string(b))
|
||||
r.Equal(exp, body)
|
||||
body := string(b)
|
||||
r.Contains(body, exp)
|
||||
r.NotContains(body, "mark.png")
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue