Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: [#554] Storage can't judge nesting folder correctly #39

Merged
merged 1 commit into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading