Skip to content

Commit

Permalink
Merge pull request #14 from goravel/bowen/fix-windows
Browse files Browse the repository at this point in the history
fix: windows path
  • Loading branch information
hwbrzzl authored Oct 28, 2023
2 parents 0b3071a + 76e844c commit ed23d53
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# S3
[utils_test.go](..%2Foss%2Futils_test.go)# S3

A s3 disk driver for `facades.Storage()` of Goravel.

Expand Down
2 changes: 2 additions & 0 deletions s3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ func TestStorage(t *testing.T) {
name: "Put",
setup: func() {
assert.Nil(t, driver.Put("Put/1.txt", "Goravel"))
assert.True(t, driver.Exists("Put"))
assert.True(t, driver.Exists("Put/1.txt"))
assert.True(t, driver.Missing("Put/2.txt"))
assert.Nil(t, driver.DeleteDirectory("Put"))
Expand All @@ -290,6 +291,7 @@ func TestStorage(t *testing.T) {
fileInfo := &File{path: "test.txt"}
path, err := driver.PutFile("PutFile", fileInfo)
assert.Nil(t, err)
assert.True(t, driver.Exists("PutFile"))
assert.True(t, driver.Exists(path))
data, err := driver.Get(path)
assert.Nil(t, err)
Expand Down
14 changes: 7 additions & 7 deletions utils.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package s3

import (
"fmt"
"path"
"path/filepath"
"strings"

"github.com/goravel/framework/contracts/filesystem"
Expand All @@ -18,18 +18,18 @@ func fullPathOfFile(filePath string, source filesystem.File, name string) (strin
return "", err
}

return filepath.Join(filePath, strings.TrimSuffix(strings.TrimPrefix(path.Base(name), string(filepath.Separator)), string(filepath.Separator))+"."+extension), nil
return fmt.Sprintf("%s/%s.%s", filePath, strings.TrimSuffix(strings.TrimPrefix(path.Base(name), "/"), "/"), extension), nil
} else {
return filepath.Join(filePath, strings.TrimPrefix(path.Base(name), string(filepath.Separator))), nil
return fmt.Sprintf("%s/%s", filePath, strings.TrimPrefix(path.Base(name), "/")), nil
}
}

func validPath(path string) string {
realPath := strings.TrimPrefix(path, "."+string(filepath.Separator))
realPath = strings.TrimPrefix(realPath, string(filepath.Separator))
realPath := strings.TrimPrefix(path, "./")
realPath = strings.TrimPrefix(realPath, "/")
realPath = strings.TrimPrefix(realPath, ".")
if realPath != "" && !strings.HasSuffix(realPath, string(filepath.Separator)) {
realPath += string(filepath.Separator)
if realPath != "" && !strings.HasSuffix(realPath, "/") {
realPath += "/"
}

return realPath
Expand Down
29 changes: 29 additions & 0 deletions utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package s3

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestFullPathOfFile(t *testing.T) {
path, err := fullPathOfFile("a/b", &File{path: "logo.png"}, "c")
assert.Equal(t, "a/b/c.png", path)
assert.Nil(t, err)

path, err = fullPathOfFile("a/b", &File{path: "logo.png"}, "c.jpg")
assert.Equal(t, "a/b/c.jpg", path)
assert.Nil(t, err)
}

func TestValidPath(t *testing.T) {
assert.Equal(t, "a/", validPath("./a"))
assert.Equal(t, "a/", validPath(".a"))
assert.Equal(t, "a/", validPath("/a"))
assert.Equal(t, "a/", validPath("./a/"))
assert.Equal(t, "a/", validPath(".a/"))
assert.Equal(t, "a/", validPath("/a/"))
assert.Equal(t, "", validPath("./"))
assert.Equal(t, "", validPath("."))
assert.Equal(t, "", validPath("/"))
}

0 comments on commit ed23d53

Please sign in to comment.