attachment

package
v0.218.0 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2026 License: MIT Imports: 23 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package attachment stores the images a user uploads before sending them with a workbench message (design 2026-10-19-workbench-image-attachments-design.md 「附件存储(server)」).

It owns the file side — type validation by magic bytes, dimensions, the on-disk layout <root>/<yyyymmdd>/<id>, thumbnails and the retention sweep — while the index rows live in jobstore (the attachments table). Ownership and thread-visibility rules are the HTTP layer's business; this package only knows ids and bytes.

Index

Constants

View Source
const (
	DefaultMaxBytes        int64 = 10 << 20
	DefaultMaxPerMessage         = 6
	DefaultHourlyPerCaller       = 200
)

Defaults of the server.attachments / storage.attachment_retention_days settings; the config package owns the yaml surface and passes the resolved numbers in through Limits.

View Source
const (
	MimePNG  = "image/png"
	MimeJPEG = "image/jpeg"
	MimeGIF  = "image/gif"
	MimeWebP = "image/webp"
)

The four accepted MIME types. Nothing else is stored: in particular SVG (scriptable) and HTML are refused whatever the file name or the declared Content-Type says.

View Source
const (
	ThumbSide = 320
)

ThumbSide is the longest side of a thumbnail in pixels. thumbMaxPixels bounds what is decoded to make one (an RGBA copy costs 4 bytes per pixel, twice): larger images keep their original.

Variables

View Source
var (
	// ErrTooLarge: the upload exceeds Limits.MaxBytes (413).
	ErrTooLarge = errors.New("attachment: file too large")
	// ErrUnsupportedType: the bytes are not a PNG / JPEG / GIF / WebP image, or the image
	// header is damaged / absurdly large (415).
	ErrUnsupportedType = errors.New("attachment: unsupported or invalid image type")
	// ErrRateLimited: the caller exceeded Limits.HourlyPerCaller (429).
	ErrRateLimited = errors.New("attachment: hourly upload limit reached")
	// ErrNotFound: unknown id, or an id that cannot be one.
	ErrNotFound = errors.New("attachment: not found")
)

Errors a caller maps to HTTP statuses.

Functions

func Dimensions

func Dimensions(mime string, data []byte) (w, h int, ok bool)

Dimensions reads the pixel size of an already sniffed image. PNG / JPEG / GIF go through the standard library's header decoder; WebP is read from its RIFF header by hand (x/image is not a dependency). ok is false when the header is damaged or the size is zero.

func Extension

func Extension(mime string) string

Extension is the file extension a sniffed MIME type conventionally has (with the dot).

func SniffMime

func SniffMime(b []byte) string

SniffMime decides the type from the leading bytes only. It returns "" for anything that is not PNG, JPEG, GIF or WebP.

func Thumbnail

func Thumbnail(mime string, data []byte) (out []byte, outMime string, ok bool)

Thumbnail renders a thumbnail of data (longest side ThumbSide, JPEG for a JPEG source, PNG otherwise). It never fails the caller: on ANY problem — a WebP (no decoder is linked), a corrupt or oversized image, an encoder error — it returns the original bytes and mime with ok=false. An image already within ThumbSide is returned as is (ok=true).

func ValidID

func ValidID(id string) bool

ValidID reports whether id looks like an attachment id.

Types

type Limits

type Limits struct {
	MaxBytes        int64
	HourlyPerCaller int
	// Retention is how long a new attachment is kept; <= 0 = keep forever (expires_at 0).
	Retention time.Duration
}

Limits are the live numbers of one upload (read per call through the Service's limits func, so a config reload applies to the next upload).

type Repo

type Repo interface {
	InsertAttachment(a jobstore.Attachment) error
	GetAttachment(id string) (jobstore.Attachment, bool, error)
	DeleteUnsentAttachment(id string) error
	PruneAttachments(now int64) ([]string, error)
}

Repo is the slice of jobstore.Store the service uses.

type Service

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

Service is the attachment store.

func New

func New(root string, repo Repo, limits func() Limits) *Service

New builds a Service rooted at root (the directory is created lazily on the first upload). limits is called per operation; nil takes the package defaults.

func (*Service) AbsPath

func (s *Service) AbsPath(rec jobstore.Attachment) (string, error)

AbsPath is the absolute path of rec's stored original on this server (the source of a file delivery); it refuses a stored path that escapes the root.

func (*Service) DeleteUnsent

func (s *Service) DeleteUnsent(rec jobstore.Attachment) error

DeleteUnsent removes an unsent attachment (row first, then file). The repo returns jobstore.ErrAttachmentBound / ErrAttachmentNotFound unchanged.

func (*Service) Get

func (s *Service) Get(id string) (jobstore.Attachment, bool, error)

Get reads the index row of id; ok is false for an unknown or malformed id.

func (*Service) MaxBytes

func (s *Service) MaxBytes() int64

MaxBytes returns the live upload ceiling (for the HTTP layer's body cap).

func (*Service) Prune

func (s *Service) Prune() (int, error)

Prune deletes the rows expired at now() and their files, then removes day directories that became empty. It returns the number of attachments removed. A file that cannot be removed is skipped (its row is already gone; the next sweep does not see it again, so it is reported in the error but never blocks the rest).

func (*Service) Read

func (s *Service) Read(rec jobstore.Attachment) ([]byte, error)

Read returns the stored original of rec. A row whose file is gone (pruned by hand, expired mid-request) is ErrNotFound.

func (*Service) ReadThumb

func (s *Service) ReadThumb(rec jobstore.Attachment) (data []byte, mime string, err error)

ReadThumb returns the thumbnail of rec (see Thumbnail; any failure returns the original).

func (*Service) Root

func (s *Service) Root() string

Root returns the storage root.

func (*Service) Save

func (s *Service) Save(caller string, r io.Reader) (jobstore.Attachment, error)

Save validates and stores one upload from r for caller. Order of refusals: rate limit (ErrRateLimited), size (ErrTooLarge), type (ErrUnsupportedType). The returned row is unsent (no thread).

func (*Service) SetClock

func (s *Service) SetClock(now func() time.Time)

SetClock replaces the time source (tests).

Jump to

Keyboard shortcuts

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