fleet/server/contexts/installersize/installersize.go
Magnus Jensen da43bf8371
Request body limits (#39080)
<!-- Add the related story/sub-task/bug number, like Resolves #123, or
remove if NA -->
**Related issue:** Resolves
https://github.com/fleetdm/confidential/issues/13934

# Checklist for submitter

If some of the following don't apply, delete the relevant line.

- [ ] Changes file added for user-visible changes in `changes/`,
`orbit/changes/` or `ee/fleetd-chrome/changes`.
See [Changes
files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/guides/committing-changes.md#changes-files)
for more information.

## Testing

- [x] Added/updated automated tests
- [ ] QA'd all new/changed functionality manually
2026-02-05 10:29:53 -05:00

40 lines
1 KiB
Go

// Package installersize provides context handling for the maximum software installer size.
package installersize
import (
"context"
"github.com/docker/go-units"
)
const MaxSoftwareInstallerSize int64 = 10 * units.GiB
// Human formats a byte size into a human-readable string.
// It evaluates both SI units (KB, MB, GB) and binary units (KiB, MiB, GiB)
// and returns whichever representation is shorter.
func Human(bytes int64) string {
si := units.HumanSize(float64(bytes))
binary := units.BytesSize(float64(bytes))
if len(binary) < len(si) {
return binary
}
return si
}
type key struct{}
// NewContext returns a new context with the max installer size value.
func NewContext(ctx context.Context, maxSize int64) context.Context {
return context.WithValue(ctx, key{}, maxSize)
}
// FromContext returns the max installer size from the context if present.
// If not present, returns DefaultMaxInstallerSize.
func FromContext(ctx context.Context) int64 {
v, ok := ctx.Value(key{}).(int64)
if !ok {
return MaxSoftwareInstallerSize
}
return v
}