images

package
v1.7.0 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2026 License: GPL-3.0 Imports: 22 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
	// MaxProfilePicDimension is the maximum width or height for profile pictures
	MaxProfilePicDimension = 512
	// MaxInventoryItemDimension is the maximum width or height for inventory item pictures
	MaxInventoryItemDimension = 400
	// JPEGQuality is the quality setting for JPEG encoding (0-100)
	JPEGQuality = 85
	// InventoryItemJPEGQuality is the quality setting for inventory item images (0-100)
	InventoryItemJPEGQuality = 70
	// BannerJPEGQuality is the quality setting for banner images (0-100)
	BannerJPEGQuality = 95
	// MaxBannerWidth is the maximum width for banner images
	MaxBannerWidth = 1920
	// MaxBannerHeight is the maximum height for banner images
	MaxBannerHeight = 600
	// 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"
)
View Source
const (
	// ErrMsgNoImageProvided is the error message when no image file is provided
	ErrMsgNoImageProvided = "no image file provided"
)

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 DeleteInventoryItemImage added in v1.3.0

func DeleteInventoryItemImage(c *gin.Context)

DeleteInventoryItemImage deletes an image for an inventory item @Summary Delete inventory item image @Description Delete the image for an inventory item. Only the item owner can delete images. @Security Bearer @Tags Inventory Images @Produce json @Param id path int true "Inventory Item ID" @Success 200 {object} map[string]interface{} "Image deleted successfully" @Failure 400 {object} map[string]string "Invalid item ID" @Failure 401 {object} map[string]string "Unauthorized" @Failure 403 {object} map[string]string "Item does not belong to user" @Failure 404 {object} map[string]string "Item not found" @Failure 500 {object} map[string]string "Internal server error" @Router /v1/myinventory/{id}/image [delete]

func DeleteMyBannerImage added in v1.6.0

func DeleteMyBannerImage(c *gin.Context)

DeleteMyBannerImage deletes the banner image for the current user @Summary Delete banner image @Description Delete the banner/hero image for the currently logged-in user @Security Bearer @Tags Account Images @Produce json @Success 200 {object} map[string]interface{} "Banner image deleted successfully" @Failure 401 {object} map[string]string "Unauthorized" @Failure 500 {object} map[string]string "Internal server error" @Router /v1/myaccount/banner [delete]

func DeleteMyProfileImage added in v1.2.0

func DeleteMyProfileImage(c *gin.Context)

DeleteMyProfileImage deletes the profile image for the current user @Summary Delete profile image @Description Delete the profile image for the currently logged-in user @Security Bearer @Tags Account Images @Produce json @Success 200 {object} map[string]interface{} "Image deleted successfully" @Failure 401 {object} map[string]string "Unauthorized" @Failure 500 {object} map[string]string "Internal server error" @Router /v1/myaccount/image [delete]

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 default quality (JPEGQuality)

func EncodeToJPEGWithQuality added in v1.6.0

func EncodeToJPEGWithQuality(img image.Image, quality int) ([]byte, error)

EncodeToJPEGWithQuality encodes an image to JPEG format with the given quality (1-100) Values outside the range are clamped. This also effectively strips EXIF data.

func GetBannerImage added in v1.6.0

func GetBannerImage(c *gin.Context)

GetBannerImage retrieves the banner image for a given account @Summary Get banner image @Description Get the banner/hero image for a user account @Tags Account Images @Produce image/jpeg @Param id path int true "Account ID" @Success 200 {file} image/jpeg "Banner image" @Failure 400 {string} string "Invalid account ID" @Failure 404 {string} string "No banner image found" @Failure 500 {string} string "Internal server error" @Router /v1/accounts/{id}/banner [get]

func GetInventoryItemImage added in v1.3.0

func GetInventoryItemImage(c *gin.Context)

GetInventoryItemImage retrieves an image for an inventory item @Summary Get inventory item image @Description Get the image for an inventory item. Only the item owner can view the image. @Security Bearer @Tags Inventory Images @Produce image/jpeg @Param id path int true "Inventory Item ID" @Success 200 {file} image/jpeg "Inventory item image" @Failure 400 {string} string "Invalid item ID" @Failure 401 {string} string "Unauthorized" @Failure 403 {string} string "Forbidden" @Failure 404 {string} string "Item not found or has no image" @Failure 500 {string} string "Internal server error" @Router /v1/myinventory/{id}/image [get]

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 GetPackItemImage added in v1.7.0

