Skip to content

Commit

Permalink
refactor: 尽量复用原有的实现
Browse files Browse the repository at this point in the history
  • Loading branch information
caixw committed Dec 17, 2021
1 parent e67439c commit a390f48
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 10 deletions.
11 changes: 3 additions & 8 deletions builder/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
Expand All @@ -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
Expand Down
3 changes: 1 addition & 2 deletions internal/filesystem/fileserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"io/fs"
"log"
"net/http"
"os"
"path"
"path/filepath"

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit a390f48

Please sign in to comment.