Skip to content

Commit 0fbf9d2

Browse files
committed
fix
1 parent ebff051 commit 0fbf9d2

File tree

2 files changed

+50
-14
lines changed

2 files changed

+50
-14
lines changed

modules/storage/minio.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,18 @@ func NewMinioStorage(ctx context.Context, cfg *setting.Storage) (ObjectStorage,
114114
}
115115

116116
func (m *MinioStorage) buildMinioPath(p string) string {
117-
p = util.PathJoinRelX(m.basePath, p)
117+
p = strings.TrimPrefix(util.PathJoinRelX(m.basePath, p), "/") // object store doesn't use slash for root path
118118
if p == "." {
119-
p = "" // minio doesn't use dot as relative path
119+
p = "" // object store doesn't use dot as relative path
120+
}
121+
return p
122+
}
123+
124+
func (m *MinioStorage) buildMinioDirPrefix(p string) string {
125+
// ending slash is required for avoiding matching like "foo/" and "foobar/" with prefix "foo"
126+
p = m.buildMinioPath(p) + "/"
127+
if p == "/" {
128+
p = "" // object store doesn't use slash for root path
120129
}
121130
return p
122131
}
@@ -215,20 +224,11 @@ func (m *MinioStorage) URL(path, name string) (*url.URL, error) {
215224
// IterateObjects iterates across the objects in the miniostorage
216225
func (m *MinioStorage) IterateObjects(dirName string, fn func(path string, obj Object) error) error {
217226
opts := minio.GetObjectOptions{}
218-
lobjectCtx, cancel := context.WithCancel(m.ctx)
219-
defer cancel()
220-
221-
basePath := m.basePath
222-
if dirName != "" {
223-
// ending slash is required for avoiding matching like "foo/" and "foobar/" with prefix "foo"
224-
basePath = m.buildMinioPath(dirName) + "/"
225-
}
226-
227-
for mObjInfo := range m.client.ListObjects(lobjectCtx, m.bucket, minio.ListObjectsOptions{
228-
Prefix: basePath,
227+
for mObjInfo := range m.client.ListObjects(m.ctx, m.bucket, minio.ListObjectsOptions{
228+
Prefix: m.buildMinioDirPrefix(dirName),
229229
Recursive: true,
230230
}) {
231-
object, err := m.client.GetObject(lobjectCtx, m.bucket, mObjInfo.Key, opts)
231+
object, err := m.client.GetObject(m.ctx, m.bucket, mObjInfo.Key, opts)
232232
if err != nil {
233233
return convertMinioErr(err)
234234
}

modules/storage/minio_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"testing"
99

1010
"code.gitea.io/gitea/modules/setting"
11+
12+
"github.com/stretchr/testify/assert"
1113
)
1214

1315
func TestMinioStorageIterator(t *testing.T) {
@@ -25,3 +27,37 @@ func TestMinioStorageIterator(t *testing.T) {
2527
},
2628
})
2729
}
30+
31+
func TestMinioStoragePath(t *testing.T) {
32+
m := &MinioStorage{basePath: ""}
33+
assert.Equal(t, "", m.buildMinioPath("/"))
34+
assert.Equal(t, "", m.buildMinioPath("."))
35+
assert.Equal(t, "a", m.buildMinioPath("/a"))
36+
assert.Equal(t, "a/b", m.buildMinioPath("/a/b/"))
37+
assert.Equal(t, "", m.buildMinioDirPrefix(""))
38+
assert.Equal(t, "a/", m.buildMinioDirPrefix("/a/"))
39+
40+
m = &MinioStorage{basePath: "/"}
41+
assert.Equal(t, "", m.buildMinioPath("/"))
42+
assert.Equal(t, "", m.buildMinioPath("."))
43+
assert.Equal(t, "a", m.buildMinioPath("/a"))
44+
assert.Equal(t, "a/b", m.buildMinioPath("/a/b/"))
45+
assert.Equal(t, "", m.buildMinioDirPrefix(""))
46+
assert.Equal(t, "a/", m.buildMinioDirPrefix("/a/"))
47+
48+
m = &MinioStorage{basePath: "/base"}
49+
assert.Equal(t, "base", m.buildMinioPath("/"))
50+
assert.Equal(t, "base", m.buildMinioPath("."))
51+
assert.Equal(t, "base/a", m.buildMinioPath("/a"))
52+
assert.Equal(t, "base/a/b", m.buildMinioPath("/a/b/"))
53+
assert.Equal(t, "base/", m.buildMinioDirPrefix(""))
54+
assert.Equal(t, "base/a/", m.buildMinioDirPrefix("/a/"))
55+
56+
m = &MinioStorage{basePath: "/base/"}
57+
assert.Equal(t, "base", m.buildMinioPath("/"))
58+
assert.Equal(t, "base", m.buildMinioPath("."))
59+
assert.Equal(t, "base/a", m.buildMinioPath("/a"))
60+
assert.Equal(t, "base/a/b", m.buildMinioPath("/a/b/"))
61+
assert.Equal(t, "base/", m.buildMinioDirPrefix(""))
62+
assert.Equal(t, "base/a/", m.buildMinioDirPrefix("/a/"))
63+
}

0 commit comments

Comments
 (0)