func GetPackItemImage(c *gin.Context)

GetPackItemImage retrieves an image for an item within a pack @Summary Get pack item image @Description Get the image for an inventory item within a pack. Public packs are accessible without authentication. @Tags Pack Images @Produce image/jpeg @Param id path int true "Pack ID" @Param itemId path int true "Item (Inventory) ID" @Success 200 {file} image/jpeg "Item image" @Failure 400 {string} string "Invalid pack ID or item ID" @Failure 404 {string} string "Pack not found, item not in pack, or has no image" @Failure 500 {string} string "Internal server error" @Router /v1/packs/{id}/items/{itemId}/image [get]

func GetProfileImage added in v1.2.0

func GetProfileImage(c *gin.Context)

GetProfileImage retrieves the profile image for a given account @Summary Get profile image @Description Get the profile image for a user account @Tags Account Images @Produce image/jpeg @Param id path int true "Account ID" @Success 200 {file} image/jpeg "Profile image" @Failure 400 {string} string "Invalid account ID" @Failure 404 {string} string "No profile image found" @Failure 500 {string} string "Internal server error" @Router /v1/accounts/{id}/image [get]

func ResizeBannerImage added in v1.6.0

func ResizeBannerImage(img image.Image) image.Image

ResizeBannerImage scales and center-crops an image to exactly MaxBannerWidth x MaxBannerHeight

func ResizeImage

func ResizeImage(img image.Image) image.Image

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

func ResizeImageWithMax added in v1.2.0

func ResizeImageWithMax(img image.Image, maxDim int) image.Image

ResizeImageWithMax resizes an image if it exceeds maxDim, maintaining aspect ratio

func UploadInventoryItemImage added in v1.3.0

func UploadInventoryItemImage(c *gin.Context)

UploadInventoryItemImage uploads or updates an image for an inventory item @Summary Upload or update inventory item image @Description Upload or update an image for an inventory item. Only the item owner can upload images. @Security Bearer @Tags Inventory Images @Accept multipart/form-data @Produce json @Param id path int true "Inventory Item 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 "Item does not belong to user" @Failure 404 {object} map[string]string "Item not found" @Failure 413 {object} map[string]string "Payload too large" @Failure 500 {object} map[string]string "Internal server error" @Failure 503 {object} map[string]string "Feature disabled" @Router /v1/myinventory/{id}/image [post]

func UploadMyBannerImage added in v1.6.0

func UploadMyBannerImage(c *gin.Context)

UploadMyBannerImage uploads or updates the banner image for the current user @Summary Upload or update banner image @Description Upload or update the banner/hero image for the currently logged-in user @Security Bearer @Tags Account Images @Accept multipart/form-data @Produce json @Param image formData file true "Image file (JPEG, PNG, or WebP, max 5MB)" @Success 200 {object} map[string]interface{} "Banner image uploaded successfully" @Failure 400 {object} map[string]string "Invalid request or image" @Failure 401 {object} map[string]string "Unauthorized" @Failure 413 {object} map[string]string "Payload too large" @Failure 500 {object} map[string]string "Internal server error" @Router /v1/myaccount/banner [post]

func UploadMyProfileImage added in v1.2.0

func UploadMyProfileImage(c *gin.Context)

UploadMyProfileImage uploads or updates the profile image for the current user @Summary Upload or update profile image @Description Upload or update the profile image for the currently logged-in user @Security Bearer @Tags Account Images @Accept multipart/form-data @Produce json @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 413 {object} map[string]string "Payload too large" @Failure 500 {object} map[string]string "Internal server error" @Router /v1/myaccount/image [post]

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 AccountImageStorage added in v1.2.0

type AccountImageStorage interface {
	// Save stores a profile image for an account
	Save(ctx context.Context, accountID uint, data []byte, metadata ImageMetadata) error

	// Get retrieves a profile image for an account
	// Returns ErrNotFound if the image doesn't exist
	Get(ctx context.Context, accountID uint) (*Image, error)

	// Delete removes a profile image for an account
	// Returns nil if the image doesn't exist (idempotent)
	Delete(ctx context.Context, accountID uint) error

	// Exists checks if a profile image exists for an account
	Exists(ctx context.Context, accountID uint) (bool, error)
}

AccountImageStorage defines the interface for account profile image storage operations

type BannerImageStorage added in v1.6.0

