images

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2026 License: GPL-3.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
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

View Source
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

func DecodeImage(data []byte) (image.Image, error)

DecodeImage decodes an image from bytes

func DeletePackImage

func DeletePackImage(c *gin.Context)

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

func EncodeToJPEG(img image.Image) ([]byte, error)

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

func GetPackImage(c *gin.Context)

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

func ResizeImage(img image.Image) image.Image

ResizeImage resizes an image if it exceeds MaxDimension, maintaining aspect ratio

func UploadPackImage

func UploadPackImage(c *gin.Context)

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

func ValidateImageFormat(data []byte) (string, error)

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) Exists

func (s *DBImageStorage) Exists(ctx context.Context, packID uint) (bool, error)

Exists checks if an image exists for a pack

func (*DBImageStorage) Get

func (s *DBImageStorage) Get(ctx context.Context, packID uint) (*Image, error)

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

type ImageMetadata struct {
	MimeType string
	FileSize int
	Width    int
	Height   int
}

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

Jump to

Keyboard shortcuts

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