attachment

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2026 License: AGPL-3.0 Imports: 12 Imported by: 0

Documentation

Overview

Package attachment handles temporary attachment storage and secure token-based retrieval.

Index

Constants

View Source
const (
	// MaxAttachmentSize is the maximum allowed plaintext size for an attachment in bytes (1 MB).
	MaxAttachmentSize = 1024 * 1024
	// AttachmentTTL is how long attachments are retained before expiry.
	// The 6-hour window matches BeeBuzz's real-time alerting model and limits token exposure.
	AttachmentTTL = 6 * time.Hour
)

Variables

View Source
var ErrAttachmentExpired = fmt.Errorf("attachment expired: %w", core.ErrNotFound)

Functions

This section is empty.

Types

type DBAttachment

type DBAttachment struct {
	ID            string `db:"id"`
	Token         string `db:"token"`
	TopicID       string `db:"topic_id"`
	MimeType      string `db:"mime_type"`
	FileSizeBytes int    `db:"file_size_bytes"`
	CreatedAt     int64  `db:"created_at"`
	ExpiresAt     int64  `db:"expires_at"`
}

type Handler

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

Handler handles attachment HTTP requests.

func NewHandler

func NewHandler(svc *Service, logger *slog.Logger) *Handler

NewHandler creates a new attachment handler.

func (*Handler) Get

func (h *Handler) Get(w http.ResponseWriter, r *http.Request)

Get handles GET /v1/attachments/{token}. No session auth — access is controlled by the opaque token in the URL.

type Repository

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

Repository provides data access for the attachment domain.

func NewRepository

func NewRepository(db *sqlx.DB) *Repository

NewRepository creates a new attachment repository.

func (*Repository) Create

func (r *Repository) Create(ctx context.Context, topicID, mimeType string, fileSizeBytes int, ttl time.Duration) (*DBAttachment, error)

Create creates a new attachment record.

func (*Repository) DeleteExpired

func (r *Repository) DeleteExpired(ctx context.Context) error

DeleteExpired deletes all expired attachments.

func (*Repository) GetByToken

func (r *Repository) GetByToken(ctx context.Context, token string) (*DBAttachment, error)

GetByToken retrieves an attachment by token. Returns nil, nil if not found.

func (*Repository) ListExpired

func (r *Repository) ListExpired(ctx context.Context) ([]DBAttachment, error)

ListExpired returns all attachment records where expires_at is in the past.

type Service

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

Service provides attachment business logic.

func NewService

func NewService(repo *Repository, attachmentsDir string, log *slog.Logger) *Service

NewService creates a new attachment service.

func (*Service) CleanupExpired

func (s *Service) CleanupExpired(ctx context.Context) error

CleanupExpired removes expired attachment files from disk and their DB records.

func (*Service) GetByToken

func (s *Service) GetByToken(ctx context.Context, token string) ([]byte, *DBAttachment, error)

GetByToken retrieves the raw (ciphertext) bytes and metadata for an attachment token. Returns core.ErrNotFound if the token does not exist, ErrAttachmentExpired if expired.

func (*Service) Store

func (s *Service) Store(ctx context.Context, topicID, mimeType string, originalSize int, data []byte) (string, error)

Store persists already-encrypted ciphertext for a topic attachment and returns the access token. originalSize is the plaintext byte count, stored for UI display.

Jump to

Keyboard shortcuts

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