From 64e0e0919290b81b688251170802636cd934bfdc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=97=E5=AD=90?= Date: Wed, 26 Apr 2023 21:22:51 +0800 Subject: [PATCH] fix: windows support (#109) * fix: windows support * feat: optimize fullPath method --- filesystem/local.go | 42 ++++++++++++++++++------------------------ 1 file changed, 18 insertions(+), 24 deletions(-) diff --git a/filesystem/local.go b/filesystem/local.go index 58d10a122..e21793d7b 100644 --- a/filesystem/local.go +++ b/filesystem/local.go @@ -4,16 +4,13 @@ import ( "context" "errors" "fmt" - "io/ioutil" "os" - "path" "path/filepath" "strings" "time" "github.com/goravel/framework/contracts/filesystem" "github.com/goravel/framework/facades" - "github.com/goravel/framework/support" "github.com/goravel/framework/support/str" ) @@ -37,9 +34,9 @@ func (r *Local) AllDirectories(path string) ([]string, error) { } if info.IsDir() { realPath := strings.ReplaceAll(fullPath, r.fullPath(path), "") - realPath = strings.TrimPrefix(realPath, "/") + realPath = strings.TrimPrefix(realPath, string(filepath.Separator)) if realPath != "" { - directories = append(directories, realPath+"/") + directories = append(directories, realPath+string(filepath.Separator)) } } @@ -56,7 +53,7 @@ func (r *Local) AllFiles(path string) ([]string, error) { return err } if !info.IsDir() { - files = append(files, strings.ReplaceAll(fullPath, r.fullPath(path)+"/", "")) + files = append(files, strings.ReplaceAll(fullPath, r.fullPath(path)+string(filepath.Separator), "")) } return nil @@ -101,10 +98,10 @@ func (r *Local) DeleteDirectory(directory string) error { func (r *Local) Directories(path string) ([]string, error) { var directories []string - fileInfo, _ := ioutil.ReadDir(r.fullPath(path)) + fileInfo, _ := os.ReadDir(r.fullPath(path)) for _, f := range fileInfo { if f.IsDir() { - directories = append(directories, f.Name()+"/") + directories = append(directories, f.Name()+string(filepath.Separator)) } } @@ -121,7 +118,7 @@ func (r *Local) Exists(file string) bool { func (r *Local) Files(path string) ([]string, error) { var files []string - fileInfo, err := ioutil.ReadDir(r.fullPath(path)) + fileInfo, err := os.ReadDir(r.fullPath(path)) if err != nil { return nil, err } @@ -135,7 +132,7 @@ func (r *Local) Files(path string) ([]string, error) { } func (r *Local) Get(file string) (string, error) { - data, err := ioutil.ReadFile(r.fullPath(file)) + data, err := os.ReadFile(r.fullPath(file)) if err != nil { return "", err } @@ -144,7 +141,7 @@ func (r *Local) Get(file string) (string, error) { } func (r *Local) MakeDirectory(directory string) error { - return os.MkdirAll(path.Dir(r.fullPath(directory)+"/"), os.ModePerm) + return os.MkdirAll(filepath.Dir(r.fullPath(directory)+string(filepath.Separator)), os.ModePerm) } func (r *Local) Missing(file string) bool { @@ -153,7 +150,7 @@ func (r *Local) Missing(file string) bool { func (r *Local) Move(oldFile, newFile string) error { newFile = r.fullPath(newFile) - if err := os.MkdirAll(path.Dir(newFile), os.ModePerm); err != nil { + if err := os.MkdirAll(filepath.Dir(newFile), os.ModePerm); err != nil { return err } @@ -165,12 +162,12 @@ func (r *Local) Move(oldFile, newFile string) error { } func (r *Local) Path(file string) string { - return support.RootPath + "/" + strings.TrimPrefix(strings.TrimPrefix(r.fullPath(file), "/"), "./") + return r.fullPath(file) } func (r *Local) Put(file, content string) error { file = r.fullPath(file) - if err := os.MkdirAll(path.Dir(file), os.ModePerm); err != nil { + if err := os.MkdirAll(filepath.Dir(file), os.ModePerm); err != nil { return err } @@ -192,7 +189,7 @@ func (r *Local) PutFile(filePath string, source filesystem.File) (string, error) } func (r *Local) PutFileAs(filePath string, source filesystem.File, name string) (string, error) { - data, err := ioutil.ReadFile(source.File()) + data, err := os.ReadFile(source.File()) if err != nil { return "", err } @@ -236,18 +233,15 @@ func (r *Local) Url(file string) string { } func (r *Local) fullPath(path string) string { - if path == "." { - path = "" - } - realPath := strings.TrimPrefix(path, "./") - realPath = strings.TrimSuffix(strings.TrimPrefix(realPath, "/"), "/") - if realPath == "" { + realPath := filepath.Clean(path) + + if realPath == "." { return r.rootPath() - } else { - return r.rootPath() + realPath } + + return filepath.Join(r.rootPath(), realPath) } func (r *Local) rootPath() string { - return strings.TrimSuffix(r.root, "/") + "/" + return strings.TrimSuffix(r.root, string(filepath.Separator)) + string(filepath.Separator) }