diff --git a/net/ghttp/ghttp_request.go b/net/ghttp/ghttp_request.go index 73801995b..5a142c73c 100644 --- a/net/ghttp/ghttp_request.go +++ b/net/ghttp/ghttp_request.go @@ -65,9 +65,9 @@ type Request struct { // staticFile is the file struct for static file service. type staticFile struct { - File *gres.File // Resource file object. - Path string // File path. - IsDir bool // Is directory. + File gres.File // Resource file object. + Path string // File path. + IsDir bool // Is directory. } // newRequest creates and returns a new request object. diff --git a/net/ghttp/ghttp_server_handler.go b/net/ghttp/ghttp_server_handler.go index 27ca64e14..34b5a5596 100644 --- a/net/ghttp/ghttp_server_handler.go +++ b/net/ghttp/ghttp_server_handler.go @@ -223,7 +223,7 @@ func (s *Server) handleAfterRequestDone(request *Request) { // It returns a file struct specifying the file information. func (s *Server) searchStaticFile(uri string) *staticFile { var ( - file *gres.File + file gres.File path string dir bool ) diff --git a/os/gcfg/gcfg_adapter_file_path.go b/os/gcfg/gcfg_adapter_file_path.go index f799e51d2..ae1a29e2a 100644 --- a/os/gcfg/gcfg_adapter_file_path.go +++ b/os/gcfg/gcfg_adapter_file_path.go @@ -179,7 +179,7 @@ func (a *AdapterFile) GetPaths() []string { func (a *AdapterFile) doGetFilePath(fileNameOrPath string) (filePath string) { var ( tempPath string - resFile *gres.File + resFile gres.File fileInfo os.FileInfo ) // Searching resource manager. @@ -187,7 +187,7 @@ func (a *AdapterFile) doGetFilePath(fileNameOrPath string) (filePath string) { for _, tryFolder := range resourceTryFolders { tempPath = tryFolder + fileNameOrPath if resFile = gres.Get(tempPath); resFile != nil { - fileInfo, _ = resFile.Stat() + fileInfo = resFile.FileInfo() if fileInfo != nil && !fileInfo.IsDir() { filePath = resFile.Name() return @@ -199,7 +199,7 @@ func (a *AdapterFile) doGetFilePath(fileNameOrPath string) (filePath string) { for _, tryFolder := range resourceTryFolders { tempPath = searchPath + tryFolder + fileNameOrPath if resFile = gres.Get(tempPath); resFile != nil { - fileInfo, _ = resFile.Stat() + fileInfo = resFile.FileInfo() if fileInfo != nil && !fileInfo.IsDir() { filePath = resFile.Name() return diff --git a/os/gres/gres_file.go b/os/gres/gres_file.go index e57f36626..f3ec40b4f 100644 --- a/os/gres/gres_file.go +++ b/os/gres/gres_file.go @@ -10,6 +10,7 @@ import ( "bytes" "io" "io/fs" + "net/http" "os" "sync" "time" @@ -25,16 +26,12 @@ import ( // Directory files should also implement [ReadDirFile]. // A file may implement [io.ReaderAt] or [io.Seeker] as optimizations. type File interface { + http.File Name() string Path() string Content() []byte FileInfo() os.FileInfo Export(dst string, option ...ExportOption) error - - // For http.File implementation. - - Readdir(count int) ([]os.FileInfo, error) - io.ReadSeekCloser } // File implements the interface fs.File. @@ -62,6 +59,11 @@ func (f *localFile) FileInfo() os.FileInfo { return f.file } +// Stat returns the FileInfo structure describing file. +func (f *localFile) Stat() (os.FileInfo, error) { + return f.file, nil +} + // Content returns the file content func (f *localFile) Content() []byte { return f.content diff --git a/os/gres/gres_z_unit_test.go b/os/gres/gres_z_unit_test.go index 4b0653730..8b225d02e 100644 --- a/os/gres/gres_z_unit_test.go +++ b/os/gres/gres_z_unit_test.go @@ -58,10 +58,12 @@ func Test_Pack(t *testing.T) { ) t.AssertNil(err) - r := gres.New() - err = r.Add(string(data)) + fs := gres.NewResFS() + err = fs.Add(string(data)) t.AssertNil(err) - t.Assert(r.Contains("files/"), true) + + res := gres.NewWithFS(fs) + t.Assert(res.Contains("files/"), true) }) gtest.C(t, func(t *gtest.T) { @@ -71,10 +73,12 @@ func Test_Pack(t *testing.T) { ) t.AssertNil(err) - r := gres.New() - err = r.Add(string(data)) + fs := gres.NewResFS() + err = fs.Add(string(data)) t.AssertNil(err) - t.Assert(r.Contains("/root/"), true) + + res := gres.NewWithFS(fs) + t.Assert(res.Contains("/root/"), true) }) } @@ -89,10 +93,12 @@ func Test_PackToFile(t *testing.T) { defer gfile.Remove(dstPath) - r := gres.New() - err = r.Load(dstPath) + fs := gres.NewResFS() + err = fs.Load(dstPath) t.AssertNil(err) - t.Assert(r.Contains("files"), true) + + res := gres.NewWithFS(fs) + t.Assert(res.Contains("files"), true) }) } @@ -152,12 +158,8 @@ func Test_Basic(t *testing.T) { t.Assert(info.IsDir(), false) t.Assert(info.Name(), "test1") - rc, err := file.Open() - t.AssertNil(err) - defer rc.Close() - b := make([]byte, 5) - n, err := rc.Read(b) + n, err := file.Read(b) t.Assert(n, 5) t.AssertNil(err) t.Assert(string(b), "test1") @@ -175,11 +177,6 @@ func Test_Basic(t *testing.T) { t.AssertNE(info, nil) t.Assert(info.IsDir(), true) t.Assert(info.Name(), "dir2") - - rc, err := file.Open() - t.AssertNil(err) - defer rc.Close() - t.Assert(file.Content(), nil) }) diff --git a/os/gview/gview_parse.go b/os/gview/gview_parse.go index 44ccbffca..63e8e13f8 100644 --- a/os/gview/gview_parse.go +++ b/os/gview/gview_parse.go @@ -121,7 +121,7 @@ func (view *View) ParseOption(ctx context.Context, option Option) (result string path string folder string content string - resource *gres.File + resource gres.File ) // Searching the absolute file path for `file`. path, folder, resource, err = view.searchFile(ctx, option.File) @@ -372,7 +372,9 @@ func (view *View) formatTemplateObjectCreatingError(filePath, tplName string, er // searchFile returns the found absolute path for `file` and its template folder path. // Note that, the returned `folder` is the template folder path, but not the folder of // the returned template file `path`. -func (view *View) searchFile(ctx context.Context, file string) (path string, folder string, resource *gres.File, err error) { +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. if !gres.IsEmpty() {