Skip to content

Commit

Permalink
fix: [#554] Storage can't judge nesting folder correctly (#40)
Browse files Browse the repository at this point in the history
* fix: test (#36) (#37)

* fix: test

* remove codecov

* fix: [#554] Storage can't judge nesting folder correctly (#39)
  • Loading branch information
hwbrzzl authored Jan 3, 2025
1 parent 4b4af25 commit 4e44620
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
32 changes: 16 additions & 16 deletions oss.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,14 +279,8 @@ func (r *Oss) Path(file string) string {
func (r *Oss) Put(file string, content string) error {
// If the file is created in a folder directly, we can't check if the folder exists.
// So we need to create the top folder first.
if !strings.HasSuffix(file, "/") {
index := strings.Index(file, "/")
if index != -1 {
folder := file[:index+1]
if err := r.MakeDirectory(folder); err != nil {
return err
}
}
if err := r.makeDirectories(file); err != nil {
return err
}

tempFile, err := r.tempFile(content)
Expand All @@ -310,14 +304,8 @@ func (r *Oss) PutFileAs(filePath string, source filesystem.File, name string) (s

// If the file is created in a folder directly, we can't check if the folder exists.
// So we need to create the top folder first.
if !strings.HasSuffix(fullPath, "/") {
index := strings.Index(fullPath, "/")
if index != -1 {
folder := fullPath[:index+1]
if err := r.MakeDirectory(folder); err != nil {
return "", err
}
}
if err := r.makeDirectories(str.Of(filePath).Finish("/").String()); err != nil {
return "", err
}

if err := r.bucketInstance.PutObjectFromFile(fullPath, source.File()); err != nil {
Expand Down Expand Up @@ -370,6 +358,18 @@ func (r *Oss) Url(file string) string {
return r.url + "/" + file
}

func (r *Oss) makeDirectories(path string) error {
folders := strings.Split(path, "/")
for i := 1; i < len(folders); i++ {
folder := strings.Join(folders[:i], "/")
if err := r.MakeDirectory(folder); err != nil {
return err
}
}

return nil
}

func (r *Oss) tempFile(content string) (*os.File, error) {
tempFile, err := os.CreateTemp(os.TempDir(), "goravel-")
if err != nil {
Expand Down
10 changes: 8 additions & 2 deletions oss_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ func TestStorage(t *testing.T) {
assert.True(t, driver.Exists("AllDirectories/2.txt"))
assert.True(t, driver.Exists("AllDirectories/3/3.txt"))
assert.True(t, driver.Exists("AllDirectories/3/4/"))
assert.True(t, driver.Exists("AllDirectories/"))
assert.True(t, driver.Exists("AllDirectories/3/"))
assert.True(t, driver.Exists("AllDirectories/3/5/"))
assert.True(t, driver.Exists("AllDirectories/3/5/6/"))
assert.True(t, driver.Exists("AllDirectories/3/5/6/6.txt"))
files, err := driver.AllDirectories("AllDirectories")
assert.Nil(t, err)
Expand Down Expand Up @@ -272,9 +276,11 @@ func TestStorage(t *testing.T) {
{
name: "Put",
setup: func() {
assert.Nil(t, driver.Put("Put/1.txt", "Goravel"))
assert.Nil(t, driver.Put("Put/a/b/1.txt", "Goravel"))
assert.True(t, driver.Exists("Put/"))
assert.True(t, driver.Exists("Put/1.txt"))
assert.True(t, driver.Exists("Put/a/"))
assert.True(t, driver.Exists("Put/a/b/"))
assert.True(t, driver.Exists("Put/a/b/1.txt"))
assert.True(t, driver.Missing("Put/2.txt"))
assert.Nil(t, driver.DeleteDirectory("Put"))
},
Expand Down

0 comments on commit 4e44620

Please sign in to comment.