2020-09-29 12:05:13 +03: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-13 06:58:34 +03:00
"path/filepath"
"reflect"
2020-09-29 12:05:13 +03:00
ini "gopkg.in/ini.v1"
)
// Storage represents configuration of storages
type Storage struct {
Type string
Path string
2020-10-13 06:58:34 +03:00
Section * ini . Section
2020-09-29 12:05:13 +03:00
ServeDirect bool
2020-10-13 06:58:34 +03:00
}
// MapTo implements the Mappable interface
func ( s * Storage ) MapTo ( v interface { } ) error {
2020-10-25 20:19:06 +03:00
pathValue := reflect . ValueOf ( v ) . Elem ( ) . FieldByName ( "Path" )
2020-10-13 06:58:34 +03:00
if pathValue . IsValid ( ) && pathValue . Kind ( ) == reflect . String {
pathValue . SetString ( s . Path )
}
if s . Section != nil {
return s . Section . MapTo ( v )
2020-09-29 12:05:13 +03:00
}
2020-10-13 06:58:34 +03:00
return nil
2020-09-29 12:05:13 +03:00
}
2020-12-21 20:59:18 +03:00
func getStorage ( name , typ string , targetSec * ini . Section ) Storage {
2020-10-31 23:51:48 +03:00
const sectionName = "storage"
2020-10-13 06:58:34 +03:00
sec := Cfg . Section ( sectionName )
// 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 )
2021-06-24 00:12:38 +03:00
if targetSec == nil {
targetSec , _ = Cfg . NewSection ( name )
}
2020-12-22 02:03:18 +03:00
var storage Storage
storage . Section = targetSec
storage . Type = typ
overrides := make ( [ ] * ini . Section , 0 , 3 )
nameSec , err := Cfg . GetSection ( sectionName + "." + name )
if err == nil {
overrides = append ( overrides , nameSec )
}
typeSec , err := Cfg . GetSection ( sectionName + "." + typ )
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-21 20:59:18 +03:00
for _ , key := range override . Keys ( ) {
if ! targetSec . HasKey ( key . Name ( ) ) {
_ , _ = targetSec . NewKey ( key . Name ( ) , key . Value ( ) )
2020-10-13 06:58:34 +03:00
}
2020-09-29 12:05:13 +03:00
}
2020-12-22 02:03:18 +03:00
if len ( storage . Type ) == 0 {
storage . Type = override . Key ( "STORAGE_TYPE" ) . String ( )
}
2020-09-29 12:05:13 +03:00
}
2020-12-21 20:59:18 +03:00
storage . ServeDirect = storage . Section . Key ( "SERVE_DIRECT" ) . MustBool ( false )
2020-10-13 06:58:34 +03: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 12:05:13 +03:00
}