Documentation
¶
Index ¶
- Constants
- Variables
- func DecodeImage(data []byte) (image.Image, error)
- func DeletePackImage(c *gin.Context)
- func EncodeToJPEG(img image.Image) ([]byte, error)
- func GetPackImage(c *gin.Context)
- func ResizeImage(img image.Image) image.Image
- func UploadPackImage(c *gin.Context)
- func ValidateImageFormat(data []byte) (string, error)
- type DBImageStorage
- func (s *DBImageStorage) Delete(ctx context.Context, packID uint) error
- func (s *DBImageStorage) Exists(ctx context.Context, packID uint) (bool, error)
- func (s *DBImageStorage) Get(ctx context.Context, packID uint) (*Image, error)
- func (s *DBImageStorage) Save(ctx context.Context, packID uint, data []byte, metadata ImageMetadata) error
- type Image
- type ImageMetadata
- type ImageStorage
- type ProcessedImage
Constants ¶
const ( // MaxUploadSize is the maximum file size for uploads (5 MB) MaxUploadSize = 5 * 1024 * 1024 // MaxProcessedSize is the maximum size after processing (5 MB) MaxProcessedSize = 5 * 1024 * 1024 // MaxDimension is the maximum width or height for processed images MaxDimension = 1920 // JPEGQuality is the quality setting for JPEG encoding (0-100) JPEGQuality = 85 // MimeTypeJPEG is the MIME type for JPEG images MimeTypeJPEG = "image/jpeg" // MimeTypePNG is the MIME type for PNG images MimeTypePNG = "image/png" // MimeTypeWebP is the MIME type for WebP images MimeTypeWebP = "image/webp" )
Variables ¶
var ( // ErrNotFound is returned when an image is not found ErrNotFound = errors.New("image not found") // ErrInvalidFormat is returned when the image format is not supported ErrInvalidFormat = errors.New("invalid image format") // ErrTooLarge is returned when the image exceeds size limits ErrTooLarge = errors.New("image too large") // ErrCorrupted is returned when the image is corrupted ErrCorrupted = errors.New("image corrupted") )
Functions ¶
func DecodeImage ¶
DecodeImage decodes an image from bytes
func DeletePackImage ¶
DeletePackImage deletes an image for a pack @Summary Delete pack image @Description Delete the image for a pack. Only the pack owner can delete images. @Security Bearer @Tags Pack Images @Produce json @Param id path int true "Pack ID" @Success 200 {object} map[string]interface{} "Image deleted successfully" @Failure 400 {object} map[string]string "Invalid pack ID" @Failure 401 {object} map[string]string "Unauthorized" @Failure 403 {object} map[string]string "Pack does not belong to user" @Failure 404 {object} map[string]string "Pack not found" @Failure 500 {object} map[string]string "Internal server error" @Router /v1/mypack/{id}/image [delete]
func EncodeToJPEG ¶
EncodeToJPEG encodes an image to JPEG format with specified quality This also effectively strips EXIF data as we're re-encoding from scratch
func GetPackImage ¶
GetPackImage retrieves an image for a pack @Summary Get pack image @Description Get the image for a pack. Public packs are accessible without authentication. @Tags Pack Images @Produce image/jpeg @Param id path int true "Pack ID" @Success 200 {file} image/jpeg "Pack image" @Failure 400 {string} string "Invalid pack ID" @Failure 401 {string} string "Unauthorized (for private packs)" @Failure 403 {string} string "Forbidden (for private packs not owned by user)" @Failure 404 {string} string "Pack not found or has no image" @Failure 500 {string} string "Internal server error" @Router /v1/packs/{id}/image [get]
func ResizeImage ¶
ResizeImage resizes an image if it exceeds MaxDimension, maintaining aspect ratio
func UploadPackImage ¶
UploadPackImage uploads or updates an image for a pack @Summary Upload or update pack image @Description Upload or update an image for a pack. Only the pack owner can upload images. @Security Bearer @Tags Pack Images @Accept multipart/form-data @Produce json @Param id path int true "Pack ID" @Param image formData file true "Image file (JPEG, PNG, or WebP, max 5MB)" @Success 200 {object} map[string]interface{} "Image uploaded successfully" @Failure 400 {object} map[string]string "Invalid request or image" @Failure 401 {object} map[string]string "Unauthorized" @Failure 403 {object} map[string]string "Pack does not belong to user" @Failure 404 {object} map[string]string "Pack not found" @Failure 413 {object} map[string]string "Payload too large" @Failure 500 {object} map[string]string "Internal server error" @Router /v1/mypack/{id}/image [post]
func ValidateImageFormat ¶
ValidateImageFormat checks if the image format is supported by examining magic bytes
Types ¶
type DBImageStorage ¶
type DBImageStorage struct{}
DBImageStorage implements ImageStorage using PostgreSQL database
func NewDBImageStorage ¶
func NewDBImageStorage() *DBImageStorage
NewDBImageStorage creates a new database image storage instance
func (*DBImageStorage) Delete ¶
func (s *DBImageStorage) Delete(ctx context.Context, packID uint) error
Delete removes an image for a pack from the database Returns nil if the image doesn't exist (idempotent)
func (*DBImageStorage) Get ¶
Get retrieves an image for a pack from the database Returns ErrNotFound if the image doesn't exist
func (*DBImageStorage) Save ¶
func (s *DBImageStorage) Save(ctx context.Context, packID uint, data []byte, metadata ImageMetadata) error
Save stores or updates an image for a pack in the database Uses UPSERT (INSERT ... ON CONFLICT ... DO UPDATE) for idempotent behavior
type Image ¶
type Image struct {
Data []byte
Metadata ImageMetadata
}
Image represents an image with its data and metadata
type ImageMetadata ¶
ImageMetadata contains metadata about a processed image
type ImageStorage ¶
type ImageStorage interface {
// Save stores an image for a pack
Save(ctx context.Context, packID uint, data []byte, metadata ImageMetadata) error
// Get retrieves an image for a pack
// Returns ErrNotFound if the image doesn't exist
Get(ctx context.Context, packID uint) (*Image, error)
// Delete removes an image for a pack
// Returns nil if the image doesn't exist (idempotent)
Delete(ctx context.Context, packID uint) error
// Exists checks if an image exists for a pack
Exists(ctx context.Context, packID uint) (bool, error)
}
ImageStorage defines the interface for image storage operations This allows for different storage backends (database, S3, etc.)
type ProcessedImage ¶
type ProcessedImage struct {
Data []byte
Metadata ImageMetadata
}
ProcessedImage contains the processed image data and metadata
func ProcessImage ¶
func ProcessImage(data []byte) (*ProcessedImage, error)
ProcessImage validates and processes an uploaded image Returns the processed image data and metadata
func ProcessImageFromReader ¶
func ProcessImageFromReader(reader io.Reader) (*ProcessedImage, error)
ProcessImageFromReader processes an image from an io.Reader