utils

package
v1.4.5 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2025 License: MIT Imports: 27 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DeviceMobile  = "mobile"
	DeviceDesktop = "desktop"
)

String constants for device types

Variables

View Source
var Config *config.Config

Global configuration instance

View Source
var ErrInvalidOffset = errors.New("invalid offset")
View Source
var S3Client *s3.Client
View Source
var SupportedImageExtensions = []string{".jpg", ".jpeg", ".png", ".gif", ".webp", ".avif"}

SupportedImageExtensions contains all file extensions recognized by the application

Functions

func ConvertToAVIF

func ConvertToAVIF(data []byte) ([]byte, error)

ConvertToAVIF converts image data to AVIF format

func ConvertToWebP

func ConvertToWebP(data []byte) ([]byte, error)

ConvertToWebP converts image data to WebP format

func DetectDeviceType

func DetectDeviceType(r *http.Request) string

DetectDeviceType returns a string identifier ("mobile" or "desktop") based on User-Agent

func GetDeviceTypeFromUserAgent

func GetDeviceTypeFromUserAgent(userAgent string) string

GetDeviceTypeFromUserAgent extracts device type from a User-Agent string

func GetFromS3

func GetFromS3(ctx context.Context, key string) ([]byte, error)

func GetRandomImage

func GetRandomImage(basePath string, deviceType DeviceType, avifSupport bool) (string, error)

GetRandomImage returns a path to a random image with appropriate format for device and browser

DEPRECATED: This function is deprecated and will be removed in a future version. Use handlers.RandomImage, handlers.RandomImageHandler, or handlers.LocalRandomImageHandler instead, which offer better PNG support and more consistent behavior across storage types.

func InitCleaner

func InitCleaner()

InitCleaner initializes and starts the image cleaner

func InitMetadataStore

func InitMetadataStore() error

InitMetadataStore initializes the metadata storage

func InitS3Client

func InitS3Client() error

func InitStorage

func InitStorage() error

InitStorage initializes the global storage provider

func IsImageFile

func IsImageFile(filename string) bool

IsImageFile checks if a filename has a supported image extension

func ReadSeeker

func ReadSeeker(r io.Reader) (io.ReadSeeker, error)

ReadSeeker converts an io.Reader to an io.ReadSeeker by reading all content into memory

func SetConfig added in v1.4.3

func SetConfig(cfg *config.Config)

SetConfig sets the global configuration

func String

func String(v string) *string

String returns a pointer to the string value passed in

func TriggerCleanup

func TriggerCleanup()

TriggerCleanup manually triggers the cleanup process

func UploadToS3

func UploadToS3(ctx context.Context, key string, data []byte) error

Types

type ByteReadSeeker

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

ByteReadSeeker implements io.ReadSeeker for a byte slice

func NewByteReadSeeker

func NewByteReadSeeker(data []byte) *ByteReadSeeker

func (*ByteReadSeeker) Read

func (b *ByteReadSeeker) Read(p []byte) (n int, err error)

func (*ByteReadSeeker) Seek

func (b *ByteReadSeeker) Seek(offset int64, whence int) (int64, error)

type DeviceType

type DeviceType int
const (
	Desktop DeviceType = iota
	Mobile
)

func DetectDevice

func DetectDevice(r *http.Request) DeviceType

DetectDevice returns the DeviceType enum (Mobile or Desktop) based on User-Agent

type ImageCleaner

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

ImageCleaner is responsible for cleaning up expired images

var Cleaner *ImageCleaner

Global cleaner instance

func NewImageCleaner

func NewImageCleaner() *ImageCleaner

NewImageCleaner creates a new image cleaner

func (*ImageCleaner) Start

func (ic *ImageCleaner) Start()

Start begins the periodic cleanup task

func (*ImageCleaner) Stop

func (ic *ImageCleaner) Stop()

Stop terminates the cleanup task

type ImageFormatInfo

type ImageFormatInfo struct {
	Format    string // Format name (e.g., "jpeg", "png", "gif")
	Extension string // File extension (e.g., ".jpg", ".png", ".gif")
	MimeType  string // MIME type (e.g., "image/jpeg", "image/png", "image/gif")
}

ImageFormatInfo contains information about an image's format

func DetectImageFormat

func DetectImageFormat(data []byte) (ImageFormatInfo, error)

DetectImageFormat detects the format of an image from its binary data

type ImageMetadata

type ImageMetadata struct {
	ID           string    `json:"id"`           // Image ID (without extension)
	OriginalName string    `json:"originalName"` // Original filename
	UploadTime   time.Time `json:"uploadTime"`   // Upload timestamp
	ExpiryTime   time.Time `json:"expiryTime"`   // Expiry timestamp (if set)
	Format       string    `json:"format"`       // Original format
	Orientation  string    `json:"orientation"`  // Image orientation
	Tags         []string  `json:"tags"`         // Image tags for categorization
	Paths        struct {
		Original string `json:"original"` // Path to original image
		WebP     string `json:"webp"`     // Path to WebP format
		AVIF     string `json:"avif"`     // Path to AVIF format
	} `json:"paths"`
}

