2020-09-29 05:05:13 -04:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package setting
|
|
|
|
|
|
|
|
import (
|
2020-10-12 23:58:34 -04:00
|
|
|
"path/filepath"
|
|
|
|
"reflect"
|
2020-09-29 05:05:13 -04:00
|
|
|
|
|
|
|
ini "gopkg.in/ini.v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Storage represents configuration of storages
|
|
|
|
type Storage struct {
|
|
|
|
Type string
|
|
|
|
Path string
|
2020-10-12 23:58:34 -04:00
|
|
|
Section *ini.Section
|
2020-09-29 05:05:13 -04:00
|
|
|
ServeDirect bool
|
2020-10-12 23:58:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// MapTo implements the Mappable interface
|
|
|
|
func (s *Storage) MapTo(v interface{}) error {
|
2020-10-25 21:40:46 -04:00
|
|
|
pathValue := reflect.ValueOf(v).Elem().FieldByName("Path")
|
2020-10-12 23:58:34 -04:00
|
|
|
if pathValue.IsValid() && pathValue.Kind() == reflect.String {
|
|
|
|
pathValue.SetString(s.Path)
|
|
|
|
}
|
|
|
|
if s.Section != nil {
|
|
|
|
return s.Section.MapTo(v)
|
2020-09-29 05:05:13 -04:00
|
|
|
}
|
2020-10-12 23:58:34 -04:00
|
|
|
return nil
|
2020-09-29 05:05:13 -04:00
|
|
|
}
|
|
|
|
|
2020-10-12 23:58:34 -04:00
|
|
|
func getStorage(name, typ string, overrides ...*ini.Section) Storage {
|
2020-11-01 10:12:50 -05:00
|
|
|
const sectionName = "storage"
|
2020-10-12 23:58:34 -04:00
|
|
|
sec := Cfg.Section(sectionName)
|
|
|
|
|
|
|
|
if len(overrides) == 0 {
|
|
|
|
overrides = []*ini.Section{
|
2020-11-01 10:12:50 -05:00
|
|
|
Cfg.Section(sectionName + "." + typ),
|
2020-10-12 23:58:34 -04:00
|
|
|
Cfg.Section(sectionName + "." + name),
|
|
|
|
}
|
|
|
|
}
|
2020-09-29 05:05:13 -04:00
|
|
|
|
|
|
|
var storage Storage
|
2020-10-12 23:58:34 -04:00
|
|
|
|
2020-10-25 21:40:46 -04:00
|
|
|
storage.Type = sec.Key("STORAGE_TYPE").MustString(typ)
|
2020-09-29 05:05:13 -04:00
|
|
|
storage.ServeDirect = sec.Key("SERVE_DIRECT").MustBool(false)
|
|
|
|
|
2020-10-12 23:58:34 -04: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)
|
|
|
|
|
|
|
|
storage.Section = sec
|
2020-09-29 05:05:13 -04:00
|
|
|
|
2020-10-12 23:58:34 -04:00
|
|
|
for _, override := range overrides {
|
|
|
|
for _, key := range storage.Section.Keys() {
|
|
|
|
if !override.HasKey(key.Name()) {
|
|
|
|
_, _ = override.NewKey(key.Name(), key.Value())
|
|
|
|
}
|
2020-09-29 05:05:13 -04:00
|
|
|
}
|
2020-10-12 23:58:34 -04:00
|
|
|
storage.ServeDirect = override.Key("SERVE_DIRECT").MustBool(false)
|
|
|
|
storage.Section = override
|
2020-09-29 05:05:13 -04:00
|
|
|
}
|
2020-10-12 23:58:34 -04: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 05:05:13 -04:00
|
|
|
}
|