Skip to content

Commit addd424

Browse files
authored
Fix storage config implementation (#14091)
The design is very flexible, but not implemented correctly. This commit fixes several issues: * Costom storage type stated in https://docs.gitea.io/en-us/config-cheat-sheet/#storage-storage not working * [storage.attachments], [storage.minio] section not respected Signed-off-by: 胡玮文 <[email protected]>
1 parent 9271040 commit addd424

File tree

2 files changed

+177
-21
lines changed

2 files changed

+177
-21
lines changed

modules/setting/storage.go

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,10 @@ func (s *Storage) MapTo(v interface{}) error {
3131
return nil
3232
}
3333

34-
func getStorage(name, typ string, overrides ...*ini.Section) Storage {
34+
func getStorage(name, typ string, targetSec *ini.Section) Storage {
3535
const sectionName = "storage"
3636
sec := Cfg.Section(sectionName)
3737

38-
if len(overrides) == 0 {
39-
overrides = []*ini.Section{
40-
Cfg.Section(sectionName + "." + typ),
41-
Cfg.Section(sectionName + "." + name),
42-
}
43-
}
44-
45-
var storage Storage
46-
47-
storage.Type = sec.Key("STORAGE_TYPE").MustString(typ)
48-
storage.ServeDirect = sec.Key("SERVE_DIRECT").MustBool(false)
49-
5038
// Global Defaults
5139
sec.Key("MINIO_ENDPOINT").MustString("localhost:9000")
5240
sec.Key("MINIO_ACCESS_KEY_ID").MustString("")
@@ -55,18 +43,22 @@ func getStorage(name, typ string, overrides ...*ini.Section) Storage {
5543
sec.Key("MINIO_LOCATION").MustString("us-east-1")
5644
sec.Key("MINIO_USE_SSL").MustBool(false)
5745

58-
storage.Section = sec
59-
60-
for _, override := range overrides {
61-
for _, key := range storage.Section.Keys() {
62-
if !override.HasKey(key.Name()) {
63-
_, _ = override.NewKey(key.Name(), key.Value())
46+
nameSec := Cfg.Section(sectionName + "." + name)
47+
typeSec := Cfg.Section(sectionName + "." + typ)
48+
for _, override := range []*ini.Section{nameSec, typeSec, sec} {
49+
for _, key := range override.Keys() {
50+
if !targetSec.HasKey(key.Name()) {
51+
_, _ = targetSec.NewKey(key.Name(), key.Value())
6452
}
6553
}
66-
storage.ServeDirect = override.Key("SERVE_DIRECT").MustBool(false)
67-
storage.Section = override
6854
}
6955

56+
var storage Storage
57+
storage.Section = targetSec
58+
59+
storage.Type = typeSec.Key("STORAGE_TYPE").MustString(typ)
60+
storage.ServeDirect = storage.Section.Key("SERVE_DIRECT").MustBool(false)
61+
7062
// Specific defaults
7163
storage.Path = storage.Section.Key("PATH").MustString(filepath.Join(AppDataPath, name))
7264
if !filepath.IsAbs(storage.Path) {

modules/setting/storage_test.go

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
// Copyright 2020 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package setting
6+
7+
import (
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
ini "gopkg.in/ini.v1"
12+
)
13+
14+
func Test_getStorageCustomType(t *testing.T) {
15+
iniStr := `
16+
[attachment]
17+
STORAGE_TYPE = my_minio
18+
MINIO_BUCKET = gitea-attachment
19+
20+
[storage.my_minio]
21+
STORAGE_TYPE = minio
22+
MINIO_ENDPOINT = my_minio:9000
23+
`
24+
Cfg, _ = ini.Load([]byte(iniStr))
25+
26+
sec := Cfg.Section("attachment")
27+
storageType := sec.Key("STORAGE_TYPE").MustString("")
28+
storage := getStorage("attachments", storageType, sec)
29+
30+
assert.EqualValues(t, "minio", storage.Type)
31+
assert.EqualValues(t, "my_minio:9000", storage.Section.Key("MINIO_ENDPOINT").String())
32+
assert.EqualValues(t, "gitea-attachment", storage.Section.Key("MINIO_BUCKET").String())
33+
}
34+
35+
func Test_getStorageNameSectionOverridesTypeSection(t *testing.T) {
36+
iniStr := `
37+
[attachment]
38+
STORAGE_TYPE = minio
39+
40+
[storage.attachments]
41+
MINIO_BUCKET = gitea-attachment
42+
43+
[storage.minio]
44+
MINIO_BUCKET = gitea
45+
`
46+
Cfg, _ = ini.Load([]byte(iniStr))
47+
48+
sec := Cfg.Section("attachment")
49+
storageType := sec.Key("STORAGE_TYPE").MustString("")
50+
storage := getStorage("attachments", storageType, sec)
51+
52+
assert.EqualValues(t, "minio", storage.Type)
53+
assert.EqualValues(t, "gitea-attachment", storage.Section.Key("MINIO_BUCKET").String())
54+
}
55+
56+
func Test_getStorageTypeSectionOverridesStorageSection(t *testing.T) {
57+
iniStr := `
58+
[attachment]
59+
STORAGE_TYPE = minio
60+
61+
[storage.minio]
62+
MINIO_BUCKET = gitea-minio
63+
64+
[storage]
65+
MINIO_BUCKET = gitea
66+
`
67+
Cfg, _ = ini.Load([]byte(iniStr))
68+
69+
sec := Cfg.Section("attachment")
70+
storageType := sec.Key("STORAGE_TYPE").MustString("")
71+
storage := getStorage("attachments", storageType, sec)
72+
73+
assert.EqualValues(t, "minio", storage.Type)
74+
assert.EqualValues(t, "gitea-minio", storage.Section.Key("MINIO_BUCKET").String())
75+
}
76+
77+
func Test_getStorageSpecificOverridesStorage(t *testing.T) {
78+
iniStr := `
79+
[attachment]
80+
MINIO_BUCKET = gitea-attachment
81+
82+
[storage.attachments]
83+
MINIO_BUCKET = gitea
84+
`
85+
Cfg, _ = ini.Load([]byte(iniStr))
86+
87+
sec := Cfg.Section("attachment")
88+
storageType := sec.Key("STORAGE_TYPE").MustString("")
89+
storage := getStorage("attachments", storageType, sec)
90+
91+
assert.EqualValues(t, "gitea-attachment", storage.Section.Key("MINIO_BUCKET").String())
92+
}
93+
94+
func Test_getStorageGetDefaults(t *testing.T) {
95+
Cfg, _ = ini.Load([]byte(""))
96+
97+
sec := Cfg.Section("attachment")
98+
storageType := sec.Key("STORAGE_TYPE").MustString("")
99+
storage := getStorage("attachments", storageType, sec)
100+
101+
assert.EqualValues(t, "gitea", storage.Section.Key("MINIO_BUCKET").String())
102+
}
103+
104+
func Test_getStorageMultipleName(t *testing.T) {
105+
iniStr := `
106+
[lfs]
107+
MINIO_BUCKET = gitea-lfs
108+
109+
[attachment]
110+
MINIO_BUCKET = gitea-attachment
111+
112+
[storage]
113+
MINIO_BUCKET = gitea-storage
114+
`
115+
Cfg, _ = ini.Load([]byte(iniStr))
116+
117+
{
118+
sec := Cfg.Section("attachment")
119+
storageType := sec.Key("STORAGE_TYPE").MustString("")
120+
storage := getStorage("attachments", storageType, sec)
121+
122+
assert.EqualValues(t, "gitea-attachment", storage.Section.Key("MINIO_BUCKET").String())
123+
}
124+
{
125+
sec := Cfg.Section("lfs")
126+
storageType := sec.Key("STORAGE_TYPE").MustString("")
127+
storage := getStorage("lfs", storageType, sec)
128+
129+
assert.EqualValues(t, "gitea-lfs", storage.Section.Key("MINIO_BUCKET").String())
130+
}
131+
{
132+
sec := Cfg.Section("avatar")
133+
storageType := sec.Key("STORAGE_TYPE").MustString("")
134+
storage := getStorage("avatars", storageType, sec)
135+
136+
assert.EqualValues(t, "gitea-storage", storage.Section.Key("MINIO_BUCKET").String())
137+
}
138+
}
139+
140+
func Test_getStorageUseOtherNameAsType(t *testing.T) {
141+
iniStr := `
142+
[attachment]
143+
STORAGE_TYPE = lfs
144+
145+
[storage.lfs]
146+
MINIO_BUCKET = gitea-storage
147+
`
148+
Cfg, _ = ini.Load([]byte(iniStr))
149+
150+
{
151+
sec := Cfg.Section("attachment")
152+
storageType := sec.Key("STORAGE_TYPE").MustString("")
153+
storage := getStorage("attachments", storageType, sec)
154+
155+
assert.EqualValues(t, "gitea-storage", storage.Section.Key("MINIO_BUCKET").String())
156+
}
157+
{
158+
sec := Cfg.Section("lfs")
159+
storageType := sec.Key("STORAGE_TYPE").MustString("")
160+
storage := getStorage("lfs", storageType, sec)
161+
162+
assert.EqualValues(t, "gitea-storage", storage.Section.Key("MINIO_BUCKET").String())
163+
}
164+
}

0 commit comments

Comments
 (0)