storage

package
v0.0.0-...-d3cb701 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ThumbnailWidth  = 400
	ThumbnailHeight = 400
	AvatarSize      = 256
)

Thumbnail sizes

Variables

View Source
var AllowedImageTypes = map[string]string{
	"image/jpeg": ".jpg",
	"image/png":  ".png",
	"image/gif":  ".gif",
	"image/webp": ".webp",
}

AllowedImageTypes defines the allowed MIME types for uploads.

Functions

func GetContentTypeFromExtension

func GetContentTypeFromExtension(filename string) string

GetContentTypeFromExtension returns the content type for a file extension.

func GetExtensionForType

func GetExtensionForType(contentType string) string

GetExtensionForType returns the file extension for a content type.

func ValidateImageType

func ValidateImageType(contentType string) bool

ValidateImageType checks if the content type is allowed.

Types

type Config

type Config struct {
	AccountID       string
	AccessKeyID     string
	SecretAccessKey string
	BucketName      string
	PublicURL       string
	MaxFileSizeMB   int
}

Config holds storage configuration.

type EntityImageURLs

type EntityImageURLs struct {
	URL          string
	ThumbnailURL string
}

EntityImageURLs holds the URL and thumbnail URL for an entity's first image.

type EntityType

type EntityType string

EntityType represents the type of entity the image belongs to.

const (
	EntityTypeListing EntityType = "listings"
	EntityTypeRequest EntityType = "requests"
	EntityTypeAuction EntityType = "auctions"
	EntityTypeAvatar  EntityType = "avatars"
)

type Image

type Image struct {
	ID           uuid.UUID  `json:"id"`
	EntityType   EntityType `json:"entity_type"`
	EntityID     uuid.UUID  `json:"entity_id"`
	URL          string     `json:"url"`
	ThumbnailURL string     `json:"thumbnail_url,omitempty"`
	Filename     string     `json:"filename"`
	Size         int64      `json:"size"`
	MimeType     string     `json:"mime_type"`
	CreatedAt    time.Time  `json:"created_at"`
}

Image represents an uploaded image with metadata.

type Repository

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

Repository handles image metadata persistence.

func NewRepository

func NewRepository(db *pgxpool.Pool) *Repository

NewRepository creates a new image repository.

func (*Repository) CountByEntity

func (r *Repository) CountByEntity(ctx context.Context, entityType EntityType, entityID uuid.UUID) (int, error)

CountByEntity returns the number of images for an entity.

func (*Repository) Create

func (r *Repository) Create(ctx context.Context, img *Image) error

Create stores image metadata in the database.

func (*Repository) Delete

func (r *Repository) Delete(ctx context.Context, imageID uuid.UUID) (*Image, error)

Delete removes an image by ID.

func (*Repository) DeleteByEntity

func (r *Repository) DeleteByEntity(ctx context.Context, entityType EntityType, entityID uuid.UUID) ([]Image, error)

DeleteByEntity removes all images for an entity.

func (*Repository) GetByEntity

func (r *Repository) GetByEntity(ctx context.Context, entityType EntityType, entityID uuid.UUID) ([]Image, error)

GetByEntity returns all images for an entity.

func (*Repository) GetFirstByEntity

func (r *Repository) GetFirstByEntity(ctx context.Context, entityType EntityType, entityID uuid.UUID) (*Image, error)

GetFirstByEntity returns the first image for an entity (for thumbnails).

func (*Repository) GetImageURLsForEntities

func (r *Repository) GetImageURLsForEntities(ctx context.Context, entityType EntityType, entityIDs []uuid.UUID) (map[uuid.UUID]EntityImageURLs, error)

GetImageURLsForEntities returns the first image URLs for multiple entities (for list views).

type Service

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

Service handles image uploads to Cloudflare R2.

func NewService

func NewService(cfg Config) (*Service, error)

NewService creates a new storage service.

func (*Service) DeleteEntityImages

func (s *Service) DeleteEntityImages(ctx context.Context, entityType EntityType, entityID uuid.UUID) error

DeleteEntityImages deletes all images for an entity.

func (*Service) DeleteImage

func (s *Service) DeleteImage(ctx context.Context, key string) error

DeleteImage deletes an image by its key.

func (*Service) ExtractKeyFromURL

func (s *Service) ExtractKeyFromURL(url string) string

ExtractKeyFromURL extracts the object key from a public URL.

func (*Service) GetPublicURL

func (s *Service) GetPublicURL(key string) string

GetPublicURL returns the public URL for a file key.

func (*Service) UploadAvatar

func (s *Service) UploadAvatar(ctx context.Context, agentID uuid.UUID, data []byte) (string, error)

UploadAvatar uploads an agent avatar image (resized to standard size).

func (*Service) UploadImage

func (s *Service) UploadImage(ctx context.Context, entityType EntityType, entityID uuid.UUID, data []byte) (string, error)

UploadImage uploads an image and returns the public URL (no thumbnail).

func (*Service) UploadImageFromReader

func (s *Service) UploadImageFromReader(ctx context.Context, entityType EntityType, entityID uuid.UUID, reader io.Reader, contentType string) (string, error)

UploadImageFromReader uploads an image from an io.Reader.

func (*Service) UploadImageWithThumbnail

func (s *Service) UploadImageWithThumbnail(ctx context.Context, entityType EntityType, entityID uuid.UUID, data []byte) (*UploadResult, error)

UploadImageWithThumbnail uploads an image with automatic thumbnail generation.

type UploadResult

type UploadResult struct {
	URL          string `json:"url"`
	ThumbnailURL string `json:"thumbnail_url,omitempty"`
}

UploadResult contains URLs for the uploaded image and its thumbnail.

Jump to

Keyboard shortcuts

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