pluto

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2026 License: CC0-1.0 Imports: 33 Imported by: 1

README

Pluto Image Service

Pluto Image Service is a lightweight, high-performance image processing and serving service built in Go. It supports dynamic image resizing, caching, and format conversion via a simple HTTP API.

Features

  • Resize, crop, and convert images on the fly
  • Caching for fast image delivery
  • Easy-to-use HTTP API
  • Built with Go for speed and efficiency

Usage

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CleanupPlutoCache added in v0.2.0

func CleanupPlutoCache(imageId int) (int, error)

Delete cache files

func CleanupPlutoImage added in v0.2.0

func CleanupPlutoImage(imageFileName string) (bool, error)

Delete original image file

func CropWithFocus added in v0.1.8

func CropWithFocus(
	img image.Image,
	targetRatio float32,
	focusX, focusY float32,
	targetW, targetH int,
) image.Image

CropWithFocus crops an image to a target aspect ratio and optionally resizes, centered on the focus point, while preserving alpha transparency.

func CropWithFocusWithoutAlpha added in v0.3.0

func CropWithFocusWithoutAlpha(
	img image.Image,
	targetRatio float32,
	focusX, focusY float32,
	targetW, targetH int,
) image.Image

func DecodeFloat32FromPath added in v0.1.8

func DecodeFloat32FromPath(s string) (float32, error)

DecodeFloat32FromPath reverses the encoding back to float32.

func DeleteCacheTx added in v0.2.0

func DeleteCacheTx(
	ctx context.Context,
	tx pgx.Tx,
	imageID int,
) (deletedFilesCount int64, err error)

Deletes cache DB entries, return number of affected rows

func DeleteFilesWithPrefix added in v0.2.0

func DeleteFilesWithPrefix(dir string, prefix string) (int, error)

DeleteFilesWithPrefix deletes all files in a directory that start with the given prefix. Returns the number of deleted files and an error (if any).

func DeleteImageTx added in v0.2.0

func DeleteImageTx(
	ctx context.Context,
	tx pgx.Tx,
	imageId int,
) (deletedFileName string, cacheRows int64, err error)

Deletes image + cache DB entries, returns filename to delete from disk

func EncodeFloat32ForPath added in v0.1.8

func EncodeFloat32ForPath(f float32) string

EncodeFloat32ForPath converts a float32 into a safe ASCII filename string. Output contains only [0-9a-f] and is exactly 8 characters long.

func FloatPtrEqual added in v0.2.0

func FloatPtrEqual(a, b *float64) bool

func GenerateImageFilename

func GenerateImageFilename(originalName string) (string, error)

GenerateImageFilename returns a securely generated random filename that preserves the original file's extension.

It creates a 128-bit (16-byte) cryptographically secure random identifier, encodes it as a 32-character hexadecimal string, and appends the original file extension (e.g. ".jpg", ".png") from the input filename.

This function is suitable for generating unique, unpredictable image filenames for uploaded files.

Example:

"cat.jpg" => "f3ab7c54c8a44f01bd1182d4a57c121a.jpg"

Parameters:

originalName - the original filename, from which the extension will be preserved.

Returns:

  • A string containing the generated filename (randomHex + original extension).
  • An error if the secure random number generation fails.

func GetImageFocusTx added in v0.2.0

func GetImageFocusTx(
	ctx context.Context,
	tx pgx.Tx,
	imageID int,
) (focusX *float64, focusY *float64, err error)

GetImageFocus returns focus_x and focus_y for an image

func GetImageIdByByContext added in v0.3.0

func GetImageIdByByContext(
	gc *gin.Context,
	context string,
	contextId int,
	identifier string,
) (int, bool)

func GetQueryBoolDefault added in v0.3.0

func GetQueryBoolDefault(gc *gin.Context, key string, def bool) (bool, bool)

func GetQueryInt added in v0.1.8

func GetQueryInt(gc *gin.Context, key string) (int, bool)

func GetQueryIntDefault added in v0.1.8

func GetQueryIntDefault(gc *gin.Context, key string, def int) (int, bool)

func ParamInt added in v0.1.4

func ParamInt(gc *gin.Context, key string) (int, bool)

func ParseAspectRatio

func ParseAspectRatio(s string) (float32, error)

func RemoveFile added in v0.2.0

func RemoveFile(path string) error

RemoveFile deletes a file at the given path. Returns an error if the file cannot be deleted.

func ResizeToWidth

func ResizeToWidth(img image.Image, width int, ratio string) image.Image

Types

type ApiTxError added in v0.3.0

type ApiTxError struct {
	Code int
	Err  error
}

func ApiErrForbidden added in v0.3.0

func ApiErrForbidden(msg string, args ...any) *ApiTxError

Specialized helpers

func ApiErrInternal added in v0.3.0

func ApiErrInternal(msg string, args ...any) *ApiTxError

func ApiErrNotFound added in v0.3.0

func ApiErrNotFound(msg string, args ...any) *ApiTxError

