mirror of
https://github.com/go-gitea/gitea
synced 2025-07-22 18:28:37 +00:00
Provide self-registering storage system (#12978)
* Provide self-registering storage system Signed-off-by: Andrew Thornton <art27@cantab.net> * More simplification Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove old strings from setting Signed-off-by: Andrew Thornton <art27@cantab.net> * oops attachments not attachment Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
This commit is contained in:
@@ -4,12 +4,6 @@
|
||||
|
||||
package setting
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
)
|
||||
|
||||
var (
|
||||
// Attachment settings
|
||||
Attachment = struct {
|
||||
@@ -20,7 +14,6 @@ var (
|
||||
Enabled bool
|
||||
}{
|
||||
Storage: Storage{
|
||||
Type: LocalStorageType,
|
||||
ServeDirect: false,
|
||||
},
|
||||
AllowedTypes: "image/jpeg,image/png,application/zip,application/gzip",
|
||||
@@ -32,37 +25,9 @@ var (
|
||||
|
||||
func newAttachmentService() {
|
||||
sec := Cfg.Section("attachment")
|
||||
Attachment.Storage.Type = sec.Key("STORAGE_TYPE").MustString("")
|
||||
if Attachment.Storage.Type == "" {
|
||||
Attachment.Storage.Type = "default"
|
||||
}
|
||||
storageType := sec.Key("STORAGE_TYPE").MustString("")
|
||||
|
||||
if Attachment.Storage.Type != LocalStorageType && Attachment.Storage.Type != MinioStorageType {
|
||||
storage, ok := storages[Attachment.Storage.Type]
|
||||
if !ok {
|
||||
log.Fatal("Failed to get attachment storage type: %s", Attachment.Storage.Type)
|
||||
}
|
||||
Attachment.Storage = storage
|
||||
}
|
||||
|
||||
// Override
|
||||
Attachment.ServeDirect = sec.Key("SERVE_DIRECT").MustBool(Attachment.ServeDirect)
|
||||
|
||||
switch Attachment.Storage.Type {
|
||||
case LocalStorageType:
|
||||
Attachment.Path = sec.Key("PATH").MustString(filepath.Join(AppDataPath, "attachments"))
|
||||
if !filepath.IsAbs(Attachment.Path) {
|
||||
Attachment.Path = filepath.Join(AppWorkPath, Attachment.Path)
|
||||
}
|
||||
case MinioStorageType:
|
||||
Attachment.Minio.Endpoint = sec.Key("MINIO_ENDPOINT").MustString(Attachment.Minio.Endpoint)
|
||||
Attachment.Minio.AccessKeyID = sec.Key("MINIO_ACCESS_KEY_ID").MustString(Attachment.Minio.AccessKeyID)
|
||||
Attachment.Minio.SecretAccessKey = sec.Key("MINIO_SECRET_ACCESS_KEY").MustString(Attachment.Minio.SecretAccessKey)
|
||||
Attachment.Minio.Bucket = sec.Key("MINIO_BUCKET").MustString(Attachment.Minio.Bucket)
|
||||
Attachment.Minio.Location = sec.Key("MINIO_LOCATION").MustString(Attachment.Minio.Location)
|
||||
Attachment.Minio.UseSSL = sec.Key("MINIO_USE_SSL").MustBool(Attachment.Minio.UseSSL)
|
||||
Attachment.Minio.BasePath = sec.Key("MINIO_BASE_PATH").MustString("attachments/")
|
||||
}
|
||||
Attachment.Storage = getStorage("attachments", storageType, sec)
|
||||
|
||||
Attachment.AllowedTypes = sec.Key("ALLOWED_TYPES").MustString(".docx,.gif,.gz,.jpeg,.jpg,.log,.pdf,.png,.pptx,.txt,.xlsx,.zip")
|
||||
Attachment.MaxSize = sec.Key("MAX_SIZE").MustInt64(4)
|
||||
|
@@ -37,40 +37,15 @@ func newLFSService() {
|
||||
}
|
||||
|
||||
lfsSec := Cfg.Section("lfs")
|
||||
LFS.Storage.Type = lfsSec.Key("STORAGE_TYPE").MustString("")
|
||||
if LFS.Storage.Type == "" {
|
||||
LFS.Storage.Type = "default"
|
||||
}
|
||||
storageType := lfsSec.Key("STORAGE_TYPE").MustString("")
|
||||
|
||||
if LFS.Storage.Type != LocalStorageType && LFS.Storage.Type != MinioStorageType {
|
||||
storage, ok := storages[LFS.Storage.Type]
|
||||
if !ok {
|
||||
log.Fatal("Failed to get lfs storage type: %s", LFS.Storage.Type)
|
||||
}
|
||||
LFS.Storage = storage
|
||||
}
|
||||
// Specifically default PATH to LFS_CONTENT_PATH
|
||||
lfsSec.Key("PATH").MustString(
|
||||
sec.Key("LFS_CONTENT_PATH").String())
|
||||
|
||||
// Override
|
||||
LFS.ServeDirect = lfsSec.Key("SERVE_DIRECT").MustBool(LFS.ServeDirect)
|
||||
switch LFS.Storage.Type {
|
||||
case LocalStorageType:
|
||||
// keep compatible
|
||||
LFS.Path = sec.Key("LFS_CONTENT_PATH").MustString(filepath.Join(AppDataPath, "lfs"))
|
||||
LFS.Path = lfsSec.Key("PATH").MustString(LFS.Path)
|
||||
if !filepath.IsAbs(LFS.Path) {
|
||||
LFS.Path = filepath.Join(AppWorkPath, LFS.Path)
|
||||
}
|
||||
|
||||
case MinioStorageType:
|
||||
LFS.Minio.Endpoint = lfsSec.Key("MINIO_ENDPOINT").MustString(LFS.Minio.Endpoint)
|
||||
LFS.Minio.AccessKeyID = lfsSec.Key("MINIO_ACCESS_KEY_ID").MustString(LFS.Minio.AccessKeyID)
|
||||
LFS.Minio.SecretAccessKey = lfsSec.Key("MINIO_SECRET_ACCESS_KEY").MustString(LFS.Minio.SecretAccessKey)
|
||||
LFS.Minio.Bucket = lfsSec.Key("MINIO_BUCKET").MustString(LFS.Minio.Bucket)
|
||||
LFS.Minio.Location = lfsSec.Key("MINIO_LOCATION").MustString(LFS.Minio.Location)
|
||||
LFS.Minio.UseSSL = lfsSec.Key("MINIO_USE_SSL").MustBool(LFS.Minio.UseSSL)
|
||||
LFS.Minio.BasePath = lfsSec.Key("MINIO_BASE_PATH").MustString("lfs/")
|
||||
}
|
||||
LFS.Storage = getStorage("lfs", storageType, lfsSec)
|
||||
|
||||
// Rest of LFS service settings
|
||||
if LFS.LocksPagingNum == 0 {
|
||||
LFS.LocksPagingNum = 50
|
||||
}
|
||||
|
@@ -804,7 +804,6 @@ func NewContext() {
|
||||
}
|
||||
}
|
||||
|
||||
newStorageService()
|
||||
newAttachmentService()
|
||||
newLFSService()
|
||||
|
||||
|
@@ -5,65 +5,77 @@
|
||||
package setting
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
ini "gopkg.in/ini.v1"
|
||||
)
|
||||
|
||||
// enumerate all storage types
|
||||
const (
|
||||
LocalStorageType = "local"
|
||||
MinioStorageType = "minio"
|
||||
)
|
||||
|
||||
// Storage represents configuration of storages
|
||||
type Storage struct {
|
||||
Type string
|
||||
Path string
|
||||
Section *ini.Section
|
||||
ServeDirect bool
|
||||
Minio struct {
|
||||
Endpoint string
|
||||
AccessKeyID string
|
||||
SecretAccessKey string
|
||||
UseSSL bool
|
||||
Bucket string
|
||||
Location string
|
||||
BasePath string
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
storages = make(map[string]Storage)
|
||||
)
|
||||
|
||||
func getStorage(sec *ini.Section) Storage {
|
||||
var storage Storage
|
||||
storage.Type = sec.Key("STORAGE_TYPE").MustString(LocalStorageType)
|
||||
storage.ServeDirect = sec.Key("SERVE_DIRECT").MustBool(false)
|
||||
switch storage.Type {
|
||||
case LocalStorageType:
|
||||
case MinioStorageType:
|
||||
storage.Minio.Endpoint = sec.Key("MINIO_ENDPOINT").MustString("localhost:9000")
|
||||
storage.Minio.AccessKeyID = sec.Key("MINIO_ACCESS_KEY_ID").MustString("")
|
||||
storage.Minio.SecretAccessKey = sec.Key("MINIO_SECRET_ACCESS_KEY").MustString("")
|
||||
storage.Minio.Bucket = sec.Key("MINIO_BUCKET").MustString("gitea")
|
||||
storage.Minio.Location = sec.Key("MINIO_LOCATION").MustString("us-east-1")
|
||||
storage.Minio.UseSSL = sec.Key("MINIO_USE_SSL").MustBool(false)
|
||||
// MapTo implements the Mappable interface
|
||||
func (s *Storage) MapTo(v interface{}) error {
|
||||
pathValue := reflect.ValueOf(v).FieldByName("Path")
|
||||
if pathValue.IsValid() && pathValue.Kind() == reflect.String {
|
||||
pathValue.SetString(s.Path)
|
||||
}
|
||||
if s.Section != nil {
|
||||
return s.Section.MapTo(v)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func getStorage(name, typ string, overrides ...*ini.Section) Storage {
|
||||
sectionName := "storage"
|
||||
if len(name) > 0 {
|
||||
sectionName = sectionName + "." + typ
|
||||
}
|
||||
sec := Cfg.Section(sectionName)
|
||||
|
||||
if len(overrides) == 0 {
|
||||
overrides = []*ini.Section{
|
||||
Cfg.Section(sectionName + "." + name),
|
||||
}
|
||||
}
|
||||
|
||||
var storage Storage
|
||||
|
||||
storage.Type = sec.Key("STORAGE_TYPE").MustString("")
|
||||
storage.ServeDirect = sec.Key("SERVE_DIRECT").MustBool(false)
|
||||
|
||||
// Global Defaults
|
||||
sec.Key("MINIO_ENDPOINT").MustString("localhost:9000")
|
||||
sec.Key("MINIO_ACCESS_KEY_ID").MustString("")
|
||||
sec.Key("MINIO_SECRET_ACCESS_KEY").MustString("")
|
||||
sec.Key("MINIO_BUCKET").MustString("gitea")
|
||||
sec.Key("MINIO_LOCATION").MustString("us-east-1")
|
||||
sec.Key("MINIO_USE_SSL").MustBool(false)
|
||||
|
||||
storage.Section = sec
|
||||
|
||||
for _, override := range overrides {
|
||||
for _, key := range storage.Section.Keys() {
|
||||
if !override.HasKey(key.Name()) {
|
||||
_, _ = override.NewKey(key.Name(), key.Value())
|
||||
}
|
||||
}
|
||||
storage.ServeDirect = override.Key("SERVE_DIRECT").MustBool(false)
|
||||
storage.Section = override
|
||||
}
|
||||
|
||||
// Specific defaults
|
||||
storage.Path = storage.Section.Key("PATH").MustString(filepath.Join(AppDataPath, name))
|
||||
if !filepath.IsAbs(storage.Path) {
|
||||
storage.Path = filepath.Join(AppWorkPath, storage.Path)
|
||||
storage.Section.Key("PATH").SetValue(storage.Path)
|
||||
}
|
||||
storage.Section.Key("MINIO_BASE_PATH").MustString(name + "/")
|
||||
|
||||
return storage
|
||||
}
|
||||
|
||||
func newStorageService() {
|
||||
sec := Cfg.Section("storage")
|
||||
storages["default"] = getStorage(sec)
|
||||
|
||||
for _, sec := range Cfg.Section("storage").ChildSections() {
|
||||
name := strings.TrimPrefix(sec.Name(), "storage.")
|
||||
if name == "default" || name == LocalStorageType || name == MinioStorageType {
|
||||
log.Error("storage name %s is system reserved!", name)
|
||||
continue
|
||||
}
|
||||
storages[name] = getStorage(sec)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user