ImageMetadata stores metadata information for images

type LocalMetadataStore

type LocalMetadataStore struct {
	BasePath string
}

LocalMetadataStore implements metadata storage for local filesystem

func NewLocalMetadataStore

func NewLocalMetadataStore(basePath string) (*LocalMetadataStore, error)

NewLocalMetadataStore creates a new local metadata store

func (*LocalMetadataStore) DeleteMetadata

func (lms *LocalMetadataStore) DeleteMetadata(ctx context.Context, id string) error

DeleteMetadata deletes image metadata

func (*LocalMetadataStore) GetMetadata

func (lms *LocalMetadataStore) GetMetadata(ctx context.Context, id string) (*ImageMetadata, error)

GetMetadata retrieves image metadata from a local file

func (*LocalMetadataStore) ListExpiredImages

func (lms *LocalMetadataStore) ListExpiredImages(ctx context.Context) ([]*ImageMetadata, error)

ListExpiredImages lists all expired images

func (*LocalMetadataStore) SaveMetadata

func (lms *LocalMetadataStore) SaveMetadata(ctx context.Context, metadata *ImageMetadata) error

SaveMetadata saves image metadata to a local file

type LocalStorage

type LocalStorage struct {
	BasePath string
}

LocalStorage implements StorageProvider for local filesystem

func NewLocalStorage

func NewLocalStorage(basePath string) (*LocalStorage, error)

func (*LocalStorage) Delete

func (ls *LocalStorage) Delete(ctx context.Context, key string) error

func (*LocalStorage) Get

func (ls *LocalStorage) Get(ctx context.Context, key string) ([]byte, error)

func (*LocalStorage) Store

func (ls *LocalStorage) Store(ctx context.Context, key string, data []byte) error

type MetadataStore

type MetadataStore interface {
	SaveMetadata(ctx context.Context, metadata *ImageMetadata) error
	GetMetadata(ctx context.Context, id string) (*ImageMetadata, error)
	ListExpiredImages(ctx context.Context) ([]*ImageMetadata, error)
	DeleteMetadata(ctx context.Context, id string) error
}

MetadataStore defines the interface for metadata storage operations

var MetadataManager MetadataStore

Global metadata storage instance

type S3MetadataStore

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

S3MetadataStore implements metadata storage for S3

func NewS3MetadataStore

func NewS3MetadataStore(s3Storage *S3Storage) *S3MetadataStore

NewS3MetadataStore creates a new S3 metadata store

func (*S3MetadataStore) DeleteMetadata

func (sms *S3MetadataStore) DeleteMetadata(ctx context.Context, id string) error

DeleteMetadata deletes image metadata from S3

func (*S3MetadataStore) GetMetadata

func (sms *S3MetadataStore) GetMetadata(ctx context.Context, id string) (*ImageMetadata, error)

GetMetadata retrieves image metadata from S3

func (*S3MetadataStore) ListExpiredImages

func (sms *S3MetadataStore) ListExpiredImages(ctx context.Context) ([]*ImageMetadata, error)

ListExpiredImages lists all expired images in S3

func (*S3MetadataStore) SaveMetadata

func (sms *S3MetadataStore) SaveMetadata(ctx context.Context, metadata *ImageMetadata) error

SaveMetadata saves image metadata to S3

type S3Object

type S3Object struct {
	Key  string
	Size int64
}

S3Object represents an object in S3 storage

type S3Storage

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

S3Storage implements StorageProvider for S3-compatible storage

func NewS3Storage

func NewS3Storage() (*S3Storage, error)

func (*S3Storage) Delete

func (s *S3Storage) Delete(ctx context.Context, key string) error

func (*S3Storage) Get

func (s *S3Storage) Get(ctx context.Context, key string) ([]byte, error)

func (*S3Storage) ListObjects

func (s *S3Storage) ListObjects(ctx context.Context, prefix string) ([]S3Object, error)

ListObjects lists objects in S3 with the given prefix

func (*S3Storage) Store

func (s *S3Storage) Store(ctx context.Context, key string, data []byte) error

type StorageConfig

type StorageConfig struct {
	Type      string // "local" or "s3"
	LocalPath string // base path for local storage
}

StorageConfig represents the storage configuration

type StorageProvider

type StorageProvider interface {
	Store(ctx context.Context, key string, data []byte) error
	Get(ctx context.Context, key string) ([]byte, error)
	Delete(ctx context.Context, key string) error
}

StorageProvider defines the interface for storage operations

var Storage StorageProvider

Global storage instance

func NewStorageProvider

func NewStorageProvider(cfg StorageConfig) (StorageProvider, error)

NewStorageProvider creates a new storage provider based on configuration

Jump to

Keyboard shortcuts

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