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
- Variables
- func Dimensions(mime string, data []byte) (w, h int, ok bool)
- func Extension(mime string) string
- func SniffMime(b []byte) string
- func Thumbnail(mime string, data []byte) (out []byte, outMime string, ok bool)
- func ValidID(id string) bool
- type Limits
- type Repo
- type Service
- func (s *Service) AbsPath(rec jobstore.Attachment) (string, error)
- func (s *Service) DeleteUnsent(rec jobstore.Attachment) error
- func (s *Service) Get(id string) (jobstore.Attachment, bool, error)
- func (s *Service) MaxBytes() int64
- func (s *Service) Prune() (int, error)
- func (s *Service) Read(rec jobstore.Attachment) ([]byte, error)
- func (s *Service) ReadThumb(rec jobstore.Attachment) (data []byte, mime string, err error)
- func (s *Service) Root() string
- func (s *Service) Save(caller string, r io.Reader) (jobstore.Attachment, error)
- func (s *Service) SetClock(now func() time.Time)
Constants ¶
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.
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.
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 ¶
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 ¶
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 ¶
Extension is the file extension a sniffed MIME type conventionally has (with the dot).
func SniffMime ¶
SniffMime decides the type from the leading bytes only. It returns "" for anything that is not PNG, JPEG, GIF or WebP.
func Thumbnail ¶
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).
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 ¶
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) MaxBytes ¶
MaxBytes returns the live upload ceiling (for the HTTP layer's body cap).
func (*Service) Prune ¶
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 ¶
ReadThumb returns the thumbnail of rec (see Thumbnail; any failure returns the original).