Skip to content

Commit 266283c

Browse files
committed
More simplification
Signed-off-by: Andrew Thornton <[email protected]>
1 parent 1b352a4 commit 266283c

File tree

4 files changed

+43
-77
lines changed

4 files changed

+43
-77
lines changed

modules/setting/attachment.go

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55
package setting
66

77
import (
8-
"path/filepath"
98
"strings"
10-
11-
"code.gitea.io/gitea/modules/log"
129
)
1310

1411
var (
@@ -33,37 +30,11 @@ var (
3330

3431
func newAttachmentService() {
3532
sec := Cfg.Section("attachment")
36-
Attachment.Storage.Type = sec.Key("STORAGE_TYPE").MustString("")
37-
if Attachment.Storage.Type == "" {
38-
Attachment.Storage.Type = "default"
39-
}
40-
41-
if Attachment.Storage.Type != LocalStorageType && Attachment.Storage.Type != MinioStorageType {
42-
storage, ok := storages[Attachment.Storage.Type]
43-
if !ok {
44-
log.Fatal("Failed to get attachment storage type: %s", Attachment.Storage.Type)
45-
}
46-
Attachment.Storage = storage
47-
48-
for _, key := range storage.Section.Keys() {
49-
if !sec.HasKey(key.Name()) {
50-
_, _ = sec.NewKey(key.Name(), key.Value())
51-
}
52-
}
53-
Attachment.Storage.Section = sec
54-
}
55-
56-
// Override
57-
Attachment.ServeDirect = sec.Key("SERVE_DIRECT").MustBool(Attachment.ServeDirect)
58-
59-
Attachment.Storage.Path = sec.Key("PATH").MustString(filepath.Join(AppDataPath, "attachments"))
60-
if !filepath.IsAbs(Attachment.Storage.Path) {
61-
Attachment.Storage.Path = filepath.Join(AppWorkPath, Attachment.Storage.Path)
62-
sec.Key("PATH").SetValue(Attachment.Storage.Path)
63-
}
33+
storageType := sec.Key("STORAGE_TYPE").MustString("")
6434

65-
sec.Key("MINIO_BASE_PATH").MustString("attachments/")
35+
Attachment.Storage = getStorage("attachment", storageType, sec)
6636

37+
// Other settings
6738
Attachment.AllowedTypes = strings.Replace(sec.Key("ALLOWED_TYPES").MustString("image/jpeg,image/png,application/zip,application/gzip"), "|", ",", -1)
6839
Attachment.MaxSize = sec.Key("MAX_SIZE").MustInt64(4)
6940
Attachment.MaxFiles = sec.Key("MAX_FILES").MustInt(5)

modules/setting/lfs.go

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -37,35 +37,15 @@ func newLFSService() {
3737
}
3838

3939
lfsSec := Cfg.Section("lfs")
40-
LFS.Storage.Type = lfsSec.Key("STORAGE_TYPE").MustString("")
41-
if LFS.Storage.Type == "" {
42-
LFS.Storage.Type = "default"
43-
}
40+
storageType := lfsSec.Key("STORAGE_TYPE").MustString("")
4441

45-
if LFS.Storage.Type != LocalStorageType && LFS.Storage.Type != MinioStorageType {
46-
storage, ok := storages[LFS.Storage.Type]
47-
if !ok {
48-
log.Fatal("Failed to get lfs storage type: %s", LFS.Storage.Type)
49-
}
50-
LFS.Storage = storage
42+
// Specifically default PATH to LFS_CONTENT_PATH
43+
lfsSec.Key("PATH").MustString(
44+
sec.Key("LFS_CONTENT_PATH").String())
5145

52-
for _, key := range storage.Section.Keys() {
53-
if !lfsSec.HasKey(key.Name()) {
54-
_, _ = lfsSec.NewKey(key.Name(), key.Value())
55-
}
56-
}
57-
LFS.Storage.Section = lfsSec
58-
}
59-
60-
// Override
61-
LFS.ServeDirect = lfsSec.Key("SERVE_DIRECT").MustBool(LFS.ServeDirect)
62-
LFS.Storage.Path = sec.Key("LFS_CONTENT_PATH").MustString(filepath.Join(AppDataPath, "lfs"))
63-
LFS.Storage.Path = lfsSec.Key("PATH").MustString(LFS.Storage.Path)
64-
if !filepath.IsAbs(LFS.Storage.Path) {
65-
lfsSec.Key("PATH").SetValue(filepath.Join(AppWorkPath, LFS.Storage.Path))
66-
}
67-
lfsSec.Key("MINIO_BASE_PATH").MustString("lfs/")
46+
LFS.Storage = getStorage("lfs", storageType, lfsSec)
6847

48+
// Rest of LFS service settings
6949
if LFS.LocksPagingNum == 0 {
7050
LFS.LocksPagingNum = 50
7151
}

modules/setting/setting.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,6 @@ func NewContext() {
759759
}
760760
}
761761

762-
newStorageService()
763762
newAttachmentService()
764763
newLFSService()
765764

modules/setting/storage.go

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
package setting
66

77
import (
8+
"path/filepath"
89
"reflect"
9-
"strings"
1010

11-
"code.gitea.io/gitea/modules/log"
1211
ini "gopkg.in/ini.v1"
1312
)
1413

@@ -38,34 +37,51 @@ func (s *Storage) MapTo(v interface{}) error {
3837
return nil
3938
}
4039

41-
var (
42-
storages = make(map[string]Storage)
43-
)
40+
func getStorage(name, typ string, overrides ...*ini.Section) Storage {
41+
sectionName := "storage"
42+
if len(name) > 0 {
43+
sectionName = sectionName + "." + typ
44+
}
45+
sec := Cfg.Section(sectionName)
46+
47+
if len(overrides) == 0 {
48+
overrides = []*ini.Section{
49+
Cfg.Section(sectionName + "." + name),
50+
}
51+
}
4452

45-
func getStorage(sec *ini.Section) Storage {
4653
var storage Storage
54+
4755
storage.Type = sec.Key("STORAGE_TYPE").MustString(LocalStorageType)
48-
storage.Section = sec
4956
storage.ServeDirect = sec.Key("SERVE_DIRECT").MustBool(false)
57+
58+
// Global Defaults
5059
sec.Key("MINIO_ENDPOINT").MustString("localhost:9000")
5160
sec.Key("MINIO_ACCESS_KEY_ID").MustString("")
5261
sec.Key("MINIO_SECRET_ACCESS_KEY").MustString("")
5362
sec.Key("MINIO_BUCKET").MustString("gitea")
5463
sec.Key("MINIO_LOCATION").MustString("us-east-1")
5564
sec.Key("MINIO_USE_SSL").MustBool(false)
56-
return storage
57-
}
5865

59-
func newStorageService() {
60-
sec := Cfg.Section("storage")
61-
storages["default"] = getStorage(sec)
66+
storage.Section = sec
6267

63-
for _, sec := range Cfg.Section("storage").ChildSections() {
64-
name := strings.TrimPrefix(sec.Name(), "storage.")
65-
if name == "default" || name == LocalStorageType || name == MinioStorageType {
66-
log.Error("storage name %s is system reserved!", name)
67-
continue
68+
for _, override := range overrides {
69+
for _, key := range storage.Section.Keys() {
70+
if !override.HasKey(key.Name()) {
71+
_, _ = override.NewKey(key.Name(), key.Value())
72+
}
6873
}
69-
storages[name] = getStorage(sec)
74+
storage.ServeDirect = override.Key("SERVE_DIRECT").MustBool(false)
75+
storage.Section = override
7076
}
77+
78+
// Specific defaults
79+
storage.Path = storage.Section.Key("PATH").MustString(filepath.Join(AppDataPath, name))
80+
if !filepath.IsAbs(storage.Path) {
81+
storage.Path = filepath.Join(AppWorkPath, storage.Path)
82+
storage.Section.Key("PATH").SetValue(storage.Path)
83+
}
84+
storage.Section.Key("MINIO_BASE_PATH").MustString(name + "/")
85+
86+
return storage
7187
}

0 commit comments

Comments
 (0)