Documentation
¶
Index ¶
- Constants
- Variables
- func GetContentTypeFromExtension(filename string) string
- func GetExtensionForType(contentType string) string
- func ValidateImageType(contentType string) bool
- type Config
- type EntityImageURLs
- type EntityType
- type Image
- type Repository
- func (r *Repository) CountByEntity(ctx context.Context, entityType EntityType, entityID uuid.UUID) (int, error)
- func (r *Repository) Create(ctx context.Context, img *Image) error
- func (r *Repository) Delete(ctx context.Context, imageID uuid.UUID) (*Image, error)
- func (r *Repository) DeleteByEntity(ctx context.Context, entityType EntityType, entityID uuid.UUID) ([]Image, error)
- func (r *Repository) GetByEntity(ctx context.Context, entityType EntityType, entityID uuid.UUID) ([]Image, error)
- func (r *Repository) GetFirstByEntity(ctx context.Context, entityType EntityType, entityID uuid.UUID) (*Image, error)
- func (r *Repository) GetImageURLsForEntities(ctx context.Context, entityType EntityType, entityIDs []uuid.UUID) (map[uuid.UUID]EntityImageURLs, error)
- type Service
- func (s *Service) DeleteEntityImages(ctx context.Context, entityType EntityType, entityID uuid.UUID) error
- func (s *Service) DeleteImage(ctx context.Context, key string) error
- func (s *Service) ExtractKeyFromURL(url string) string
- func (s *Service) GetPublicURL(key string) string
- func (s *Service) UploadAvatar(ctx context.Context, agentID uuid.UUID, data []byte) (string, error)
- func (s *Service) UploadImage(ctx context.Context, entityType EntityType, entityID uuid.UUID, data []byte) (string, error)
- func (s *Service) UploadImageFromReader(ctx context.Context, entityType EntityType, entityID uuid.UUID, ...) (string, error)
- func (s *Service) UploadImageWithThumbnail(ctx context.Context, entityType EntityType, entityID uuid.UUID, data []byte) (*UploadResult, error)
- type UploadResult
Constants ¶
const ( ThumbnailWidth = 400 ThumbnailHeight = 400 AvatarSize = 256 )
Thumbnail sizes
Variables ¶
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 ¶
GetContentTypeFromExtension returns the content type for a file extension.
func GetExtensionForType ¶
GetExtensionForType returns the file extension for a content type.
func ValidateImageType ¶
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 ¶
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) 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 ¶
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 ¶
DeleteImage deletes an image by its key.
func (*Service) ExtractKeyFromURL ¶
ExtractKeyFromURL extracts the object key from a public URL.
func (*Service) GetPublicURL ¶
GetPublicURL returns the public URL for a file key.
func (*Service) UploadAvatar ¶
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.