100 lines
3.5 KiB
Go
100 lines
3.5 KiB
Go
// Package files is the shared private file layer over the ObjectStorage port.
|
|
// It owns technical rules common to every stored file: opaque keys, declared
|
|
// content types, streaming size limits, checksums, and cleanup of failed
|
|
// writes. Feature packages own their policies and every product rule, such as
|
|
// who may read a file, page limits, parsing, and retention.
|
|
package files
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"mime"
|
|
"strings"
|
|
|
|
"lincodex/backend/internal/port/outbound"
|
|
)
|
|
|
|
const maxNamespaceLength = 32
|
|
|
|
var (
|
|
ErrInvalidPolicy = errors.New("invalid file policy")
|
|
ErrInvalidUpload = errors.New("invalid file upload")
|
|
ErrInvalidKey = errors.New("invalid file key")
|
|
ErrUnsupportedType = errors.New("unsupported file content type")
|
|
ErrTooLarge = errors.New("file exceeds size limit")
|
|
ErrSizeMismatch = errors.New("file size differs from declared size")
|
|
// ErrNotFound is the port sentinel, so callers need not import the port.
|
|
ErrNotFound = outbound.ErrObjectNotFound
|
|
)
|
|
|
|
// Policy is the storage rule for one kind of file. A feature package defines
|
|
// it once; the store enforces it on every write, read, and deletion.
|
|
type Policy struct {
|
|
namespace Namespace
|
|
maxSize int64
|
|
contentTypes map[string]struct{}
|
|
}
|
|
|
|
// NewPolicy accepts a declared namespace, a positive size limit in bytes, and
|
|
// exact media types without parameters or wildcards.
|
|
func NewPolicy(namespace Namespace, maxSize int64, contentTypes ...string) (Policy, error) {
|
|
if !registered(namespace) || !validNamespace(namespace) {
|
|
return Policy{}, fmt.Errorf("files.NewPolicy - validate namespace: %w", ErrInvalidPolicy)
|
|
}
|
|
if maxSize <= 0 {
|
|
return Policy{}, fmt.Errorf("files.NewPolicy - validate size limit: %w", ErrInvalidPolicy)
|
|
}
|
|
if len(contentTypes) == 0 {
|
|
return Policy{}, fmt.Errorf("files.NewPolicy - validate content types: %w", ErrInvalidPolicy)
|
|
}
|
|
allowed := make(map[string]struct{}, len(contentTypes))
|
|
for _, contentType := range contentTypes {
|
|
mediaType, ok := mediaTypeOf(contentType)
|
|
if !ok || mediaType != contentType || strings.Contains(mediaType, "*") {
|
|
return Policy{}, fmt.Errorf("files.NewPolicy - validate content type: %w", ErrInvalidPolicy)
|
|
}
|
|
allowed[mediaType] = struct{}{}
|
|
}
|
|
return Policy{namespace: namespace, maxSize: maxSize, contentTypes: allowed}, nil
|
|
}
|
|
|
|
func (policy Policy) Namespace() Namespace { return policy.namespace }
|
|
|
|
func (policy Policy) MaxSize() int64 { return policy.maxSize }
|
|
|
|
func (policy Policy) valid() bool { return policy.namespace != "" }
|
|
|
|
// allow returns the bare media type stored with the object. Parameters are
|
|
// dropped so a caller-declared value cannot smuggle data into the header
|
|
// that a later download response repeats.
|
|
func (policy Policy) allow(contentType string) (string, bool) {
|
|
mediaType, ok := mediaTypeOf(contentType)
|
|
if !ok {
|
|
return "", false
|
|
}
|
|
_, allowed := policy.contentTypes[mediaType]
|
|
return mediaType, allowed
|
|
}
|
|
|
|
func mediaTypeOf(contentType string) (string, bool) {
|
|
mediaType, _, err := mime.ParseMediaType(contentType)
|
|
if err != nil || !strings.Contains(mediaType, "/") {
|
|
return "", false
|
|
}
|
|
return mediaType, true
|
|
}
|
|
|
|
// validNamespace requires lowercase letters, digits, and hyphens, starting
|
|
// with a letter, so a namespace is one safe key path element.
|
|
func validNamespace(namespace Namespace) bool {
|
|
if namespace == "" || len(namespace) > maxNamespaceLength || namespace[0] < 'a' || namespace[0] > 'z' {
|
|
return false
|
|
}
|
|
for _, char := range namespace {
|
|
if char != '-' && (char < 'a' || char > 'z') && (char < '0' || char > '9') {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|