From 233295be076408d042021f5fc7bf91b91eea8293 Mon Sep 17 00:00:00 2001 From: ynwcel <454383818@qq.com> Date: Wed, 18 Dec 2024 10:39:01 +0800 Subject: [PATCH] fix(os/gview): search file faild from resource manager of package gres (#4024) --- os/gview/gview_parse.go | 56 +++++++++++++++++++++++------------ os/gview/gview_z_unit_test.go | 24 +++++++++++++++ 2 files changed, 61 insertions(+), 19 deletions(-) diff --git a/os/gview/gview_parse.go b/os/gview/gview_parse.go index 243231872..0b20e7772 100644 --- a/os/gview/gview_parse.go +++ b/os/gview/gview_parse.go @@ -12,6 +12,7 @@ import ( "fmt" htmltpl "html/template" "strconv" + "strings" texttpl "text/template" "github.com/gogf/gf/v2/container/gmap" @@ -372,31 +373,48 @@ func (view *View) formatTemplateObjectCreatingError(filePath, tplName string, er // searchFile returns the absolute path of the `file` and its template folder path. // The returned `folder` is the template folder path, not the folder of the template file `path`. func (view *View) searchFile(ctx context.Context, file string) (path string, folder string, resource *gres.File, err error) { - var tempPath string - // Firstly, checking the resource manager. + var ( + tempPath string + trimmedFile = strings.TrimLeft(file, `\/`) + ) + // Firstly checking the resource manager. if !gres.IsEmpty() { - // Try folders. - for _, tryFolder := range resourceTryFolders { - tempPath = tryFolder + file - if resource = gres.Get(tempPath); resource != nil { - path = resource.Name() - folder = tryFolder - return - } - } // Search folders. - view.searchPaths.RLockFunc(func(array []string) { - for _, searchPath := range array { - for _, tryFolder := range resourceTryFolders { - tempPath = searchPath + tryFolder + file - if resFile := gres.Get(tempPath); resFile != nil { - path = resFile.Name() - folder = searchPath + tryFolder + if path == "" { + view.searchPaths.RLockFunc(func(array []string) { + for _, searchPath := range array { + tempPath = strings.TrimRight(searchPath, `\/`) + `/` + trimmedFile + if tmpFile := gres.Get(tempPath); tmpFile != nil { + path = tmpFile.Name() + folder = searchPath + resource = tmpFile return } + + for _, tryFolder := range resourceTryFolders { + tempPath = strings.TrimRight(searchPath, `\/`) + `/` + strings.TrimRight(tryFolder, `\/`) + `/` + file + if tmpFile := gres.Get(tempPath); tmpFile != nil { + path = tmpFile.Name() + folder = searchPath + tryFolder + resource = tmpFile + return + } + } + } + }) + } + // Try folders. + if path == "" { + for _, tryFolder := range resourceTryFolders { + tempPath = strings.TrimRight(tryFolder, `\/`) + `/` + trimmedFile + if tmpFile := gres.Get(tempPath); tmpFile != nil { + path = tmpFile.Name() + folder = tryFolder + resource = tmpFile + return } } - }) + } } // Secondly, checking the file system. diff --git a/os/gview/gview_z_unit_test.go b/os/gview/gview_z_unit_test.go index f345835ea..bdff3456f 100644 --- a/os/gview/gview_z_unit_test.go +++ b/os/gview/gview_z_unit_test.go @@ -621,6 +621,10 @@ func init() { if err := gres.Add("H4sIAAAAAAAC/wrwZmYRYeBg4GBIFA0LY0ACEgycDCWpuQU5iSWp+ullmanl8SWpxSV6GSW5OaEhrAyM5o1fk095n/HdumrdNeaLW7c2MDAw/P8f4M3OoZ+9QESIgYGBj4GBAWYBA0MTmgUcSBaADSxt/JoM0o6sKMCbkUmEGeFCZKNBLoSBbY0gkqB7EcZhdw8ECDD8d0xEMg7JdaxsIAVMDEwMfQwMDAvAygEBAAD//0d6jptEAQAA"); err != nil { panic("add binary content to resource manager failed: " + err.Error()) } + + if err := gres.Add("H4sIAAAAAAAC/wrwZmYRYeBg4GBIFA0LY0ACEgycDCWpuQU5iSWp+ullmanl8SWpxSV6GSW5OaEhrAyM5o1fk095n/HdumrdNeaLW7c2MDAw/P8f4M3OoZ+9QESIgYGBj4GBAWYBA0MTmgUcSBaADSxt/JoM0o6sKMCbkUmEGeFCZKNBLoSBbY0gkqB7EcZhdw8ECDD8d0xEMg7JdaxsIAVMDEwMfQwMDAvAygEBAAD//0d6jptEAQAA", "assets/"); err != nil { + panic("add binary content to resource manager failed: " + err.Error()) + } } func Test_GviewInGres(t *testing.T) { @@ -635,3 +639,23 @@ func Test_GviewInGres(t *testing.T) { t.Assert(result, "name:john") }) } + +func Test_GviewSearchFileInGres(t *testing.T) { + gres.Dump() + gtest.C(t, func(t *gtest.T) { + v := gview.New() + v.SetPath("assets/template") + result, err := v.Parse(context.TODO(), "gview_test.html", g.Map{ + "name": "john", + }) + t.AssertNil(err) + t.Assert(result, "name:john") + + v1 := gview.New("assets/template") + result1, err1 := v1.Parse(context.TODO(), "gview_test.html", g.Map{ + "name": "john", + }) + t.AssertNil(err1) + t.Assert(result1, "name:john") + }) +}