services

package
v0.3.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 29, 2026 License: MIT Imports: 32 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrTypeNotAllowed is returned by Upload for content of a type hako does
	// not take, or an image whose header cannot be read.
	ErrTypeNotAllowed = errors.New("file type not allowed")
	// ErrImageTooLarge is returned by Upload for an image whose width times
	// height is over the maximum.
	ErrImageTooLarge = errors.New("image has too many pixels")
)
View Source
var DefaultAllowedTypes = []string{
	"image/png", "image/jpeg", "image/gif", "image/webp",
	"application/pdf", "text/plain", "text/csv", "application/json",
}

DefaultAllowedTypes are images, PDF, plain text, CSV and JSON.

View Source
var ErrFileTooLarge = errors.New("file exceeds the maximum upload size")

ErrFileTooLarge is returned by Upload when a file holds more than the maximum upload size.

View Source
var ErrInfected = errors.New("file contains malware")

ErrInfected is what an *InfectedError is.

View Source
var ErrInvalidName = fmt.Errorf("a file name must be UTF-8 of at most %d bytes, without control or bidi characters", maxNameBytes)

ErrInvalidName is returned for a name that cannot be given to a file.

View Source
var ErrRateLimited = errors.New("upload rate exceeded")

ErrRateLimited is what a *RateLimitError is.

Functions

func CheckName added in v0.2.0

func CheckName(name string) error

CheckName returns ErrInvalidName when name cannot be given to a file.

func IsStoredFilename added in v0.2.0

func IsStoredFilename(filename string) bool

IsStoredFilename tells whether filename can be a stored file's: a UUID in its canonical form, and an extension. Any other, such as "../x", is not looked for in the store.

func ParseAllowedTypes added in v0.1.0

func ParseAllowedTypes(list string) ([]string, error)

ParseAllowedTypes reads a comma-separated list of media types such as "image/png, application/pdf". A blank list means DefaultAllowedTypes.

func RateKey added in v0.1.0

func RateKey(principal domain.Principal, remoteAddr string) string

RateKey is the key a principal's uploads are counted under: its ID, or the client's address for anonymous uploads, which all share one principal.

Types

type FileService

type FileService struct {
	// contains filtered or unexported fields
}

func NewFileService

func NewFileService(repo ports.FileRepository, storage *local.Storage, scanner ports.Scanner, policy UploadPolicy) *FileService

func (*FileService) Check added in v0.1.0

func (s *FileService) Check(ctx context.Context, originalFilename string, r io.Reader) error

Check returns the error Upload would refuse a file with, for its content, without storing anything. It scans the file too.

func (*FileService) Delete added in v0.3.1

func (s *FileService) Delete(ctx context.Context, principal domain.Principal, filename string) error

Delete removes the stored file of filename, for the principal that uploaded it. It fails with domain.ErrFileNotFound when there is none, and with domain.ErrNotUploader for any other principal. The content goes first, so the file stops being served; a failure after that is undone by a retry.

func (*FileService) GetFileInfo

func (s *FileService) GetFileInfo(ctx context.Context, idStr string) (*domain.File, error)

func (*FileService) GetFilePath

func (s *FileService) GetFilePath(filename string) string

func (*FileService) Upload

func (s *FileService) Upload(ctx context.Context, principal domain.Principal, originalFilename, name string, r io.Reader) (*domain.File, error)

Upload stores the content of r for principal, under a name made of a new UUID and the extension of the content's type: the client's, when it fits. A name, when given, also gives the file a slug for its named path. A refused file leaves nothing in storage or the repository. It fails with ErrInvalidName for its name, ErrTypeNotAllowed for its content, ErrFileTooLarge for its size, ErrImageTooLarge for its dimensions, an *InfectedError for malware, and ports.ErrScanUnavailable when it cannot be scanned.

type InfectedError added in v0.2.0

type InfectedError struct {
	Signature string
}

InfectedError refuses a file whose content matches a malware signature.

func (*InfectedError) Error added in v0.2.0

func (e *InfectedError) Error() string

func (*InfectedError) Is added in v0.2.0

func (e *InfectedError) Is(target error) bool

type RateLimitError added in v0.1.0

type RateLimitError struct {
	RetryAfter time.Duration
}

RateLimitError refuses an upload over its key's budget. RetryAfter is when the budget will hold it, or 0 when it never will: the upload asks for more than a full budget.

func (*RateLimitError) Error added in v0.1.0

func (e *RateLimitError) Error() string

func (*RateLimitError) Is added in v0.1.0

func (e *RateLimitError) Is(target error) bool

func (*RateLimitError) RetrySeconds added in v0.1.0

func (e *RateLimitError) RetrySeconds() int

RetrySeconds is RetryAfter in whole seconds, rounded up.

type RatePolicy added in v0.1.0

type RatePolicy struct {
	UploadsPerMinute int
	Burst            int
	BytesPerMinute   int64
}

RatePolicy is the budget of each key: UploadsPerMinute files a minute, with bursts of up to Burst files, and BytesPerMinute bytes a minute.

func (RatePolicy) Validate added in v0.1.0

func (p RatePolicy) Validate(maxUploadBytes int64) error

Validate refuses a policy that would refuse every upload, or the largest ones hako takes.

type UploadLimiter added in v0.1.0

type UploadLimiter struct {
	// contains filtered or unexported fields
}

UploadLimiter keeps a budget of files and bytes per key, in memory.

func NewUploadLimiter added in v0.1.0

func NewUploadLimiter(policy RatePolicy, now func() time.Time) *UploadLimiter

NewUploadLimiter returns a limiter applying policy, with now as its clock.

func (*UploadLimiter) Take added in v0.1.0

func (l *UploadLimiter) Take(key string, files int, bytes int64) error

Take spends files uploads and bytes bytes of key's budget. When the budget does not hold them, it spends nothing and returns a *RateLimitError.

type UploadPolicy added in v0.1.0

type UploadPolicy struct {
	// MaxUploadBytes caps each file.
	MaxUploadBytes int64
	// AllowedTypes, from ParseAllowedTypes, are the types a file's content
	// may have.
	AllowedTypes []string
	// MaxImagePixels caps an image's width times height.
	MaxImagePixels int64
}

UploadPolicy is what a file must satisfy to be stored.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址