2020-09-29 17:05:13 +08:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-28 02:20:29 +08:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-09-29 17:05:13 +08:00
|
|
|
|
|
|
|
package setting
|
|
|
|
|
|
|
|
import (
|
2020-10-13 11:58:34 +08:00
|
|
|
"path/filepath"
|
|
|
|
"reflect"
|
2020-09-29 17:05:13 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Storage represents configuration of storages
|
|
|
|
type Storage struct {
|
|
|
|
Type string
|
|
|
|
Path string
|
2023-04-25 23:06:39 +08:00
|
|
|
Section ConfigSection
|
2020-09-29 17:05:13 +08:00
|
|
|
ServeDirect bool
|
2020-10-13 11:58:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// MapTo implements the Mappable interface
|
|
|
|
func (s *Storage) MapTo(v interface{}) error {
|
2020-10-26 01:19:06 +08:00
|
|
|
pathValue := reflect.ValueOf(v).Elem().FieldByName("Path")
|
2020-10-13 11:58:34 +08:00
|
|
|
if pathValue.IsValid() && pathValue.Kind() == reflect.String {
|
|
|
|
pathValue.SetString(s.Path)
|
|
|
|
}
|
|
|
|
if s.Section != nil {
|
|
|
|
return s.Section.MapTo(v)
|
2020-09-29 17:05:13 +08:00
|
|
|
}
|
2020-10-13 11:58:34 +08:00
|
|
|
return nil
|
2020-09-29 17:05:13 +08:00
|
|
|
}
|
|
|
|
|
2023-04-25 23:06:39 +08:00
|
|
|
func getStorage(rootCfg ConfigProvider, name, typ string, targetSec ConfigSection) Storage {
|
2020-11-01 04:51:48 +08:00
|
|
|
const sectionName = "storage"
|
2023-02-20 00:12:01 +08:00
|
|
|
sec := rootCfg.Section(sectionName)
|
2020-10-13 11:58:34 +08:00
|
|
|
|
|
|
|
// 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)
|
2023-02-28 00:26:13 +08:00
|
|
|
sec.Key("MINIO_INSECURE_SKIP_VERIFY").MustBool(false)
|
2023-03-28 23:10:24 +08:00
|
|
|
sec.Key("MINIO_CHECKSUM_ALGORITHM").MustString("default")
|
2020-10-13 11:58:34 +08:00
|
|
|
|
2021-06-24 05:12:38 +08:00
|
|
|
if targetSec == nil {
|
2023-02-20 00:12:01 +08:00
|
|
|
targetSec, _ = rootCfg.NewSection(name)
|
2021-06-24 05:12:38 +08:00
|
|
|
}
|
|
|
|
|
2020-12-22 07:03:18 +08:00
|
|
|
var storage Storage
|
|
|
|
storage.Section = targetSec
|
|
|
|
storage.Type = typ
|
|
|
|
|
2023-04-25 23:06:39 +08:00
|
|
|
overrides := make([]ConfigSection, 0, 3)
|
2023-02-20 00:12:01 +08:00
|
|
|
nameSec, err := rootCfg.GetSection(sectionName + "." + name)
|
2020-12-22 07:03:18 +08:00
|
|
|
if err == nil {
|
|
|
|
overrides = append(overrides, nameSec)
|
|
|
|
}
|
|
|
|
|
2023-02-20 00:12:01 +08:00
|
|
|
typeSec, err := rootCfg.GetSection(sectionName + "." + typ)
|
2020-12-22 07:03:18 +08:00
|
|
|
if err == nil {
|
|
|
|
overrides = append(overrides, typeSec)
|
|
|
|
nextType := typeSec.Key("STORAGE_TYPE").String()
|
|
|
|
if len(nextType) > 0 {
|
|
|
|
storage.Type = nextType // Support custom STORAGE_TYPE
|
|
|
|
}
|
|
|
|
}
|
|
|
|
overrides = append(overrides, sec)
|
|
|
|
|
|
|
|
for _, override := range overrides {
|
2020-12-22 01:59:18 +08:00
|
|
|
for _, key := range override.Keys() {
|
|
|
|
if !targetSec.HasKey(key.Name()) {
|
|
|
|
_, _ = targetSec.NewKey(key.Name(), key.Value())
|
2020-10-13 11:58:34 +08:00
|
|
|
}
|
2020-09-29 17:05:13 +08:00
|
|
|
}
|
2020-12-22 07:03:18 +08:00
|
|
|
if len(storage.Type) == 0 {
|
|
|
|
storage.Type = override.Key("STORAGE_TYPE").String()
|
|
|
|
}
|
2020-09-29 17:05:13 +08:00
|
|
|
}
|
2020-12-22 01:59:18 +08:00
|
|
|
storage.ServeDirect = storage.Section.Key("SERVE_DIRECT").MustBool(false)
|
|
|
|
|
2020-10-13 11:58:34 +08:00
|
|
|
// 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
|
2020-09-29 17:05:13 +08:00
|
|
|
}
|