Skip to content

Commit

Permalink
Merge pull request #56 from goravel/bowen/#554-1
Browse files Browse the repository at this point in the history
fix: [#554] Storage can't judge nesting folder correctly
  • Loading branch information
hwbrzzl authored Jan 3, 2025
2 parents bb38b66 + 484c484 commit f6ad5b2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
14 changes: 5 additions & 9 deletions minio.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,11 +250,7 @@ func (r *Minio) LastModified(file string) (time.Time, error) {
}

func (r *Minio) MakeDirectory(directory string) error {
if !strings.HasSuffix(directory, "/") {
directory += "/"
}

return r.Put(directory, "")
return r.Put(str.Of(directory).Finish("/").String(), "")
}

func (r *Minio) MimeType(file string) (string, error) {
Expand Down Expand Up @@ -284,11 +280,11 @@ func (r *Minio) Path(file string) string {

func (r *Minio) 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.
// So we need to create the folders first.
if !strings.HasSuffix(file, "/") {
index := strings.Index(file, "/")
if index != -1 {
folder := file[:index+1]
folders := strings.Split(file, "/")
for i := 1; i < len(folders); i++ {
folder := strings.Join(folders[:i], "/")
if err := r.MakeDirectory(folder); err != nil {
return err
}
Expand Down
10 changes: 8 additions & 2 deletions minio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,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 @@ -278,9 +282,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 f6ad5b2

Please sign in to comment.