type BannerImageStorage interface {
	// Save stores a banner image for an account
	Save(ctx context.Context, accountID uint, data []byte, metadata ImageMetadata) error

	// Get retrieves a banner image for an account
	// Returns ErrNotFound if the image doesn't exist
	Get(ctx context.Context, accountID uint) (*Image, error)

	// Delete removes a banner image for an account
	// Returns nil if the image doesn't exist (idempotent)
	Delete(ctx context.Context, accountID uint) error

	// Exists checks if a banner image exists for an account
	Exists(ctx context.Context, accountID uint) (bool, error)
}

BannerImageStorage defines the interface for account banner image storage operations

type DBStorage added in v1.3.0

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

DBStorage wraps dbImageStore with exported methods, implementing ImageStorage, AccountImageStorage, and InventoryImageStorage interfaces. Use the specific constructor functions to create instances for each table.

func NewDBAccountImageStorage added in v1.2.0

func NewDBAccountImageStorage() *DBStorage

NewDBAccountImageStorage creates a new database account image storage instance

func NewDBBannerImageStorage added in v1.6.0

func NewDBBannerImageStorage() *DBStorage

NewDBBannerImageStorage creates a new database banner image storage instance

func NewDBImageStorage

func NewDBImageStorage() *DBStorage

NewDBImageStorage creates a new database image storage instance for packs

func NewDBInventoryImageStorage added in v1.3.0

func NewDBInventoryImageStorage() *DBStorage

NewDBInventoryImageStorage creates a new database inventory image storage instance

func (*DBStorage) Delete added in v1.3.0

func (s *DBStorage) Delete(ctx context.Context, id uint) error

func (*DBStorage) Exists added in v1.3.0

func (s *DBStorage) Exists(ctx context.Context, id uint) (bool, error)

func (*DBStorage) Get added in v1.3.0

func (s *DBStorage) Get(ctx context.Context, id uint) (*Image, error)

func (*DBStorage) Save added in v1.3.0

func (s *DBStorage) Save(ctx context.Context, id uint, data []byte, metadata ImageMetadata) error

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 InventoryImageStorage added in v1.3.0

type InventoryImageStorage interface {
	// Save stores an image for an inventory item
	Save(ctx context.Context, itemID uint, data []byte, metadata ImageMetadata) error

	// Get retrieves an image for an inventory item
	// Returns ErrNotFound if the image doesn't exist
	Get(ctx context.Context, itemID uint) (*Image, error)

	// Delete removes an image for an inventory item
	// Returns nil if the image doesn't exist (idempotent)
	Delete(ctx context.Context, itemID uint) error

	// Exists checks if an image exists for an inventory item
	Exists(ctx context.Context, itemID uint) (bool, error)
}

InventoryImageStorage defines the interface for inventory item image storage operations

type ProcessedImage

type ProcessedImage struct {
	Data     []byte
	Metadata ImageMetadata
}

ProcessedImage contains the processed image data and metadata

func ProcessBannerImage added in v1.6.0

func ProcessBannerImage(data []byte) (*ProcessedImage, error)

ProcessBannerImage validates and processes an uploaded banner image

func ProcessBannerImageFromReader added in v1.6.0

func ProcessBannerImageFromReader(reader io.Reader) (*ProcessedImage, error)

ProcessBannerImageFromReader processes a banner image from an io.Reader

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

func ProcessInventoryItemImage added in v1.6.0

func ProcessInventoryItemImage(data []byte) (*ProcessedImage, error)

ProcessInventoryItemImage validates and processes an uploaded inventory item picture Uses MaxInventoryItemDimension (400px) and InventoryItemJPEGQuality (70%)

func ProcessInventoryItemImageFromReader added in v1.6.0

func ProcessInventoryItemImageFromReader(reader io.Reader) (*ProcessedImage, error)

ProcessInventoryItemImageFromReader processes an inventory item image from an io.Reader

func ProcessProfileImage added in v1.2.0

func ProcessProfileImage(data []byte) (*ProcessedImage, error)

ProcessProfileImage validates and processes an uploaded profile picture Uses MaxProfilePicDimension (512px) instead of MaxDimension (1920px)

func ProcessProfileImageFromReader added in v1.2.0

func ProcessProfileImageFromReader(reader io.Reader) (*ProcessedImage, error)

ProcessProfileImageFromReader processes a profile image from an io.Reader

Jump to

Keyboard shortcuts

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