pictrs

package
v0.0.0-...-6e2970e Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2025 License: AGPL-3.0 Imports: 15 Imported by: 0

Documentation

Overview

Package pictrs provides an API wrapper for pict-rs: https://git.asonix.dog/asonix/pict-rs.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoExtension = errors.New("no extension provided")

ErrNoExtension indicates that no file extension was provided when it was expected.

Functions

func Value

func Value[T any](v T) *T

Value returns a *T containing the value v.

Types

type Client

type Client struct {
	BaseURL  string
	APIToken string
	// contains filtered or unexported fields
}

Client represents a client for interacting with the pict-rs API.

func (*Client) BlurHashImage

func (c *Client) BlurHashImage(ctx context.Context, file string) (string, error)

BlurHashImage retrieves the BlurHash representation of the image file from the pict-rs server.

BlurHash is described in https://blurha.sh/.

func (*Client) DeleteImage

func (c *Client) DeleteImage(ctx context.Context, file, deleteToken string) error

DeleteImage deletes an image file from the pict-rs server using the provided delete token associated with the file.

func (*Client) Healthz

func (c *Client) Healthz(ctx context.Context) error

Healthz checks the health of the pict-rs server.

func (*Client) OriginalImage

func (c *Client) OriginalImage(ctx context.Context, file string) (FileDownloader, error)

OriginalImage retrieves the full resolution image file from the pict-rs server. The returned io.ReadCloser will contain the original image data.

func (*Client) OriginalImageDetails

func (c *Client) OriginalImageDetails(ctx context.Context, file string) (UploadedFileDetails, error)

OriginalImageDetails retrieves the details of the original image file from the pict-rs server.

func (*Client) ProcessedImage

func (c *Client) ProcessedImage(ctx context.Context, file string, ext Extension, transformations Transformations) (FileDownloader, error)

ProcessedImage retrieves a processed version of the image file from the pict-rs server. If [ext] is empty, it'll be taken from [file], otherwise it's an error.

func (*Client) ProcessedImageDetails

func (c *Client) ProcessedImageDetails(ctx context.Context, file string, ext Extension, transformations Transformations) (UploadedFileDetails, error)

ProcessedImageDetails retrieves the details of a processed image file from the pict-rs server. If [ext] is empty, it'll be taken from [file], otherwise it's an error.

func (*Client) UploadImages

func (c *Client) UploadImages(ctx context.Context, files []FileUploader, params UploadParams) ([]UploadedFile, error)

UploadImages uploads files to the pict-rs server.

func (*Client) UploadImagesBackgrounded

func (c *Client) UploadImagesBackgrounded(ctx context.Context, files []FileUploader, params UploadParams) ([]string, error)

UploadImagesBackgrounded uploads files to the pict-rs server, but the files are processed in the background, allowing for immediate response. It returns a list of upload IDs.

func (*Client) UploadRemoteImage

func (c *Client) UploadRemoteImage(ctx context.Context, remoteURL string) (*UploadedFile, error)

UploadRemoteImage uploads the image at the remote URL to the pict-rs server.

func (*Client) UploadRemoteImageBackgrounded

func (c *Client) UploadRemoteImageBackgrounded(remoteURL string) (string, error)

UploadRemoteImageBackgrounded uploads the image at the remote URL to the pict-rs server, but the upload is processed in the background. It returns the upload ID for later retrieval.

func (*Client) WaitForUpload

func (c *Client) WaitForUpload(ctx context.Context, uploadID string) (*UploadedFile, error)

WaitForUpload waits for a backgrounded upload to complete and returns the result from the pict-rs server. If the file is not yet available and the waiting has timed out, it will be retried until the context is done.

func (*Client) WithHTTPClient

func (c *Client) WithHTTPClient(client *http.Client) *Client

WithHTTPClient sets the HTTP client to use for requests.

func (*Client) WithLogger

func (c *Client) WithLogger(logger *slog.Logger) *Client

WithLogger sets the logger to use for requests.

type CropTransformation

type CropTransformation struct {
	Width  int
	Height int
}

CropTransformation produces a cropped version of the image with the given width and height. The resulting crop is centered on the original image.

