Skip to content

Commit

Permalink
fix: windows support (#109)
Browse files Browse the repository at this point in the history
* fix: windows support

* feat: optimize fullPath method
  • Loading branch information
devhaozi authored Apr 26, 2023
1 parent 80ac31b commit 64e0e09
Showing 1 changed file with 18 additions and 24 deletions.
42 changes: 18 additions & 24 deletions filesystem/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

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

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

Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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 {
Expand All @@ -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
}

Expand All @@ -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
}

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

0 comments on commit 64e0e09

Please sign in to comment.