aboutsummaryrefslogtreecommitdiff
path: root/setup/config/config_mediaapi.go
blob: 9a7d8496940717ae35c84c66976da3203931952c (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
package config

import (
	"fmt"
)

type MediaAPI struct {
	Matrix *Global `yaml:"-"`

	InternalAPI InternalAPIOptions `yaml:"internal_api"`
	ExternalAPI ExternalAPIOptions `yaml:"external_api"`

	// The MediaAPI database stores information about files uploaded and downloaded
	// by local users. It is only accessed by the MediaAPI.
	Database DatabaseOptions `yaml:"database"`

	// The base path to where the media files will be stored. May be relative or absolute.
	BasePath Path `yaml:"base_path"`

	// The absolute base path to where media files will be stored.
	AbsBasePath Path `yaml:"-"`

	// The maximum file size in bytes that is allowed to be stored on this server.
	// Note: if max_file_size_bytes is set to 0, the size is unlimited.
	// Note: if max_file_size_bytes is not set, it will default to 10485760 (10MB)
	MaxFileSizeBytes *FileSizeBytes `yaml:"max_file_size_bytes,omitempty"`

	// Whether to dynamically generate thumbnails on-the-fly if the requested resolution is not already generated
	DynamicThumbnails bool `yaml:"dynamic_thumbnails"`

	// The maximum number of simultaneous thumbnail generators. default: 10
	MaxThumbnailGenerators int `yaml:"max_thumbnail_generators"`

	// A list of thumbnail sizes to be pre-generated for downloaded remote / uploaded content
	ThumbnailSizes []ThumbnailSize `yaml:"thumbnail_sizes"`
}

// DefaultMaxFileSizeBytes defines the default file size allowed in transfers
var DefaultMaxFileSizeBytes = FileSizeBytes(10485760)

func (c *MediaAPI) Defaults(generate bool) {
	c.InternalAPI.Listen = "http://localhost:7774"
	c.InternalAPI.Connect = "http://localhost:7774"
	c.ExternalAPI.Listen = "http://[::]:8074"
	c.Database.Defaults(5)
	if generate {
		c.Database.ConnectionString = "file:mediaapi.db"
		c.BasePath = "./media_store"
	}

	c.MaxFileSizeBytes = &DefaultMaxFileSizeBytes
	c.MaxThumbnailGenerators = 10
}

func (c *MediaAPI) Verify(configErrs *ConfigErrors, isMonolith bool) {
	checkURL(configErrs, "media_api.internal_api.listen", string(c.InternalAPI.Listen))
	checkURL(configErrs, "media_api.internal_api.connect", string(c.InternalAPI.Connect))
	if !isMonolith {
		checkURL(configErrs, "media_api.external_api.listen", string(c.ExternalAPI.Listen))
	}
	checkNotEmpty(configErrs, "media_api.database.connection_string", string(c.Database.ConnectionString))

	checkNotEmpty(configErrs, "media_api.base_path", string(c.BasePath))
	checkPositive(configErrs, "media_api.max_file_size_bytes", int64(*c.MaxFileSizeBytes))
	checkPositive(configErrs, "media_api.max_thumbnail_generators", int64(c.MaxThumbnailGenerators))

	for i, size := range c.ThumbnailSizes {
		checkPositive(configErrs, fmt.Sprintf("media_api.thumbnail_sizes[%d].width", i), int64(size.Width))
		checkPositive(configErrs, fmt.Sprintf("media_api.thumbnail_sizes[%d].height", i), int64(size.Height))
	}
}