func NewApiTxError added in v0.3.0

func NewApiTxError(code int, msg string, args ...any) *ApiTxError

func WithTransaction added in v0.3.0

func WithTransaction(ctx context.Context, db *pgxpool.Pool, fn func(tx pgx.Tx) *ApiTxError) *ApiTxError

func (*ApiTxError) Error added in v0.3.0

func (e *ApiTxError) Error() string

type CacheEntry added in v0.3.0

type CacheEntry struct {
	Id        int       `json:"id"`
	Receipt   string    `json:"receipt"`
	ImageId   *int      `json:"image_id,omitempty"`
	CreatedAt time.Time `json:"created_at"`
	MimeType  *string   `json:"mime_type,omitempty"`
}

type Config

type Config struct {
	BaseApiUrl            string `json:"base_api_url"`
	DbHost                string `json:"db_host"`
	DbPort                int    `json:"db_port"`
	DbUser                string `json:"db_user"`
	DbPassword            string `json:"db_password"`
	DbName                string `json:"db_name"`
	DbSchema              string `json:"db_schema"`
	SSLMode               string `json:"ssl_mode"`
	PlutoVerbose          bool   `json:"pluto_verbose"`
	PlutoRoute            string `json:"pluto_route"`
	PlutoImageDir         string `json:"pluto_image_dir"`
	PlutoCacheDir         string `json:"pluto_cache_dir"`
	PlutoMaxImagePx       int    `json:"pluto_max_image_px"`
	PlutoDefaultQuality   int    `json:"pluto_default_quality"`
	PlutoDefaultImageType string `json:"pluto_default_image_type"`
}

Config holds database configuration details

func DefaultConfig added in v0.2.0

func DefaultConfig() Config

func (Config) Print

func (config Config) Print()

type DeleteImageResult added in v0.3.0

type DeleteImageResult struct {
	HttpStatus        int
	Message           string
	FileRemovedFlag   bool
	CacheFilesRemoved int
	ImageId           int
}

DeleteImageResult mirrors UpsertImageResult

func DeleteImage added in v0.3.0

func DeleteImage(
	gc *gin.Context,
	context string,
	contextId int,
	identifier string,
	postCallback TxFunc,
) (DeleteImageResult, error)

DeleteImage deletes an image by context/contextId/identifier

type ImageCleanupResult added in v0.2.0

type ImageCleanupResult struct {
	CacheFilesRemoved int
	ImageFileRemoved  bool
}

func CleanupPlutoImageFiles added in v0.2.0

func CleanupPlutoImageFiles(
	imageId int,
	fileName *string,
) (*ImageCleanupResult, error)

type ImageMeta added in v0.3.0

type ImageMeta struct {
	Id           *int           `json:"id"`
	FileName     *string        `json:"file_name,omitempty"`
	Width        *int           `json:"width,omitempty"`
	Height       *int           `json:"height,omitempty"`
	MimeType     *string        `json:"mime_type,omitempty"`
	Alt          *string        `json:"alt,omitempty"`
	Description  *string        `json:"description,omitempty"`
	License      *int           `json:"license,omitempty"`
	Exif         map[string]any `json:"exif,omitempty"`
	Expiration   *string        `json:"expiration_date,omitempty"`
	Creator      *string        `json:"creator,omitempty"`
	Copyright    *string        `json:"copyright,omitempty"`
	FocusX       *float64       `json:"focus_x,omitempty"`
	FocusY       *float64       `json:"focus_y,omitempty"`
	MarginLeft   *int           `json:"margin_left,omitempty"`
	MarginRight  *int           `json:"margin_right,omitempty"`
	MarginTop    *int           `json:"margin_top,omitempty"`
	MarginBottom *int           `json:"margin_bottom,omitempty"`
}

type Pluto

type Pluto struct {
	Config   Config
	Verbose  bool
	DbPool   *pgxpool.Pool
	DbSchema string
}
var PlutoInstance *Pluto

func Initialize added in v0.1.3

func Initialize(configFilePath string, pool *pgxpool.Pool, verbose bool) (*Pluto, error)

New creates and initializes a new Pluto instance

func (*Pluto) Log

func (pluto *Pluto) Log(msg string)

func (*Pluto) RegisterRoutes

func (pluto *Pluto) RegisterRoutes(rg *gin.RouterGroup, middlewares ...gin.HandlerFunc)

type TxFunc added in v0.3.0

type TxFunc func(ctx context.Context, tx pgx.Tx) error

type UpsertImageResult added in v0.3.0

type UpsertImageResult struct {
	HttpStatus        int
	Message           string
	FileRemovedFlag   bool
	CacheFilesRemoved int
	ImageId           int
}

func UpsertImage added in v0.3.0

func UpsertImage(
	gc *gin.Context,
	context string,
	contextId int,
	identifier string,
	fileNamePrefix *string,
	userId int,
	postCallback TxFunc,
) (UpsertImageResult, error)

Jump to

Keyboard shortcuts

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