func (CropTransformation) MarshalText

func (c CropTransformation) MarshalText() ([]byte, error)

func (CropTransformation) String

func (c CropTransformation) String() string

func (*CropTransformation) UnmarshalText

func (c *CropTransformation) UnmarshalText(b []byte) error

type Extension

type Extension string

Extension represents a file extension, such as ".jpg" or ".png".

const (
	InheritExtension Extension = ""
	APNGExtension    Extension = ".apng"
	AVIFExtension    Extension = ".avif"
	GIFExtension     Extension = ".gif"
	JPGExtension     Extension = ".jpg"
	JPEGExtension    Extension = ".jpg"
	JXLExtension     Extension = ".jxl"
	PNGExtension     Extension = ".png"
	WebPExtension    Extension = ".webp"
)

type FileDownloader

type FileDownloader interface {
	io.ReadCloser
	ContentType() string
	ContentLength() int64 // -1 if unknown
}

FileDownloader extends io.ReadCloser to represent a reader for downloading image data while also querying its metadata.

type FileFormat

type FileFormat string

FileFormat is the format of the uploaded file.

const (
	ImageFormat     FileFormat = "Image"
	VideoFormat     FileFormat = "Video"
	AnimationFormat FileFormat = "Animation"
)

type FileUploader

type FileUploader struct {
	Name   string
	Reader io.Reader
}

FileUploader is a file that is to be uploaded to the pict-rs server. It must have a Reader and optionally a Name.

type ResizeTransformation

type ResizeTransformation struct {
	Size   int
	Area   bool
	Filter string
}

ResizeTransformation represents a resize transformation for an image. If area is false, then the image will be resized to [Size]x[Size] pixels, otherwise the image will be resized such that the area is at most [Size] pixels.

func (ResizeTransformation) MarshalText

func (r ResizeTransformation) MarshalText() ([]byte, error)

func (ResizeTransformation) String

func (r ResizeTransformation) String() string

func (*ResizeTransformation) UnmarshalText

func (r *ResizeTransformation) UnmarshalText(b []byte) error

type ServerError

type ServerError struct {
	Code       string `json:"code,omitempty"`
	Message    string `json:"msg"`
	HTTPStatus int    `json:"-"`
}

ServerError represents an error response from the pict-rs server.

func (*ServerError) Error

func (e *ServerError) Error() string

type Transformations

type Transformations struct {
	Identity  bool                  `schema:"identity,omitempty"`
	Blur      *float64              `schema:"blur,omitempty"`
	Thumbnail *int                  `schema:"thumbnail,omitempty"`
	Resize    *ResizeTransformation `schema:"resize,omitempty"`
	Crop      *CropTransformation   `schema:"crop,omitempty"`
}

Transformations represents a set of transformations that can be applied to an image.

type UploadParams

type UploadParams struct {
	MaxWidth       *int  `schema:"max_width,omitempty"`
	MaxHeight      *int  `schema:"max_height,omitempty"`
	MaxArea        *int  `schema:"max_area,omitempty"`
	MaxFrameCount  *int  `schema:"max_frame_count,omitempty"`
	MaxFileSizeMB  *int  `schema:"max_file_size,omitempty"`
	AllowImage     *bool `schema:"allow_image,omitempty"`
	AllowAnimation *bool `schema:"allow_animation,omitempty"`
	AllowVideo     *bool `schema:"allow_video,omitempty"`
}

UploadParams contains arguments for

type UploadedFile

type UploadedFile struct {
	DeleteToken string              `json:"delete_token"`
	File        string              `json:"file"`
	Details     UploadedFileDetails `json:"details"`
}

UploadedFile is a file that has been uploaded to the pict-rs server.

type UploadedFileDetails

type UploadedFileDetails struct {
	BlurHash    string     `json:"blurhash"`
	Width       int        `json:"width"`
	Height      int        `json:"height"`
	ContentType string     `json:"content_type"`
	CreatedAt   time.Time  `json:"created_at"`
	Format      FileFormat `json:"format"`
}

UploadedFileDetails contains details about an uploaded file.

Jump to

Keyboard shortcuts

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