diff --git a/builder/fs.go b/builder/fs.go index 7577845..c912f9c 100644 --- a/builder/fs.go +++ b/builder/fs.go @@ -35,6 +35,7 @@ func MemoryFS() WritableFS { return &memoryFS{FS: memfs.New()} } // DirFS 返回以普通目录作为保存对象的文件系统 func DirFS(dir string) WritableFS { return &dirFS{ + FS: os.DirFS(dir), dir: dir, files: make([]string, 0, 10), } @@ -46,20 +47,14 @@ type memoryFS struct { } type dirFS struct { + fs.FS dir string files []string } -func (dir *dirFS) Open(name string) (fs.File, error) { - if !fs.ValidPath(name) { - return nil, &fs.PathError{Op: "open", Path: name, Err: os.ErrInvalid} - } - return os.Open(dir.dir + "/" + name) -} - func (dir *dirFS) WriteFile(name string, data []byte, perm fs.FileMode) error { if !fs.ValidPath(name) { - return &fs.PathError{Op: "close", Path: name, Err: os.ErrInvalid} + return &fs.PathError{Op: "close", Path: name, Err: fs.ErrInvalid} } p := dir.dir + "/" + name diff --git a/internal/filesystem/fileserver.go b/internal/filesystem/fileserver.go index edc6ddc..0f7d8d3 100644 --- a/internal/filesystem/fileserver.go +++ b/internal/filesystem/fileserver.go @@ -8,7 +8,6 @@ import ( "io/fs" "log" "net/http" - "os" "path" "path/filepath" @@ -59,7 +58,7 @@ func printError(erro *log.Logger, err error, w http.ResponseWriter) { switch { case errors.Is(err, fs.ErrPermission): http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden) - case errors.Is(err, os.ErrNotExist): + case errors.Is(err, fs.ErrNotExist): http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) case err != nil: http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)