diff --git a/config.sample.yaml b/config.sample.yaml index 6a3c0a58..f7e0416d 100644 --- a/config.sample.yaml +++ b/config.sample.yaml @@ -31,6 +31,13 @@ admins: uploads: maxBytes: 104857600 # 100MB default, 0 to disable + # The number of bytes to claim as the maximum size for uploads for the limits API. If this + # is not provided then the maxBytes setting will be used instead. This is useful to provide + # if the media repo's settings and the reverse proxy do not match for maximum request size. + # This is purely for informational reasons and does not actually limit any functionality. + # Set this to -1 to indicate that there is no limit. Zero will force the use of maxBytes. + #reportedMaxBytes: 104857600 + # This is intended for larger deployments where media should be distributed among other # directories, drives, servers, etc. For smaller deployments, a single entry in this list # is recommended. diff --git a/src/github.com/turt2live/matrix-media-repo/api/r0/limits.go b/src/github.com/turt2live/matrix-media-repo/api/r0/limits.go new file mode 100644 index 00000000..c4527b5f --- /dev/null +++ b/src/github.com/turt2live/matrix-media-repo/api/r0/limits.go @@ -0,0 +1,28 @@ +package r0 + +import ( + "net/http" + + "github.com/sirupsen/logrus" + "github.com/turt2live/matrix-media-repo/api" + "github.com/turt2live/matrix-media-repo/config" +) + +type LimitsResponse struct { + UploadMaxSize int64 `json:"m.upload.size,omitempty"` +} + +func Limits(r *http.Request, log *logrus.Entry, user api.UserInfo) interface{} { + uploadSize := config.Get().Uploads.ReportedMaxSizeBytes + if uploadSize == 0 { + uploadSize = config.Get().Uploads.MaxSizeBytes + } + + if uploadSize < 0 { + uploadSize = 0 // invokes the omitEmpty + } + + return &LimitsResponse{ + UploadMaxSize: uploadSize, + } +} diff --git a/src/github.com/turt2live/matrix-media-repo/api/webserver/webserver.go b/src/github.com/turt2live/matrix-media-repo/api/webserver/webserver.go index 10685176..adf54813 100644 --- a/src/github.com/turt2live/matrix-media-repo/api/webserver/webserver.go +++ b/src/github.com/turt2live/matrix-media-repo/api/webserver/webserver.go @@ -34,6 +34,7 @@ func Init() { quarantineRoomHandler := handler{api.AccessTokenRequiredRoute(r0.QuarantineRoomMedia), counter} localCopyHandler := handler{api.AccessTokenRequiredRoute(r0.LocalCopy), counter} infoHandler := handler{api.AccessTokenRequiredRoute(r0.MediaInfo), counter} + limitsHandler := handler{api.AccessTokenRequiredRoute(r0.Limits), counter} routes := make(map[string]route) versions := []string{"r0", "v1"} // r0 is typically clients and v1 is typically servers. v1 is deprecated. @@ -46,6 +47,7 @@ func Init() { routes["/_matrix/media/"+version+"/thumbnail/{server:[a-zA-Z0-9.:\\-_]+}/{mediaId:[a-zA-Z0-9.\\-_]+}"] = route{"GET", thumbnailHandler} routes["/_matrix/media/"+version+"/preview_url"] = route{"GET", previewUrlHandler} routes["/_matrix/media/"+version+"/identicon/{seed:.*}"] = route{"GET", identiconHandler} + routes["/_matrix/media/"+version+"/limits"] = route{"GET", limitsHandler} // Routes that we define but are not part of the spec routes["/_matrix/media/"+version+"/admin/purge_remote"] = route{"POST", purgeHandler} diff --git a/src/github.com/turt2live/matrix-media-repo/config/config.go b/src/github.com/turt2live/matrix-media-repo/config/config.go index 8c8dc7bf..97118ee3 100644 --- a/src/github.com/turt2live/matrix-media-repo/config/config.go +++ b/src/github.com/turt2live/matrix-media-repo/config/config.go @@ -33,9 +33,10 @@ type DatabaseConfig struct { } type UploadsConfig struct { - StoragePaths []string `yaml:"storagePaths,flow"` - MaxSizeBytes int64 `yaml:"maxBytes"` - AllowedTypes []string `yaml:"allowedTypes,flow"` + StoragePaths []string `yaml:"storagePaths,flow"` + MaxSizeBytes int64 `yaml:"maxBytes"` + AllowedTypes []string `yaml:"allowedTypes,flow"` + ReportedMaxSizeBytes int64 `yaml:"reportedMaxBytes"` } type DownloadsConfig struct { @@ -187,9 +188,10 @@ func NewDefaultConfig() *MediaRepoConfig { Homeservers: []*HomeserverConfig{}, Admins: []string{}, Uploads: &UploadsConfig{ - MaxSizeBytes: 104857600, // 100mb - StoragePaths: []string{}, - AllowedTypes: []string{"*/*"}, + MaxSizeBytes: 104857600, // 100mb + ReportedMaxSizeBytes: 0, + StoragePaths: []string{}, + AllowedTypes: []string{"*/*"}, }, Downloads: &DownloadsConfig{ MaxSizeBytes: 104857600, // 100mb