Documentation
¶
Index ¶
- Variables
- func CheckName(name string) error
- func IsStoredFilename(filename string) bool
- func ParseAllowedTypes(list string) ([]string, error)
- func RateKey(principal domain.Principal, remoteAddr string) string
- type FileService
- func (s *FileService) Check(ctx context.Context, originalFilename string, r io.Reader) error
- func (s *FileService) Delete(ctx context.Context, principal domain.Principal, filename string) error
- func (s *FileService) GetFileInfo(ctx context.Context, idStr string) (*domain.File, error)
- func (s *FileService) GetFilePath(filename string) string
- func (s *FileService) Upload(ctx context.Context, principal domain.Principal, originalFilename, name string, ...) (*domain.File, error)
- type InfectedError
- type RateLimitError
- type RatePolicy
- type UploadLimiter
- type UploadPolicy
Constants ¶
This section is empty.
Variables ¶
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") )
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.
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.
var ErrInfected = errors.New("file contains malware")
ErrInfected is what an *InfectedError is.
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.
var ErrRateLimited = errors.New("upload rate exceeded")
ErrRateLimited is what a *RateLimitError is.
Functions ¶
func CheckName ¶ added in v0.2.0
CheckName returns ErrInvalidName when name cannot be given to a file.
func IsStoredFilename ¶ added in v0.2.0
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
ParseAllowedTypes reads a comma-separated list of media types such as "image/png, application/pdf". A blank list means DefaultAllowedTypes.
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
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 (*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
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
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.
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.