This commit is contained in:
John Guo
2024-12-10 16:52:37 +08:00
parent efec967bec
commit 1afec61190
6 changed files with 34 additions and 33 deletions

View File

@ -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.

View File

@ -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
)

View File

@ -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

View File

@ -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

View File

@ -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)
})

View File

@ -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() {