From 4e44620c7f4dc2109b636432f9314eff70dcb50b Mon Sep 17 00:00:00 2001 From: Wenbo Han Date: Fri, 3 Jan 2025 19:13:01 +0800 Subject: [PATCH] fix: [#554] Storage can't judge nesting folder correctly (#40) * fix: test (#36) (#37) * fix: test * remove codecov * fix: [#554] Storage can't judge nesting folder correctly (#39) --- oss.go | 32 ++++++++++++++++---------------- oss_test.go | 10 ++++++++-- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/oss.go b/oss.go index 5903897..3b70f4b 100644 --- a/oss.go +++ b/oss.go @@ -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) @@ -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 { @@ -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 { diff --git a/oss_test.go b/oss_test.go index 5b7dc00..eeb4abb 100644 --- a/oss_test.go +++ b/oss_test.go @@ -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) @@ -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")) },