aboutsummaryrefslogtreecommitdiffstats
path: root/modules/setting/storage.go
blob: 32f74aa072cbfb7c43c0c8d0ea6dabbbe278a0cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright 2020 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package setting

import (
	"path/filepath"
	"reflect"

	ini "gopkg.in/ini.v1"
)

// Storage represents configuration of storages
type Storage struct {
	Type        string
	Path        string
	Section     *ini.Section
	ServeDirect bool
}

// MapTo implements the Mappable interface
func (s *Storage) MapTo(v interface{}) error {
	pathValue := reflect.ValueOf(v).Elem().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, targetSec *ini.Section) Storage {
	const sectionName = "storage"
	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)

	if targetSec == nil {
		targetSec, _ = Cfg.NewSection(name)
	}

	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 {
		for _, key := range override.Keys() {
			if !targetSec.HasKey(key.Name()) {
				_, _ = targetSec.NewKey(key.Name(), key.Value())
			}
		}
		if len(storage.Type) == 0 {
			storage.Type = override.Key("STORAGE_TYPE").String()
		}
	}
	storage.ServeDirect = storage.Section.Key("SERVE_DIRECT").MustBool(false)

	// 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
}